Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 38 ยท Localization and Mapping

Probability and the Bayes Filter for Robots

Learn why robots must reason with uncertainty, then build a discrete Bayes filter that locates a robot in a corridor from noisy door sensing, in Python.

No sensor is perfect and no wheel rolls exactly as commanded. A wheel slips on a wet floor, an ultrasonic sensor misses a thin chair leg, a camera blurs in motion. A robot that trusts a single reading will make wrong decisions. Probabilistic robotics treats everything the robot believes as a probability distribution and updates it as it moves and senses.

This lesson builds the core idea of the whole chapter, the Bayes filter, in its simplest form: a robot in a one-dimensional corridor that wants to know where it is.

Belief instead of a single answer

Instead of saying "the robot is at cell 4," we keep a belief: a probability for each possible position, summing to 1. At the start, if the robot has no clue where it is, every cell is equally likely: a uniform belief. As it gathers evidence, the belief becomes peaked around the true location.

Two ingredients of probability do the work:

  • Conditional probability, P(A | B): how likely A is, given B.
  • Bayes' rule: P(x | z) = P(z | x) * P(x) / P(z). Here x is the state (position) and z is a measurement. The belief P(x) before sensing is the prior, P(z | x) is the likelihood (the sensor model), and P(x | z) is the updated posterior.

In words: multiply what you believed by how well each possibility explains the reading, then renormalize so the numbers sum to one.

The Bayes filter loop

Belief Probability over every possible state.
Predict Motion shifts and blurs the belief.
Update A measurement sharpens it.
Repeat Every cycle, forever.
1 / 2

The world: a corridor with doors

Our robot lives on a ring of 10 cells. Doors are at cells 1, 2 and 7; the rest are plain wall. The robot has a door sensor that is right 80 percent of the time. It does not know where it started.

The two steps of a Bayes filter:

Update (sense). For every cell, multiply the belief by the probability of the actual reading if the robot were in that cell: 0.8 if the reading agrees with the map, 0.2 if not. Normalize.

Predict (move). The robot commands "move 1 cell right." Motion is imperfect: with 80 percent it moves exactly 1, with 10 percent it undershoots (0 cells), with 10 percent it overshoots (2 cells). The new belief is the old belief convolved with this motion model: each cell's mass is spread to where it might have gone.

Python Discrete Bayes filter: sense and move

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

After the first reading the three door cells carry most of the belief, but it is still ambiguous: the robot cannot tell which door it is at. After moving, the whole bump shifts right and flattens a bit, because motion adds uncertainty.

Why combining sense and move disambiguates

A single door reading cannot tell the three doors apart. The map has two adjacent doors (cells 1 and 2) then a long wall, then a door at 7. If the robot senses a door, moves one cell, and senses a door again, only the cell pair (1, 2) matches "door, door." The sequence provides what no single reading can.

Python Localizing with a sequence of readings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The best cell goes from 0.21 to about 0.4 once the second reading agrees with the first (the final peak is lower because the last move spreads the belief). Entropy measures how spread out the belief is; it falls as the robot becomes more certain.

From the discrete filter to the real ones

The discrete Bayes filter is exact but slow for big maps: it stores a number for every cell, and for 3 pose variables on a fine grid that gets heavy. Practical filters are different ways of representing the belief:

Filter Belief representation Best when
Histogram (this lesson) A grid of probabilities Small, discrete state spaces
Kalman filter One Gaussian: mean and covariance Roughly linear motion, single hypothesis
Particle filter A cloud of weighted samples Non-linear, multi-modal belief

The Markov assumption underlies all of them: the current state summarizes the past, so the filter only needs the previous belief, the latest command, and the newest measurement.

A Bayes filter needs two models: a motion model (how commands change the state, with noise) and a sensor model (what readings to expect in each state, with noise). Better models give better beliefs; wrong models, such as being overconfident in the sensor, make the filter confidently wrong.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Bayes filter's belief represent?

    The belief is a probability distribution over states, such as positions, that sums to 1.
  2. 2. In the measurement update, what do you do after multiplying the belief by the sensor likelihood?

    Multiplication produces unnormalized weights. Dividing by their sum yields a valid posterior.
  3. 3. Why does the robot's motion step make the belief flatter?

    Because movement is imperfect, the mass spreads into neighboring cells. Sensing is what sharpens the belief again.
  4. 4. Which filter represents the belief with a set of weighted samples?

    A particle filter uses many weighted random samples to approximate any belief, including multi-modal ones.

Summary

  • Robots keep a belief: a probability distribution over their state.
  • The Bayes filter alternates a predict step (motion, adds uncertainty) and an update step (measurement, reduces it).
  • Bayes' rule multiplies the prior by the likelihood and normalizes.
  • Several readings combined with motion can resolve ambiguity that one reading cannot.
  • Kalman and particle filters are efficient versions of the same idea.

Next: The Kalman Filter for Robots