Conditional Probability
New information changes probabilities. If you know a die roll was even, the chance it is a 6 rises from to . Conditional probability formalises this update: restrict attention to the outcomes consistent with what you know, and renormalise.
Definition
For events and with , the probability of given is
becomes the new sample space; within it, outcomes keep their relative weights. For equally likely outcomes, .
from fractions import Fraction
from itertools import product
omega = list(product(range(1, 7), repeat=2))
def P(event, given=lambda w: True):
F = [w for w in omega if given(w)]
return Fraction(sum(1 for w in F if event(w)), len(F))
sum8 = lambda w: w[0] + w[1] == 8
first_is_3 = lambda w: w[0] == 3
double = lambda w: w[0] == w[1]
print("P(sum 8) =", P(sum8))
print("P(sum 8 | first die 3) =", P(sum8, first_is_3))
print("P(sum 8 | double) =", P(sum8, double))
print("P(double | sum 8) =", P(double, sum8))P(sum 8) = 5/36
P(sum 8 | first die 3) = 1/6
P(sum 8 | double) = 1/6
P(double | sum 8) = 1/5
Note that and are different numbers: but . Confusing the two is the most common error in probability (see Paradoxes).
The multiplication rule and trees
Rearranging the definition gives , and for a chain of events
This is the logic of tree diagrams: each branch is labelled with a conditional probability, and the probability of a path is the product along it. Drawing two cards without replacement, .
The law of total probability
If split the sample space into disjoint cases, then
Example: 60% of a company's servers run in region A, where a request fails with probability 0.01, and 40% in region B, where it fails with probability 0.03. Overall failure probability: .
import random
rng = random.Random(2)
n = 200_000
fails_total = fails_B = in_B = 0
for _ in range(n):
region_B = rng.random() < 0.4
fail = rng.random() < (0.03 if region_B else 0.01)
fails_total += fail
in_B += region_B
fails_B += fail and region_B
print("P(fail) ~", round(fails_total / n, 4), " exact 0.018")
print("P(region B | fail) ~", round(fails_B / fails_total, 3), " exact", round(0.012 / 0.018, 3))P(fail) ~ 0.0179 exact 0.018
P(region B | fail) ~ 0.675 exact 0.667
The second line runs the logic backwards: given a failure, which region was it? That reversal is Bayes' theorem.
In software
Conditional probabilities are what dashboards actually show when they filter: "error rate for mobile users" is . Recommendation systems estimate , and language models compute (Sampling from Language Models).
Exercises
Exercise 1: Two children
A family has two children, and at least one is a girl. What is the probability that both are girls (each child independently a girl with probability 1/2)?
Answer
.
Solution
The equally likely cases GG, GB, BG, BB; "at least one girl" leaves GG, GB, BG, of which one is GG.
Exercise 2: Cards in a row
Three cards are drawn without replacement. What is the probability that all three are hearts?
Answer
.
Solution
Multiplication rule along one branch of the tree: each factor is the probability of a heart given the previous cards were hearts.
Exercise 3: Total probability (code)
Urn 1 has 3 red and 2 blue balls, urn 2 has 1 red and 4 blue. Pick an urn by a fair coin, then a ball. Find exactly and by simulation.
Answer
.
Solution
import random
rng = random.Random(3)
urns = [["r"] * 3 + ["b"] * 2, ["r"] + ["b"] * 4]
n = 100_000
red = sum(rng.choice(urns[rng.randint(0, 1)]) == "r" for _ in range(n))
print(red / n, 2 / 5)0.39987 0.4
Test yourself
What is by definition?
If , then is…
- 0.9
- 0.1
- not determined without more information
- 1