Lesson 5 of 16 · Conditioning
Conditional Probability
Conditional probability explained: what P(A | B) means, calculating it by counting or with the formula, contingency tables, and why P(A | B) isn't P(B | A).
- 6 min read
- Intermediate
- Reviewed
- Explain what the conditional probability P(A | B) means
- Calculate conditional probabilities by counting and with the formula
- Read conditional probabilities from a table of counts
- Use the general multiplication rule P(A and B) = P(A | B) × P(B)
- Avoid confusing P(A | B) with P(B | A)
Probability with extra information
New information changes probabilities. The chance that a random email is spam might be 20%, but if the email says “You've WON a FREE prize!!!”, your estimate shoots up. Conditional probability is how we update a probability when we learn something.
The idea behind the formula: once we know B happened, every outcome outside B is ruled out. B becomes the new sample space, and we ask what share of it is also in A.
Two ways to calculate it
Roll two dice. What is the chance of a total of 10 or more, given that you rolled doubles?
from itertools import product
space = set(product(range(1, 7), repeat=2))
P = lambda event: len(event) / len(space)
high = {o for o in space if sum(o) >= 10} # total of 10 or more
doubles = {o for o in space if o[0] == o[1]}
# Way 1: shrink the sample space to the doubles, then count
print(f"high totals among the doubles: {sorted(high & doubles)} out of {len(doubles)}")
print(f"P(high | doubles) by counting = {len(high & doubles) / len(doubles):.3f}")
# Way 2: the formula P(A | B) = P(A and B) / P(B)
print(f"P(high | doubles) by formula = {P(high & doubles) / P(doubles):.3f}")
print(f"P(high), knowing nothing = {P(high):.3f}")Both ways give 1/3. Knowing you rolled doubles doubles the chance of a high total, from 1/6 to 1/3. That's another way to see that these events are dependent: for independent events, P(A | B) = P(A), because B tells you nothing about A.
Conditional probability from a table
Real data often arrives as counts. Here are 1,000 emails, labelled by hand:
| contains “free” | no “free” | total | |
|---|---|---|---|
| spam | 120 | 80 | 200 |
| not spam | 280 | 520 | 800 |
| total | 400 | 600 | 1,000 |
To condition on a row or column, look only at that row or column:
- P(free | spam): look at the spam row. 120 of 200 spam emails contain “free”, so 0.6.
- P(spam | free): look at the “free” column. 120 of 400 emails with “free” are spam, so 0.3.
# 1,000 emails, labelled by hand
counts = {
("spam", "free"): 120, # spam emails that contain the word "free"
("spam", "no free"): 80,
("not spam", "free"): 280,
("not spam", "no free"): 520,
}
total = sum(counts.values())
def P(condition):
return sum(n for key, n in counts.items() if condition(key)) / total
p_spam = P(lambda key: key[0] == "spam")
p_free = P(lambda key: key[1] == "free")
p_both = P(lambda key: key == ("spam", "free"))
print(f"P(spam) = {p_spam:.2f}")
print(f"P(free | spam) = {p_both / p_spam:.2f}")
print(f"P(spam | free) = {p_both / p_free:.2f}")The general multiplication rule
Rearranging the formula gives a rule that works for any two events, independent or not:
Common mistakes
Calculate the probability of being late given rain, and given dry weather. Does rain make lateness more likely?
# How 200 commutes went, by weather
counts = {
("rain", "late"): 30,
("rain", "on time"): 20,
("dry", "late"): 15,
("dry", "on time"): 135,
}
# Calculate P(late | rain) and P(late | dry).
# Print both on one line, rounded to 2 decimals.
In a class, 30% of students study French, and 12% study both French and Spanish. A student studies French. What is the probability they also study Spanish?
Summary
- P(A | B) is the probability of A once we know B happened.
- Calculate it by shrinking the sample space to B, or with P(A | B) = P(A and B) / P(B).
- In a table of counts, condition on a row or column by looking only at it.
- General multiplication rule: P(A and B) = P(A | B) × P(B).
- For independent events, P(A | B) = P(A).
- P(A | B) ≠ P(B | A) in general: always check which way round the condition goes.
Next: Bayes' theorem.
Finished reading? Tick it off to track your progress.