# Module 5.2: Probabilistic Belief and Bayes Filtering

- 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/probabilistic-belief-and-bayes-filtering/
- Key Concepts: Belief distributions over state, Predict step (motion uncertainty), Update step (measurement Bayes rule)

When I first tried writing localization code, I kept wanting a single clean (x, y) variable for where the robot was. Every time a noisy sensor disagreed with the wheels, I didn't know which number to trust. Switching from tracking a single point to tracking a cloud of probability — a belief — is the moment state estimation finally clicks.

## Thinking in clouds instead of points

Imagine losing your keys somewhere in a dim hallway. You don't actually know their exact coordinate, so your brain carries a mental heat map: high probability near the coat rack, a decent chance by the kitchen counter, almost zero in the bathtub.

In robotics, that heat map over possible poses is called the belief distribution, written bel(x_t). Instead of pretending we know our exact pose, we keep both our best guess (the peak) and how uncertain we are (how wide the blob is).

## The two-beat rhythm of Bayes filtering

Every probabilistic filter — from simple 1D grids to full 3D SLAM — repeats the same two-step dance over and over:

- Predict (motion update): the robot drives forward using noisy wheel commands. Because motion adds fresh uncertainty (slip, bumps, gyro noise), the belief distribution shifts forward and spreads out wider.
- Update (measurement Bayes rule): a sensor spots a known landmark or wall. We multiply our spread-out prediction by the sensor's likelihood — shrinking the belief back down around the poses that agree with both.

## Interactive Sandbox: 2D Extended Kalman Filter Uncertainty Ellipse Visualizer

Use the Predict and Update buttons in the EKF visualizer below to see this two-beat cycle directly. Click Predict a few times in a row to watch the shaded uncertainty ellipse bloat, then click Update to see a landmark reading pull it back into focus.

## Why confidence calibration matters

Notice what happens if you turn on Landmark dropout and keep clicking Predict: without external measurements to anchor it, your uncertainty keeps growing without bound. That isn't a bug — it's an honest report from the math telling you the robot no longer knows where it is.

A robot that knows it is uncertain can slow down, spin its camera around to look for a marker, or ask for help. A robot that tracks only a single point estimate will confidently drive straight into a table leg.

## The recursive Bayes filter loop

```text
bel̄(x_t) = ∫ p(x_t | u_t, x_{t-1}) bel(x_{t-1}) dx_{t-1}   →   bel(x_t) = η · p(z_t | x_t) · bel̄(x_t)
```

Predict smears the prior belief forward through the noisy motion model; Update multiplies by the measurement likelihood and renormalizes.

## Check Your Intuition

**Question:** A robot sits completely still for 10 seconds with only its wheel odometry running (Predict) and no camera/LiDAR sightings (no Update). What should happen to its belief?

- [ ] Its uncertainty shrinks because it isn't moving
- [✓] Its uncertainty stays flat or widens slightly due to process noise, because no external measurement confirmed it stayed put
- [ ] Its belief jumps to a random corner of the room

**Explanation:** Only an external measurement update can inject fresh certainty about where you are relative to the world; predicting without sensing never shrinks uncertainty.

## Where You'll See This in Modern Robotics

Modern robot policies increasingly output distributions or uncertainty estimates alongside actions, borrowing the exact same idea: knowing when a perception estimate is fuzzy lets a planner stay cautious near obstacles.

## Companion Python Notebook: 02 · Discrete 1D & 2D Bayes Filter Belief Lab

Implement a cyclic hallway histogram Bayes filter alternating motion shift/blur (Predict) with door-sensor likelihood updates.

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