Particle Filters and Global Localization
Picture waking up inside an office building with two identical long hallways. Until you walk to the end and spot the breakroom coffee machine, you have two completely separate, equally good guesses for where you are. A single Gaussian bell curve can't hold two separate hypotheses at once — and that is exactly why particle filters exist.
Core ideas covered in this module
- Monte Carlo Localization (MCL)
- Importance weighting & resampling
- Solving the kidnapped robot problem
Hundreds of tiny guesses voting together
Instead of summarizing belief as one ellipse, Monte Carlo Localization (MCL) scatters hundreds or thousands of weighted dots — particles — across the map. Each particle is a concrete hypothesis: 'Maybe I'm right here, facing east.'
When the robot moves one meter forward, every particle moves one meter forward in its own local direction (plus a sprinkle of random motion noise). When the 2D LiDAR scans the walls, we ask each particle: if I were standing at your spot, would my laser rays hit walls at roughly these same distances? Particles that match the scan get heavy weights; particles facing an open doorway when the robot sees a wall get crushed toward zero.
Importance resampling and the kidnapped robot problem
After weighting, we resample: draw a fresh batch of particles where high-weight survivors get duplicated and near-zero guesses vanish. In a symmetric hallway, you'll watch two clusters survive side-by-side until one asymmetrical corner or alcove breaks the tie and collapses the whole cloud onto the true robot.
What if someone picks the robot up while it's running and drops it in a completely different room — the classic kidnapped robot problem? If every particle had already converged to the wrong hallway, pure resampling can get trapped. Mixing a small fraction of fresh random particles into each resample gives the filter a way to rediscover its true room.
Interactive lab
Particle Filter Kidnapped Robot Floorplan Demo
Canvas / SVG· LiveTip: Alternate Move + Sense and Resample. Both hallways look identical until the robot passes the right-hand alcove.
Watching Effective Sample Size (N_eff)
Keep an eye on the Effective Sample Size (N_eff) readout. Right after a uniform reset, all 80 particles carry equal weight, so N_eff = 80. After a sharp laser scan rules out bad guesses, only a handful of particles hold most of the weight and N_eff plunges — that's your cue to resample so compute isn't wasted tracking dead hypotheses.
The trade-off with particle filters is dimensionality: 80 to 500 particles easily cover a 2D floorplan (x, y, θ), whereas covering a 12-joint humanoid state space with particles would take more dots than atoms in the room. Pick the filter that fits your state dimension.
Importance weight and Effective Sample Size
w_t^(i) ∝ p(z_t | x_t^(i)) | N_eff = 1 / Σ (w̃_t^(i))²
Normalized weights w̃ sum to 1. When only a few particles match the scan, N_eff drops low and triggers resampling.
Why does a particle filter beat a standard single-Gaussian EKF when a robot powers on inside an unfamiliar hallway with no starting coordinates?
ROS's classic AMCL (Adaptive Monte Carlo Localization) package runs on thousands of factory and hospital mobile robots, dynamically scaling particle counts up when confused and down once locked onto a floorplan.
Hands-On Python Notebook
04 · Particle Filter & Kidnapped Robot Lab
Build a Monte Carlo Localization particle filter with low-variance resampling and random particle injection for global localization.
Try this on your own inside the notebook
- Test particle starvation when random injection is turned off after kidnapping
- Compare convergence speed with N=30 vs. N=200 particles
In Google Colab, use File → Save a copy in Drive to keep your own edits and notes. Or download the files directly:
uvx marimo edit --sandbox notebooks/course-5/04_particle_filters_localization.pyWrap up this module
Saved automatically in your browser profile so you can pick up right where you left off.