Probability for Robotics · Theory · M3
M3-T2 — Bayes' Rule: Prior, Likelihood and Posterior
Sensors tell us how likely a reading is if the world is a certain way. Bayes’ rule turns that into how likely the world is, given the reading.
A detector fires. How sure should the robot be that the thing is really there? The answer depends not only on how good the detector is but on how often the thing is there at all, and a good detector can still be wrong most of the time it fires. This lesson derives the rule that combines the two, names its four parts, and explains what each means physically. It is the formula behind every filter in the rest of the course.
Learning objectives
After this lesson, you should be able to:
- Name the four parts of Bayes’ rule (prior, likelihood, evidence, posterior) and say what each means physically.
- Derive Bayes’ rule from the product rule and compute a posterior for a detector with missed detections and false alarms.
- Explain the base-rate effect: why a reliable detector can give a low posterior when its target is rare.
- Explain why a likelihood is not a probability of the hypothesis, and why a zero prior can never be overcome.
The intuition
Rover's camera looks for the dock marker. On a frame where the marker is in view, the detector fires with probability 0.8. On a frame where it is not, it still fires with probability 0.1 (a poster, a reflection). So the detector is right most of the time. Now Rover is driving down a corridor, where the marker is in view on only 5% of frames, and the detector fires. How likely is it that the marker is really there?
Most people say about 80%. The correct answer is about 30%. The reason is easy to see by counting. Imagine 10 000 frames from the corridor. In 500 of them (5%) the marker is in view, and the detector fires on 400 of those. In the other 9 500 frames the marker is not in view, and the detector fires on 10% of them: 950 false alarms. Of the 1 350 times the detector fires, only 400 are real.
This is Bayes' rule in words: the belief after a detection depends on two things, how well the hypothesis explains the detection (a real marker makes it likely, a missing one makes it unlikely) and how plausible the hypothesis was before. A rare event needs strong evidence. The curves show the other side of the same fact: the same detector gives 0.89 where the prior is 0.5 and only 0.07 where it is 0.01.
The four parts, with their physical meaning
| Part | Name | Physical meaning | In the marker example |
|---|---|---|---|
| Prior | What the robot believed about the hypothesis before this reading: from the map, from the previous step, from how common the thing is. | 0.05: the marker is rarely in view | |
| Likelihood | How well the hypothesis explains the reading: if the world really were like , how probable would this reading be? It is a property of the sensor and its noise, read as a function of with the reading fixed. | 0.8 if the marker is present, 0.1 if absent | |
| Evidence | How probable this reading is overall, averaged over every hypothesis. It plays only one role: it rescales the numerator so that the posterior sums to 1. | 0.8 × 0.05 + 0.1 × 0.95 = 0.135 | |
| Posterior | The belief after the reading: what the robot should now think. | 0.04 / 0.135 = 0.296 |
Compare the two directions again. The likelihood answers “what does this sensor do in this world?”, which can be measured by experiment: put a marker in view a thousand times and count. The posterior answers “what is this world, given what the sensor did?”, which is what the robot has to act on, and which cannot be measured directly because the robot does not know the truth. Bayes' rule is the bridge, and the prior is the toll.
Posterior is proportional to prior times likelihood. Multiply how plausible the hypothesis was by how well it explains the reading, then rescale so the beliefs sum to 1.
Definitions
- Hypothesis
- A statement about the world that the robot is trying to decide about: “the marker is present”, “the robot is in cell ”, “this cell is occupied”. With several candidate hypotheses they must be exclusive and exhaustive.
- Data
- What the sensor reported. It is known, and fixed, when Bayes’ rule is applied.
- Prior
- The probability of the hypothesis before the data.
- Likelihood
- The probability (or density) of the data if the hypothesis were true. As a function of it is called the likelihood function; it does not have to sum to 1 over the hypotheses.
- Evidence
- The overall probability of the data, . Also called the normalizer or marginal likelihood.
- Posterior
- The probability of the hypothesis given the data.
Derivation: Bayes' rule and the base-rate effect
The rule itself
The product rule, writing the joint with the hypothesis first.
The product rule again, with the data first. It is the same joint, because a joint does not care in which order its two events are named.
Two expressions for the same number, so they are equal.
Divide by (the data actually happened, so it is not zero). This is Bayes’ rule.
Marginalize the hypothesis (the law of total probability). It shows that the evidence is not a new quantity: it is the numerator summed over every hypothesis, which is why normalizing always works.
Why a rare target gives a low posterior
For a yes-or-no hypothesis with prior , detection rate and false-alarm rate :
Bayes’ rule, with the evidence written out by total probability over and .
Divide the top and bottom by . The result depends only on two ratios: the likelihood ratio (how much more likely the reading is if the hypothesis is true) and the prior odds .
When the prior is small the posterior is about the likelihood ratio times the prior. With a prior of 0.01 becomes about 0.08, never anywhere near 0.8.
A zero prior stays zero whatever the data say, because the posterior is proportional to the prior. A hypothesis assigned zero probability can never be recovered by any amount of evidence.
The second line is the odds form of Bayes' rule, and M3-T4: Recursive Bayesian Updating builds the recursive filter on it. For now note what it says: a detector's usefulness is its likelihood ratio, and a good ratio can only multiply the odds the robot started with.
Posterior odds = likelihood ratio × prior odds. A reading changes belief by a fixed factor, so a rare hypothesis stays improbable after one reading and becomes certain only after several.
A worked example
Rover's marker detector fires on a frame with probability when the marker is in view and when it is not. In the corridor the marker is in view on 5% of frames.
- By counting. Of 10 000 frames, 500 have the marker; the detector fires on 400 of them. Of the other 9 500, it fires on 950. A detection is real in of cases.
- By Bayes' rule. Numerator: . Evidence: . Posterior: . Identical to the count.
- Change the prior. Near the dock, where the marker is in view half the time (), the same detection gives 0.8889. In a long corridor (), 0.0748. The detector has not changed, the robot's situation has.
- Use a no-detection too. If the detector does not fire at , the likelihoods are and , and the posterior falls to . Silence is also evidence.
- A better detector. Replace the single frame by the 3-of-5 vote of the previous module (detection 0.9421, false alarm 0.0086). At a vote that fires gives . Cutting the false-alarm rate by a factor of ten moved the posterior from 0.30 to 0.85.
- Several hypotheses. Recall the three rooms of the last lesson, with prior (0.3, 0.3, 0.4) and the light sensor's probabilities of reading “bright” in each room, (0.667, 0.167, 0.125). The products are (0.20, 0.05, 0.05), their sum 0.30 is the evidence, and the posterior is (0.667, 0.167, 0.167): the same answer the joint table gave. Note that the three likelihoods sum to 0.958, not 1: they are not a probability distribution over the rooms.
from robotprob import posterior_binary, bayes_update
d, f = 0.8, 0.1 # P(fires | marker), P(fires | no marker)
prior = 0.05
evidence = d * prior + f * (1 - prior)
print('evidence P(fires) =', round(evidence, 4))
print('posterior, prior 0.05 =', round(posterior_binary(prior, d, f), 4))
print('posterior, prior 0.5 =', round(posterior_binary(0.5, d, f), 4))
print('posterior, prior 0.01 =', round(posterior_binary(0.01, d, f), 4))
print('no detection, prior 0.05 =', round(posterior_binary(prior, 1 - d, 1 - f), 4))
print('natural frequencies =', 10000 * prior * d, 'real,', 10000 * (1 - prior) * f, 'false')
d3, f3 = 0.9421, 0.0086 # the 3-of-5 vote of the previous module
print('3-of-5 vote, prior 0.05 =', round(posterior_binary(prior, d3, f3), 4))
post = bayes_update([0.3, 0.3, 0.4], [2 / 3, 1 / 6, 0.125]) # three rooms, reading "bright"
print('rooms after bright =', [round(v, 4) for v in post])
print('sum of the likelihoods =', round(2 / 3 + 1 / 6 + 0.125, 4))evidence P(fires) = 0.135 posterior, prior 0.05 = 0.2963 posterior, prior 0.5 = 0.8889 posterior, prior 0.01 = 0.0748 no detection, prior 0.05 = 0.0116 natural frequencies = 400.0 real, 950.0 false 3-of-5 vote, prior 0.05 = 0.8522 rooms after bright = [0.6667, 0.1667, 0.1667] sum of the likelihoods = 0.9583
posterior_binary(prior, p_if_true, p_if_false) is Bayes’ rule for a yes-or-no hypothesis; bayes_update(prior, likelihood) multiplies and normalizes a list of hypotheses.In robotics
Detections must be read against how common the target is
A person detector that fires correctly on 99% of people and falsely on 1% of empty frames looks excellent. In a warehouse aisle where one frame in a thousand contains a person, a detection is real only about 9% of the time (0.99 × 0.001 against 0.01 × 0.999). That is not a reason to ignore it: it is a reason to slow down, look again, and combine several frames, and M3-T4: Recursive Bayesian Updating shows how the posterior then grows quickly. The perception course discusses the same effect for tracks in Uncertainty and Conflicting Observations.
Loop-closure and place recognition
A SLAM system compares each new scan or image with thousands of past ones. Almost every comparison is a non-match, so a match score that is right 99% of the time can still produce mostly wrong loop closures. Real systems follow a similarity hit with a geometric check (a second, independent piece of evidence) before adding a constraint, because one bad closure can bend the whole map (Loop Closure).
Localization: the correction step
For a localizer the hypotheses are the possible poses, the prior is the belief after moving, the likelihood is the sensor model evaluated at the actual reading for each candidate pose, and the posterior is the corrected belief. Every filter in modules 6 to 9 is this one formula, computed on a grid (module 6), in closed form for Gaussians (modules 7 and 8) or with samples (module 9). The SLAM course gives its own introduction in Bayesian Estimation.
Priors come from somewhere, and can be wrong
A prior from a map says “the cell behind the wall is impossible”, and if the map is wrong the filter can never learn the truth. Practical localizers therefore floor the prior at a small positive value, inject a few random particles, or keep a small probability for “none of the above”. Treat a prior of exactly zero or one as a decision never to learn.
Common misconceptions
“The detector is 80% accurate, so a detection means 80% sure.”
The 80% is P(detection | marker), a property of the detector. The robot needs P(marker | detection), which also depends on how often the marker is in view and on the false-alarm rate. At a prior of 5% the answer is 30%. Confusing the two is called the base-rate fallacy.
“The likelihood is the probability of the hypothesis.”
It is the probability of the reading if the hypothesis were true. Across hypotheses it need not sum to 1 (0.8 + 0.1 = 0.9, and 0.2 + 0.9 = 1.1 for the opposite reading), and for a continuous sensor it is a density that can exceed 1. Only after multiplying by the prior and normalizing does it become a probability of the hypothesis.
“A very unlikely hypothesis stays unlikely however much evidence there is.”
Only a prior of exactly zero is permanent. A small prior is overturned by repeated evidence because each reading multiplies the odds by the likelihood ratio; the next lesson shows five frames taking a prior of 0.05 to 0.98.
“The posterior is a kind of average of the prior and the likelihood.”
It is a product, then rescaled. A reading that the hypothesis explains twice as well doubles its odds, whatever the prior was. Averaging would never let strong evidence overturn a strong prior, and multiplication does.
Key takeaways
- Bayes’ rule: posterior = likelihood × prior / evidence. Posterior ∝ prior × likelihood; the evidence only rescales so the beliefs sum to 1.
- The likelihood describes the sensor (how probable this reading is under each hypothesis). The posterior describes the robot’s belief. They are different quantities and differ in direction.
- The base rate matters: a rare target gives a low posterior even for a good detector. Posterior odds = likelihood ratio × prior odds.
- Silence (no detection) is evidence too, and is computed with the complementary likelihoods.
- A prior of zero can never be overturned. Keep a small positive floor wherever the model might be wrong.
References and further reading
Each source is marked free or paid. Free to read does not mean free to reuse: the course resources page lists every source with its licence.
- 3Blue1Brown, “Bayes theorem, the geometry of changing beliefs” . A visual explanation. Free to watch.
- J. Tsitsiklis and P. Jaillet, MIT RES.6-012 Introduction to Probability , lecture 2 (conditioning and Bayes’ rule) and lecture 14 (introduction to Bayesian inference). Free videos and notes, licence CC BY-NC-SA 4.0.
- J. Blitzstein and J. Hwang, Introduction to Probability, 2nd edition, CRC Press, 2019 , chapter 2. Free to read online; the book is copyrighted.
- C. Stachniss, “Bayes Filter” (video lecture) , which applies the rule to robot localization. Free to watch.
- S. Thrun, W. Burgard and D. Fox, Probabilistic Robotics, MIT Press, 2005, section 2.2 (Bayes rule). Paid book.
Knowledge check
Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A loop-closure detector accepts 90% of true loop closures and, wrongly, 2% of false candidates. Among the candidates it examines, 1% are true loops. What fraction of accepted candidates are true loops? What would you do about it?
Explanation
P(true | accepted) = 0.9 × 0.01 / (0.9 × 0.01 + 0.02 × 0.99) = 0.009 / (0.009 + 0.0198) = 0.3125, about 31%. Roughly two accepted closures in three would be wrong, and one wrong closure can distort an entire map. Remedies: add independent evidence before accepting (a geometric consistency check or a second sensor, which multiplies the likelihood ratio), lower the false-accept rate even at some cost in detections, or accept the closure provisionally with a robust loss so that a bad one can be rejected later.
Question 2
A range reading of 2.1 m is compared with two hypotheses about the wall: it is at 2.0 m, or at 2.3 m. The sensor noise is Gaussian with σ = 0.1 m, and the two hypotheses have equal prior probability. The likelihood values are 2.42 per metre and 0.54 per metre. What is the posterior of each hypothesis? Why can the likelihoods be larger than 1, and why does that not matter?
Explanation
The products with the equal prior 0.5 are 1.21 and 0.27, summing to 1.48, so the posteriors are 1.21 / 1.48 = 0.818 for the wall at 2.0 m and 0.182 for 2.3 m (a likelihood ratio of 4.48). The likelihoods are densities in the reading, in units of 1/m, and a narrow density has a high peak; only areas are probabilities. It does not matter because the evidence term divides it out: posteriors depend on the ratio of likelihoods, which has no unit.
Question 3
A grid localizer has set the probability of cell 17 to zero because the stored map shows a wall there. A door has since been installed and Rover is in fact in cell 17. What does the filter do with the readings, and how do you prevent it?
Explanation
The posterior is proportional to the prior times the likelihood, and zero times anything is zero, so the filter keeps cell 17 at probability zero however well the readings match it. The robot never relocalizes and instead the belief piles onto the wrong cells. Prevention: never set a belief exactly to zero (floor it at something like 10⁻⁶ of the maximum), add a small amount of probability back at every step so the belief can recover, or inject random hypotheses (random particles in a particle filter), as the kidnapped-robot test of module 9 requires.
What’s next
Bayes’ rule combines a prior with one reading. To combine several readings, we need to know when it is legitimate to multiply their likelihoods, and that is the question of independence.
Related content
Nothing below is required to finish this lesson.
- BeforeM3-T1: Joint, Marginal and Conditional Probability
Build a joint table for two variables, marginalize it, condition on an observation and use the product rule and the law of total probability.
- LabM3-L1: Bayesian Landmark Detection
A marker detector fires near the charging dock, but it also fires by mistake. Compute how much each detection should change the belief that the robot is at the dock.
Other courses on this site treat neighbouring topics from their own angle:
Get notified when the next lesson and new labs are released.
One email per new lesson or lab.