Probability for Robotics · Theory · M3
M3-T1 — Joint, Marginal and Conditional Probability
A robot reasons about two things at once, where it is and what it sensed. Joint, marginal and conditional probabilities are the three ways of reading one table.
Every interesting question in robotics involves at least two uncertain things: the pose and the measurement, the cell and its occupancy, the state now and the state a moment ago. This lesson shows how to describe two uncertain quantities with one table, and the three operations on it that the rest of the course uses constantly: ignoring one variable (marginalizing), learning the value of one (conditioning), and building the table from smaller pieces (the product rule). Two of the filter steps of module 6 turn out to be exactly these operations.
Learning objectives
After this lesson, you should be able to:
- Read a joint probability table, and compute marginal and conditional probabilities from it.
- Derive the definition of conditional probability, the product rule, marginalization and the law of total probability.
- Explain conditioning as “restrict to what was observed, then renormalize”, and recognise that P(A | B) and P(B | A) differ.
- Identify the prediction step of a localizer as the law of total probability.
The intuition
Rover wakes up somewhere in the building and has no idea where. It has a light sensor that reports bright or dim. Two things are uncertain: the room (hall, kitchen or dock) and the light reading. We can describe them together with one table, the joint probability, that gives a probability for every combination.
Three questions can be asked of this one table, and they are the three words of the title.
- Joint: how likely is the combination hall and bright? Read one cell: 0.20.
- Marginal: how likely is the robot to be in the hall at all, whatever the light? Add up the whole column: 0.20 + 0.10 = 0.30. The name comes from writing these totals in the margins of the table. Marginalizing means ignoring a variable by adding over it.
- Conditional: the sensor reads bright; how likely is the hall now? Throw away everything that is not bright (the bottom row is ruled out by the observation), and rescale what remains so that it sums to 1 again. The hall's cell is 0.20 out of a bright total of 0.30, so 0.20 / 0.30 = 0.667.
The last operation is the important one. It is the mathematical form of learning: a robot that sees “bright” is not in a world where the bottom row happened. Its belief about the room shifts from (30%, 30%, 40%) to (67%, 17%, 17%) because the hall explains a bright reading much better than the dock does, and the right-hand bars of the figure show it.
Conditioning is “restrict, then renormalize”. Every filter in this course performs that operation with a different table, and the renormalization is the step that makes probabilities sum to one again.
Be careful with the direction. The probability of a bright reading in the dock is 0.05 / 0.40 = 0.125. The probability of being in the dock after a bright reading is 0.05 / 0.30 = 0.167. The first is a property of the sensor, the second a belief about the robot, and they are different numbers answering different questions. Confusing them is the commonest mistake in probabilistic reasoning, and the next lesson (Bayes' rule) is the bridge between the two.
Definitions
- Joint
- The probability that both and happen: . For two random variables, is a number for every pair of values (a table, or a density over the plane).
- Marginal
- The probability of alone, with the other variable ignored: the row or column total of the joint table.
- Conditional
- The probability of when is known to have happened. Read the bar as “given”. It is defined only when .
- Conditioning variable
- The quantity to the right of the bar, whose value is taken as known. A different variable on the right is a different question.
Two variables are called independent when learning one tells you nothing about the other, . The light table above is not like that: knowing the reading changes the belief about the room. Independence, and its more useful cousin conditional independence, have a lesson of their own (M3-T3: Independence and Conditional Independence).
Derivation: why conditioning divides by P(B)
Conditional probability is often given as a definition. It is better understood as a consequence of a requirement: after learning , the probabilities of everything that is still possible must again sum to 1.
Conditional probability
Imagine a very large number of repeated runs and count them. Among the runs that had (those are the only ones consistent with what we learned) we ask how many also had .
Divide numerator and denominator by . The fractions of all runs are, in the long run, the probabilities (the frequency reading of the first lesson).
The conditional probability is the share of the -world that is also .
Check: conditional probabilities given a fixed obey the axioms, and in particular they sum to 1. That is why the division is needed: the cells alone only sum to .
Product rule, marginalization and total probability
Multiply the definition by . Starting from the other conditional, , gives the same joint with the roles swapped.
If the values of the other variable are mutually exclusive and exhaustive (a partition), splits into the part that goes with each of them.
The pieces are disjoint, so axiom 3 adds them. This is marginalization: the other variable has been summed out.
Replace each joint by the product rule. This is the law of total probability: the probability of is the average of its conditional probabilities, weighted by how likely each condition is.
The chain rule
Apply the product rule with taken together as one event.
Apply it again to . Any number of variables can be peeled off one at a time, in any order.
Conditioning shrinks the table to what was observed and rescales it. Marginalizing adds a variable away. The product rule builds a joint from a marginal and a conditional. Together they are all the machinery needed for Bayesian estimation.
A worked example
The table of the figure is the joint distribution of room and light. Everything below can be read from it, and the program checks each step.
- Marginals. Row totals: , . Column totals: . Each set sums to 1.
- Condition on the observation. Given a bright reading, keep the bright row and divide by 0.30: , and 0.167 for the kitchen and for the dock. These three sum to 1.
- The other direction. How the sensor behaves in each room: , , . Note that they do not sum to 1: they are three different conditional distributions, one per room.
- Total probability. Rebuild from the sensor behaviour and the room probabilities: . It agrees with the row total.
- Independence fails. If the room and the light were independent, would equal . The table says 0.20, more than twice as much. The reading carries information about the room, which is the whole point of a sensor.
from robotprob import Discrete
joint = Discrete({('hall', 'bright'): 0.20, ('kitchen', 'bright'): 0.05, ('dock', 'bright'): 0.05,
('hall', 'dim'): 0.10, ('kitchen', 'dim'): 0.25, ('dock', 'dim'): 0.35})
rooms = ['hall', 'kitchen', 'dock']
def P(event): # probability of an event on the joint table
return joint.prob(event)
p_bright = P(lambda o: o[1] == 'bright') # marginal: add over the rooms
print('P(bright) =', round(p_bright, 2), ' P(dim) =', round(1 - p_bright, 2))
for r in rooms:
print('P(' + r + ') =', round(P(lambda o: o[0] == r), 2))
for r in rooms: # conditional: restrict to bright, then renormalize
print('P(' + r + ' | bright) =', round(P(lambda o: o == (r, 'bright')) / p_bright, 4))
total = 0.0 # law of total probability
for r in rooms:
p_r = P(lambda o: o[0] == r)
p_b_given_r = P(lambda o: o == (r, 'bright')) / p_r
total += p_b_given_r * p_r
print('P(bright | ' + r + ') =', round(p_b_given_r, 4))
print('total probability =', round(total, 2))
print('P(hall) P(bright) =', round(P(lambda o: o[0] == 'hall') * p_bright, 2), ' but P(hall, bright) =', round(P(lambda o: o == ('hall', 'bright')), 2))P(bright) = 0.3 P(dim) = 0.7 P(hall) = 0.3 P(kitchen) = 0.3 P(dock) = 0.4 P(hall | bright) = 0.6667 P(kitchen | bright) = 0.1667 P(dock | bright) = 0.1667 P(bright | hall) = 0.6667 P(bright | kitchen) = 0.1667 P(bright | dock) = 0.125 total probability = 0.3 P(hall) P(bright) = 0.09 but P(hall, bright) = 0.2
Discrete distribution whose outcomes are pairs. Every other number is computed from it.In robotics
The prediction step of a localizer is the law of total probability
Rover believes it is in cell of a corridor with probability , then drives forward and ends up in cell with probability (the motion model). The probability that it is now in cell is : exactly the law of total probability, with the old cell as the variable being summed out. That one line is the whole prediction step of the Bayes filter, and the joint of old and new position is the product rule. M6-T1: The Bayes Filter derives the filter on this basis.
The update step is conditioning
When a measurement arrives, the robot throws away the part of its belief that disagrees with it and renormalizes. It is the “restrict, then renormalize” operation of this lesson, with the restriction softened: poorly matching cells are not ruled out but reduced in proportion to how badly they explain the reading (Bayes' rule, the next lesson).
Conditional probability tables describe detectors
A classifier's confusion matrix is a conditional table, : each row is the output distribution when the truth is one particular class. It is a property of the detector. What the robot wants is the opposite conditional, the probability of each true class given the prediction, and it depends also on how common each class is. Perception pipelines that report only “95% accuracy” hide this distinction; see Uncertainty and Conflicting Observations.
Marginalization removes what you do not care about
A SLAM system estimates the robot's trajectory and the positions of thousands of landmarks together, a huge joint distribution. To report just the robot's current pose it marginalizes the landmarks out. In a Gaussian this is trivial (pick the rows and columns, module 4); in a factor graph it is called marginalization or the Schur complement (M11-T3: Factor Graphs). Dropping old poses from a sliding-window estimator is the same operation.
Safety arguments use total probability
“The probability of a collision” is rarely known directly. It is assembled as over scenarios (person crosses, door opens, sensor fails). Writing the sum explicitly makes the assumed scenario probabilities visible and open to challenge.
Common misconceptions
“P(A | B) and P(B | A) are about the same.”
They can differ enormously, and they answer different questions. In the table above P(bright | dock) = 0.125 but P(dock | bright) = 0.167. For rare events the gap is dramatic: a detector that fires on 99% of real objects can still give a posterior of only a few percent for “object” when objects are rare. The next lesson computes that.
“Conditional probabilities given different things add up to 1.”
Only those given the same thing do. P(hall | bright), P(kitchen | bright) and P(dock | bright) sum to 1; P(bright | hall), P(bright | kitchen) and P(bright | dock) do not (0.667 + 0.167 + 0.125 = 0.958), because they come from different conditional distributions.
“Conditioning on something that did not happen is meaningless.”
It is a way to ask “what if”. Safety and diagnosis rely on it: P(collision | sensor fails) is computed for a failure that has not occurred. The only restriction is P(condition) > 0.
“Marginalizing a variable throws away information.”
It discards the variable, not the evidence about the others. The marginal belief about the room already includes everything the light reading and all other data said about it; what is lost is the ability to say how the room and the light relate.
Key takeaways
- A joint probability describes two uncertain quantities with one table (or one density). Its cells sum to 1.
- Marginalizing means adding over a variable you want to ignore: P(A) = Σ P(A, b). For continuous variables, integrate.
- Conditioning means restricting to what was observed and renormalizing: P(A | B) = P(A, B) / P(B). The division makes the restricted table sum to 1 again.
- The product rule P(A, B) = P(A | B) P(B) builds a joint from pieces; the law of total probability P(A) = Σ P(A | b) P(b) averages conditionals. Together they are the prediction step of a localizer.
- P(A | B) is not P(B | A). One is a property of a sensor or model, the other a belief; relating them is Bayes’ rule.
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.
- J. Tsitsiklis and P. Jaillet, MIT RES.6-012 Introduction to Probability , lecture 2 (conditioning and Bayes’ rule) and lectures 6 and 7 (discrete random variables, joint distributions). Free videos and notes, licence CC BY-NC-SA 4.0.
- J. Blitzstein and J. Hwang, Introduction to Probability, 2nd edition, CRC Press, 2019 , chapters 2 (conditional probability) and 7 (joint distributions). Free to read online; the book is copyrighted.
- C. Piech, Probability for Computer Scientists (Stanford CS109 course reader) , parts 1 and 3 (conditional probability, joint distributions). Free to read online.
- S. Thrun, W. Burgard and D. Fox, Probabilistic Robotics, MIT Press, 2005, section 2.2 (the law of total probability and conditioning). Paid book.
Knowledge check
Three questions. Write an answer, then reveal the explanation. Your answers stay in your browser.
Question 1
A joint table over “door state” (open or closed) and “camera says open or closed” has these cells: open and says-open 0.36, open and says-closed 0.04, closed and says-open 0.06, closed and says-closed 0.54. Compute P(door open), P(says open) and P(door open | says open). Is the camera reading independent of the door state?
Explanation
P(door open) = 0.36 + 0.04 = 0.40. P(says open) = 0.36 + 0.06 = 0.42. P(open | says open) = 0.36 / 0.42 = 0.857. They are not independent: independence would need P(open, says open) = 0.40 × 0.42 = 0.168, but the table gives 0.36. Equivalently, P(open | says open) = 0.857 differs from P(open) = 0.40. The reading is informative, which is what a sensor is for.
Question 2
A navigation stack knows, for each of three scenarios, the probability that the scenario occurs (corridor clear 0.90, person crossing 0.08, door opening 0.02) and the probability of an emergency stop in each (0.01, 0.50, 0.30). What is the overall probability of an emergency stop? Which law did you use, and what happens if the scenario probabilities do not sum to 1?
Explanation
Total probability: P(stop) = 0.90 × 0.01 + 0.08 × 0.50 + 0.02 × 0.30 = 0.009 + 0.040 + 0.006 = 0.055, about 5.5%. The scenarios must form a partition (exclusive and exhaustive) for the law to hold. If the three probabilities sum to less than 1, some scenario is missing and the result underestimates; if they sum to more than 1, scenarios overlap and some stops are counted twice. Also note that the person-crossing scenario contributes 73% of the answer although it is only 8% likely: a small, dangerous scenario dominates.
Question 3
Explain in words why dividing by P(B) is necessary when conditioning, using the light table: what would the numbers 0.20, 0.05 and 0.05 mean if you did not divide, and why is that not a belief?
Explanation
After seeing “bright”, the bright cells of the table (0.20, 0.05, 0.05) are all that remain possible, but together they sum to only 0.30: the rest of the unit of probability sat on outcomes (the dim row) that have just been ruled out. A belief must sum to 1, because the robot is certainly in one of the rooms. Dividing by 0.30 redistributes the whole unit among the cells that are still possible while keeping their ratios, giving 0.667, 0.167 and 0.167. Without the division the numbers would still have the right proportions but would not be probabilities.
What’s next
You can now compute a conditional from a joint table. Robots usually do not have the joint table. They have a prior belief and a model of their sensor, and need the conditional in the other direction. Bayes’ rule is the formula for that.
Related content
Nothing below is required to finish this lesson.
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.