Chapter 2: Conditional probability and Bayes’ rule
Previous: PS 01 Sample Spaces Events and Axioms | PS 00 Course Map | Next: PS 03 Discrete Random Variables
Learning something new shrinks the set of possibilities. Conditional probability is probability recomputed inside that smaller world. Bayes’ rule lets you turn “how likely is the evidence if the hypothesis is true?” into “how likely is the hypothesis, now that I have seen the evidence?”. It is the engine of every system that updates its beliefs from data, from spam filters to robot localization.
- Compute conditional probabilities from the definition and from tables.
- Use the multiplication rule and probability trees for multi-step experiments.
- Decide whether two events are independent.
- Use the law of total probability to split a problem into cases.
- Apply Bayes’ rule, and avoid base-rate neglect and the prosecutor’s fallacy.
- Update beliefs repeatedly as evidence arrives.
You need events, the axioms and counting from PS 01 Sample Spaces Events and Axioms.
2.1 Conditional probability
The idea: shrinking the world
You roll a die. Before looking, . Now a friend peeks and says “it’s even”. The possible outcomes are now just , all still equally likely. So the chance it is 2 becomes .
What happened: the new information removed the outcomes 1, 3, 5. We recomputed the probability inside the remaining set.
The definition
For events and with ,
Read: “the probability of given ”.
Why this formula. Knowing happened, only the part of that lies inside can still happen: that is . Then we rescale by so that the new world has total probability 1 (axiom 2 in the new world).
In the sand picture: throw away all the sand outside , then scale up what remains so it is one unit again.
Die example: , , : ✓.
Worked example 2.1: from a table
A survey of 200 robot test runs:
| success | failure | total | |
|---|---|---|---|
| bright lighting | 90 | 10 | 100 |
| dim lighting | 50 | 50 | 100 |
| total | 140 | 60 | 200 |
- .
- : restrict to the bright row.
- .
- : restrict to the failure column.
Conditioning means choosing a row or column of the table and working only inside it.
but . These answer different questions. Confusing with is the single most common probability error.
- Two dice. Given that the sum is 8, find the probability that the first die shows 3.
- From the table, find .
Answers. 1. Sum 8: . One has first die 3: . 2. .
2.2 The multiplication rule and trees
The rule
Rearrange the definition:
For a chain of events:
In words: the probability that several things all happen is the probability of the first, times the probability of the second given the first, and so on.
Worked example 2.2: drawing without replacement
Draw two cards from a deck without putting the first back. ?
After one ace is drawn, 3 aces remain among 51 cards. The second probability is conditional on the first draw.
Probability trees
For multi-stage experiments, draw a tree. Each branch is labelled with a conditional probability. Multiply along a path to get the probability of that path. Add across paths to combine outcomes.
Worked example 2.3. A bag has 3 red and 2 blue balls. Draw two without replacement.
R2 2/4 → P(R,R) = 3/5 × 2/4 = 6/20
R1 3/5 <
/ B2 2/4 → P(R,B) = 3/5 × 2/4 = 6/20
start <
\ R2 3/4 → P(B,R) = 2/5 × 3/4 = 6/20
B1 2/5 <
B2 1/4 → P(B,B) = 2/5 × 1/4 = 2/20
The four paths sum to ✓. .
2.3 Independence
The definition
Two events are independent when knowing one tells you nothing about the other.
and are independent if
Equivalently (when ): .
The product form is preferred as the definition because it is symmetric and works even when a probability is zero.
Examples.
- Two coin flips: . Independent.
- Two cards without replacement: not independent (the first draw changes the deck).
- In the lighting table: but . Not independent: lighting matters.
Checking independence
Worked example 2.4. Roll one die. (even), (at most 4).
, , so . And ✓. Independent, even though both events are about the same die. Knowing the roll is at most 4 leaves even and odd equally likely (2 of each).
If and are disjoint with positive probabilities, then . They are strongly dependent: if one happens, the other cannot.
Independence of several events
Events are (mutually) independent if the product rule holds for every sub-collection. Then
This is what makes “5 independent tests each passing with probability 0.9” give .
In real data it is usually an approximation, and often a bad one. Consecutive video frames, repeated trials with the same object, or measurements from one sensor over time are typically correlated. Check the assumption before relying on a calculation that uses it.
- , , . Independent?
- If and are independent, are and independent?
Answers. 1. . Yes. 2. Yes: .
2.4 The law of total probability
The idea: split into cases
Often is hard to find directly, but easy once you know which “case” you are in. Split the world into cases that are disjoint and cover everything (a partition), then average.
In words: the overall probability is a weighted average of the probability in each case, weighted by how likely each case is.
Why: is the disjoint union of the pieces , and by the multiplication rule.
Worked example 2.5: two machines
A factory has two machines. Machine 1 makes 60% of parts, with a 2% defect rate. Machine 2 makes 40%, with a 5% defect rate.
The overall rate, 3.2%, lies between 2% and 5%, closer to 2% because machine 1 makes more parts.
2.5 Bayes’ rule
Turning a conditional around
Often you know (how a cause produces effects) but want (how to infer the cause from what you see). Write the multiplication rule both ways and divide:
where the denominator usually comes from the law of total probability:
The names
| Term | Name | Meaning |
|---|---|---|
| prior | your belief before seeing the evidence | |
| likelihood | how well predicts the evidence | |
| evidence | the total probability of what you saw | |
| posterior | your belief after seeing the evidence |
Worked example 2.6: the factory, reversed
A part is defective. Which machine made it?
Machine 2 makes only 40% of the parts but 62.5% of the defects.
2.6 The medical test: base rates matter
The problem
A disease affects 1% of people. A test detects it 95% of the time when present (sensitivity), and gives a false positive for 5% of healthy people. You test positive. What is the probability you have the disease?
Most people (including many doctors, in studies) answer about 95%. Let us compute.
The calculation
- Prior: , so .
- Likelihoods: , .
- Evidence: .
- Posterior:
About 16%. Not 95%.
Why: count people
When a Bayes problem is confusing, imagine a concrete number of cases and count.
Take 10,000 people.
- 100 have the disease. The test catches 95 of them.
- 9,900 are healthy. The test wrongly flags 5% of them: 495.
- Total positives: . Of these, only 95 are ill.
The disease is rare, so the healthy group is enormous, and even a small false-positive rate applied to an enormous group produces more false alarms than there are real cases. Ignoring the prior (the 1%) is called base-rate neglect.
A second test
You take a second, independent test, and it is also positive. Now your prior is yesterday’s posterior, 0.161:
Bayes’ rule can be applied repeatedly, each time using the latest belief as the prior. This recursive updating is how robots track their position, how spam filters learn, and how the Kalman filter works (04 Perception and State).
2.7 Two traps
is not
, but .
In court this is the prosecutor’s fallacy: “If the defendant were innocent, there would be only a 1 in a million chance of this DNA match. So there is a 1 in a million chance the defendant is innocent.” The first number is ; the conclusion claims . In a city of 10 million people, about 10 innocent people would also match.
Conditional independence
Two events can be dependent overall but independent once you know a third thing.
and are conditionally independent given if
Example. Two thermometers in the same room. Their readings are correlated (both rise when the room warms up). But given the true temperature, their errors are unrelated: knowing one thermometer’s error tells you nothing about the other’s. The readings are conditionally independent given the temperature.
This is exactly the assumption behind combining multiple sensors, and behind the Markov property in PS 12 Markov Chains and Information.
Summary
| Idea | Formula |
|---|---|
| conditional probability | |
| multiplication rule | |
| independence | |
| total probability | |
| Bayes’ rule |
Three facts to remember:
- Conditioning = restrict to the known part, then rescale.
- . Bayes’ rule converts one into the other.
- Rare hypotheses stay unlikely after weak evidence: always include the prior.
Exercises
Part A: warm-up
A1. , , . Find and . Are and independent?
A2. Two cards are drawn without replacement. Find .
A3. A die is rolled. Given that the result is greater than 3, what is the probability it is even?
A4. A box has 4 good and 2 faulty fuses. Two are drawn without replacement. Find and using a tree.
A1. , . : not independent.
A2. .
A3. Given , the even ones are : .
A4. . Exactly one faulty: .
Part B: core
B1. 20% of email is spam. The word “prize” appears in 30% of spam and 1% of legitimate email. An email contains “prize”. What is the probability it is spam?
B2. A robot’s grasp detector reports “success”. Grasps actually succeed 70% of the time. The detector reports success for 90% of real successes and 20% of real failures. Find and .
B3. In the medical test of section 2.6, how high would the prior need to be for a single positive test to give at least a 50% chance of disease?
B4. Three machines make 50%, 30% and 20% of a product, with defect rates 1%, 2% and 4%. A defective item is found. Which machine most likely made it, and with what probability?
B5. A robot localizes itself in a corridor with 3 doors among 10 equally likely positions. Its door sensor says “door” with probability 0.8 at a door and 0.1 elsewhere. It senses “door”. What is the probability it is actually at a door? It then moves one position (assume, for simplicity, that it is equally likely to arrive at a door or not as before) and senses “door” again. Update again.
B1. .
B2. . . ; real failure and reported failure: ; so .
B3. Need , so . A prior of 5% or more.
B4. . Posteriors: , , . Machine 3, with probability about 42%, despite making the fewest items.
B5. Prior . . Posterior . Second update using as prior: . (A real robot would also model how the motion changes its position; this is the idea behind particle filters.)
Part C: explain and prove
C1. Prove that if and are independent, then so are and .
C2. Show that . Interpret.
C3. Explain, using the 10,000-people method, why a test with 99% sensitivity and 99% specificity is still unreliable for a disease with prevalence 1 in 10,000.
C4. Give an example of two events that are dependent but conditionally independent given a third event.
C1. .
C2. . Conditioning on produces a genuine probability distribution in the world .
C3. Take 1,000,000 people: 100 ill, 99 test positive. 999,900 healthy, 1% = 9,999 false positives. Positive predictive value .
C4. Two thermometers in one room (section 2.7), or two children’s test results given their shared teacher’s quality, or two coin flips from a coin that might be biased: the flips are dependent (both tell you about the bias) but independent once the bias is known.
Part D: practise with code
D1. Simulate the medical test for 1,000,000 people with NumPy. Estimate and compare with .
D2. Write bayes_update(prior, p_e_given_h, p_e_given_not_h) and apply it 5 times in a row for the test. Plot the posterior after each positive result.
D3. Simulate drawing two cards without replacement 100,000 times and estimate . Compare with .
D1. ill = rng.random(N) < 0.01; pos = np.where(ill, rng.random(N) < 0.95, rng.random(N) < 0.05); then ill[pos].mean().
D2. The posterior climbs . Independent repeated evidence quickly overwhelms a small prior.
Where this is used
- State estimation: prior belief about the state, likelihood of the sensor reading, posterior belief, repeated every time step (04 Perception and State).
- Policies: a policy is a conditional distribution of actions given observations (06 Imitation and Distribution Shift).
- Evaluation: “success given this lighting condition” is a conditional probability, and comparing conditions is how you find failure modes (16 Evaluation and Uncertainty).
Next: PS 03 Discrete Random Variables: attaching numbers to outcomes.