Sequential Monte Carlo Particle Filters
Tracking a moving target with a cloud of guesses. Thousands of weighted particles chase the posterior through time, cloning the good ones.
Why Does This Exist?
Some posteriors move. A robot localizes, a phone tracks a voice, a fund estimates volatility: each new observation should update the belief about a hidden state that itself evolves. When the motion and the sensors are linear and Gaussian, the Kalman filter answers in closed form. When they are nonlinear or the noise is multimodal, no closed form exists, and the particle filter is the workhorse: it represents the filtering distribution with a cloud of weighted samples propagated through time.
Each step has three moves: propose particles forward through the motion model, weight them by how well they explain the new observation, and resample to clone the winners. That last step is the difference between a filter and a slow collapse, and it is where beginners go wrong.
The one idea you need first
Importance sampling estimates expectations under with samples from by weighting each sample with . A particle filter applies this sequentially: the proposal is the motion model and the weight update is the observation likelihood. The static version is covered alongside Monte Carlo methods.
Think of It Like This
A search party with radios
A lost hiker moves through hills while the search team holds five guesses of the position. Each hour, every guess walks forward according to the hiker's habits (the motion model), spreading out. Then a helicopter reports a glimpse: guesses near the glimpse get high weight, far ones get near zero. Finally the team regroups: members holding bad guesses walk over to copy the good ones, so all five guesses cluster near the evidence before the next hour starts.
Without the regrouping step, after a few hours four of five guesses would be hopelessly lost and only one would carry all the weight: the team wasted. Resampling is the regroup. The price is milder: cloned guesses share ancestors, so diversity must come from the motion noise injected each round.
Where the analogy stops: real filters carry thousands of particles, not five, and the regroup uses a single systematic draw rather than a team decision.
How It Actually Works
State evolves by and observation arrives by . Given particles , one filtering step is:
- Propagate. Draw .
- Weight. Set , then normalize.
- Resample (when needed). Draw new particles with probabilities and reset all weights to .
The filtered mean estimates the hidden state. Resample when the effective sample size falls below about ; resampling every step is simpler and fine for an introduction.
Worked example
Five particles sit at with equal weights. Motion adds noise giving . Truth is and the sensor reports with Gaussian noise of standard deviation . Likelihood weights are proportional to :
- :
- :
- :
- :
- :
Normalized: . The weighted mean is . Resampling five draws from these weights yields, for one typical outcome, particles : the two best ancestors survive, the two worst vanish, and the equal-weight mean sits above truth before the next motion step corrects it. Effective sample size is : resampling was overdue, which is the normal state of affairs.
Code
The worked pass in code: propagate, weight by the Gaussian likelihood, report the effective sample size.
import numpy as np
rng = np.random.default_rng(9)parts = np.array([1.0, 1.5, 2.0, 2.5, 3.0]) # particlesparts = parts + rng.normal(0, 0.2, size=5) # motion noisew = np.exp(-0.5 * ((parts - 2.2) / 0.3) ** 2) # observation y=2.2w = w / w.sum() # normalizeprint(np.round(w, 3)) # -> mass on the two nearestprint(round(float((w**2).sum() ** -1), 2)) # -> effective sample sizeWeights concentrate on the particles nearest 2.2 and the effective size prints well below 5, signalling resampling. Add parts = rng.choice(parts, size=5, p=w) to resample and watch the losers disappear.
Watch Out For
Skipping resampling until one particle owns everything
Without resampling, weights compound multiplicatively and within a dozen steps a single particle carries 99.9 percent of the mass: thousands of likelihood evaluations wasted on ghosts. Monitor the effective sample size every step and resample below . This degeneracy is silent, the mean still prints, so the monitor must be code, not eyeballing.
Resampling noise swamping a small particle set
With only dozens of particles, resampling variance itself dominates: good ancestors die by bad luck and the filter jitters. Use systematic resampling (one uniform draw, evenly spaced thresholds) instead of multinomial draws, and scale with state dimension. A 10-dimensional state with 100 particles is hope, not filtering.
The Quick Version
- A particle filter tracks a moving posterior with weighted samples through propagate, weight, resample.
- Weights are the observation likelihood; the weighted mean estimates the hidden state.
- In the worked pass the two particles nearest the observation take 97 percent of the mass.
- Effective sample size near 1.8 out of 5 means resample now, not later.
- No resampling gives degeneracy: one particle owns everything within a dozen steps.
- Linear Gaussian cases deserve a Kalman filter instead; particles earn their cost on nonlinear ones.