Probability and Statistics · Conditional probability
P(A | B) as shrinking the sample space to B, the multiplication rule, the law of total probability, and the Monty Hall problem by simulation.
P(A | B) = P(A ∩ B) / P(B). Conditioning on B means restricting the world to the outcomes where B happened, then asking how often A happens inside it.
- Multiplication rule: P(A ∩ B) = P(A | B)·P(B).
- Law of total probability: if B₁…Bₖ split S into pieces, then P(A) = Σ P(A | Bᵢ)·P(Bᵢ).
Monty Hall is the standard intuition test. You pick door 1. The host, who knows where the car is, opens a different door that has a goat. Should you switch? Conditioning says switching wins 2/3 of the time. I didn’t trust that until I simulated it.
import numpy as np
rng = np.random.default_rng(1)
n = 100_000
car = rng.integers(0, 3, n)
pick = np.zeros(n, dtype=int)
# host opens a goat door that is not the pick
host = np.empty(n, dtype=int)
for i in range(n):
options = [d for d in (0, 1, 2) if d != pick[i] and d != car[i]]
host[i] = options[rng.integers(len(options))]
switch_to = 3 - pick - host
print("stay wins :", round((pick == car).mean(), 3))
print("switch wins:", round((switch_to == car).mean(), 3))
# law of total probability: P(defect) from two machines
p_m = {"M1": 0.6, "M2": 0.4}
p_def_given = {"M1": 0.02, "M2": 0.05}
print("P(defect) =", sum(p_m[m] * p_def_given[m] for m in p_m))
Output:
stay wins : 0.334
switch wins: 0.666
P(defect) = 0.032Sources
- MIT 18.05 Reading 3: Conditional probability — MIT OpenCourseWare
- Seeing Theory: compound probability — Brown University
- OpenIntro Statistics — OpenIntro