# Module 5.4: Particle Filters and Global Localization

- Course: [Course 5: Sensors, State Estimation & Localization (SLAM)](https://roboticsconcepts.dev/courses/sensors-state-estimation-slam.md)
- HTML Page: https://roboticsconcepts.dev/courses/sensors-state-estimation-slam/particle-filters-and-global-localization/
- Key Concepts: Monte Carlo Localization (MCL), Importance weighting & resampling, Solving the kidnapped robot problem

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.

## 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 Sandbox: Particle Filter Kidnapped Robot Floorplan Demo

Click Move + Sense and Resample in the symmetric two-corridor floorplan below. Watch the 80 particles split into two competing clusters across the identical corridors before snapping to the true hallway — then click Kidnap Robot to test how the filter recovers.

## 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

```text
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.

## Check Your Intuition

**Question:** Why does a particle filter beat a standard single-Gaussian EKF when a robot powers on inside an unfamiliar hallway with no starting coordinates?

- [✓] Particles can represent multiple separate candidate rooms simultaneously until an asymmetric feature breaks the tie
- [ ] Particle filters never need a map of the walls
- [ ] Particles run faster because they ignore sensor noise

**Explanation:** Global localization starts with multimodal uncertainty across symmetric places; a particle cloud naturally holds multiple separate clusters until measurements disambiguate them.

## Where You'll See This in Modern Robotics

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.

## Companion 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.

- Open in molab (WASM): https://molab.marimo.io/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/course-5/04_particle_filters_localization.py/wasm
- Open in Google Colab: https://colab.research.google.com/github/alchemicduncan/robotics-concepts-dev/blob/main/notebooks/colab/course-5/04_particle_filters_localization.ipynb
- Download .py: https://roboticsconcepts.dev/downloads/notebooks/course-5/04_particle_filters_localization.py
- Download .ipynb: https://roboticsconcepts.dev/downloads/notebooks/colab/course-5/04_particle_filters_localization.ipynb
- Run locally: `uvx marimo edit --sandbox notebooks/course-5/04_particle_filters_localization.py`
