Skip to the lesson
FixTheRobot Explore Robotics / Probability for Robotics / M3 Conditional Probability and Bayes' Rule / M3-T1

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.

  • Lesson 1 of 4
  • Beginner
  • Module 3: Conditional Probability and Bayes' Rule
  • Theory
  • About 15 minutes to read

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.

A joint probability table as a mosaic, and conditioning as keeping one rowLeft: a rectangle of area one divided into three columns for the rooms hall, kitchen and dock, with widths 0.3, 0.3 and 0.4. Each column is split into a top part for a bright light reading and a bottom part for dim. The six cells hold the joint probabilities 0.20, 0.05 and 0.05 for bright and 0.10, 0.25 and 0.35 for dim. Right: two bars show the belief about the room. Before the reading it is 30, 30 and 40 percent. After seeing bright, only the bright row is kept and rescaled to total one, which gives 67, 17 and 17 percent. HallP = 0.30 0.200.10 KitchenP = 0.30 0.050.25 DockP = 0.40 0.050.35brightdimarea of a cell = joint probability P(room, light) 30%30%40%belief before: P(room)67%17%17%belief after seeing bright: P(room | bright) hallkitchendock
Left: the joint table drawn as a rectangle of area 1. Column widths are the room probabilities and the split in each column is the light reading, so the area of a cell is the probability of that combination. Right: seeing “bright” keeps only the top row, whose cells are then rescaled to sum to 1.

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 P(A,B)P(A, B)
The probability that both AA and BB happen: P(A∩B)P(A \cap B). For two random variables, p(x,y)p(x, y) is a number for every pair of values (a table, or a density over the plane).
Marginal P(A)P(A)
The probability of AA alone, with the other variable ignored: the row or column total of the joint table.
Conditional P(A∣B)P(A \mid B)
The probability of AA when BB is known to have happened. Read the bar as “given”. It is defined only when P(B)>0P(B) > 0.
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.
Conditional probability
P(A∣B)=P(A,B)P(B)(P(B)>0)P(A \mid B) = \frac{P(A, B)}{P(B)} \qquad (P(B) > 0)
The numerator is one cell, the denominator the total of everything still possible. For densities: f(x∣y)=f(x,y)/f(y)f(x \mid y) = f(x, y)/f(y).
Product rule
P(A,B)=P(A∣B) P(B)=P(B∣A) P(A)P(A, B) = P(A \mid B)\, P(B) = P(B \mid A)\, P(A)
A joint probability is a conditional times a marginal, in either order. Robots build their joint models this way: a prior and then a sensor model.
Marginalization and the law of total probability
P(A)=∑bP(A,b)=∑bP(A∣b) P(b)P(A) = \sum_{b} P(A, b) = \sum_{b} P(A \mid b)\, P(b)
The sum runs over every value bb of the other variable. For continuous variables the sum becomes an integral: p(x)=∫p(x∣y) p(y) dyp(x) = \int p(x \mid y)\,p(y)\,dy.
Chain rule
P(A,B,C)=P(A)  P(B∣A)  P(C∣A,B)P(A, B, C) = P(A)\; P(B \mid A)\; P(C \mid A, B)
The product rule applied twice. It writes any joint distribution as a sequence of conditionals, one variable at a time, and is how the robot models of module 5 are assembled.

Two variables are called independent when learning one tells you nothing about the other, P(A∣B)=P(A)P(A \mid B) = P(A). 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 BB, the probabilities of everything that is still possible must again sum to 1.

Conditional probability

  1. runs:N total,  nB with B,  nAB with both A and B\text{runs}: N \text{ total},\; n_B \text{ with } B,\; n_{AB} \text{ with both } A \text{ and } B

    Imagine a very large number of repeated runs and count them. Among the runs that had BB (those are the only ones consistent with what we learned) we ask how many also had AA.

  2. nABnB=nAB/NnB/N\dfrac{n_{AB}}{n_B} = \dfrac{n_{AB}/N}{n_B/N}

    Divide numerator and denominator by NN. The fractions of all runs are, in the long run, the probabilities (the frequency reading of the first lesson).

  3. P(A∣B)=P(A,B)P(B)P(A \mid B) = \dfrac{P(A, B)}{P(B)}

    The conditional probability is the share of the BB-world that is also AA.

  4. ∑aP(a∣B)=∑aP(a,B)P(B)=P(B)P(B)=1\sum_{a} P(a \mid B) = \dfrac{\sum_a P(a, B)}{P(B)} = \dfrac{P(B)}{P(B)} = 1

    Check: conditional probabilities given a fixed BB obey the axioms, and in particular they sum to 1. That is why the division is needed: the BB cells alone only sum to P(B)P(B).

Product rule, marginalization and total probability

  1. P(A,B)=P(A∣B) P(B)P(A, B) = P(A \mid B)\, P(B)

    Multiply the definition by P(B)P(B). Starting from the other conditional, P(B∣A)P(B \mid A), gives the same joint with the roles swapped.

  2. A=(A∩b1)∪(A∩b2)∪…A = (A \cap b_1) \cup (A \cap b_2) \cup \dots

    If the values b1,b2,…b_1, b_2, \dots of the other variable are mutually exclusive and exhaustive (a partition), AA splits into the part that goes with each of them.

  3. P(A)=∑bP(A,b)P(A) = \sum_b P(A, b)

    The pieces are disjoint, so axiom 3 adds them. This is marginalization: the other variable has been summed out.

  4. P(A)=∑bP(A∣b) P(b)P(A) = \sum_b P(A \mid b)\, P(b)

    Replace each joint by the product rule. This is the law of total probability: the probability of AA is the average of its conditional probabilities, weighted by how likely each condition is.

The chain rule

  1. P(A,B,C)=P(C∣A,B) P(A,B)P(A, B, C) = P(C \mid A, B)\, P(A, B)

    Apply the product rule with (A,B)(A, B) taken together as one event.

  2. P(A,B,C)=P(C∣A,B) P(B∣A) P(A)P(A, B, C) = P(C \mid A, B)\, P(B \mid A)\, P(A)

    Apply it again to P(A,B)P(A, B). 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.

  1. Marginals. Row totals: P(bright)=0.20+0.05+0.05=0.30P(\text{bright}) = 0.20 + 0.05 + 0.05 = 0.30, P(dim)=0.70P(\text{dim}) = 0.70. Column totals: P(hall)=0.30,  P(kitchen)=0.30,  P(dock)=0.40P(\text{hall}) = 0.30,\; P(\text{kitchen}) = 0.30,\; P(\text{dock}) = 0.40. Each set sums to 1.
  2. Condition on the observation. Given a bright reading, keep the bright row and divide by 0.30: P(hall∣bright)=0.20/0.30=0.667P(\text{hall} \mid \text{bright}) = 0.20/0.30 = 0.667, and 0.167 for the kitchen and for the dock. These three sum to 1.
  3. The other direction. How the sensor behaves in each room: P(bright∣hall)=0.20/0.30=0.667P(\text{bright} \mid \text{hall}) = 0.20/0.30 = 0.667, P(bright∣kitchen)=0.05/0.30=0.167P(\text{bright} \mid \text{kitchen}) = 0.05/0.30 = 0.167, P(bright∣dock)=0.05/0.40=0.125P(\text{bright} \mid \text{dock}) = 0.05/0.40 = 0.125. Note that they do not sum to 1: they are three different conditional distributions, one per room.
  4. Total probability. Rebuild P(bright)P(\text{bright}) from the sensor behaviour and the room probabilities: 0.667×0.30+0.167×0.30+0.125×0.40=0.20+0.05+0.05=0.300.667 \times 0.30 + 0.167 \times 0.30 + 0.125 \times 0.40 = 0.20 + 0.05 + 0.05 = 0.30. It agrees with the row total.
  5. Independence fails. If the room and the light were independent, P(hall,bright)P(\text{hall}, \text{bright}) would equal P(hall) P(bright)=0.30×0.30=0.09P(\text{hall})\,P(\text{bright}) = 0.30 \times 0.30 = 0.09. 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
The joint table is a 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 ii of a corridor with probability bib_i, then drives forward and ends up in cell jj with probability p(j∣i)p(j \mid i) (the motion model). The probability that it is now in cell jj is ∑ip(j∣i) bi\sum_i p(j \mid i)\, b_i: 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, P(predicted∣true)P(\text{predicted} \mid \text{true}): 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 ∑sP(collision∣s) P(s)\sum_{s} P(\text{collision} \mid s)\,P(s) over scenarios ss (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

  1. A joint probability describes two uncertain quantities with one table (or one density). Its cells sum to 1.
  2. Marginalizing means adding over a variable you want to ignore: P(A) = Σ P(A, b). For continuous variables, integrate.
  3. 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.
  4. 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.
  5. 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.

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.

Next lesson

M3-T2 — Bayes' Rule: Prior, Likelihood and Posterior

Derive Bayes' rule, name its four parts, and compute a posterior for a detector with false positives and missed detections.

Nothing below is required to finish this lesson.

Other courses on this site treat neighbouring topics from their own angle: