Probability and Statistics · Bayes' theorem
Bayes' theorem as updating a prior with evidence, the base-rate trap in medical tests, and sequential updating.
- Prior P(H): what I believed before seeing the evidence.
- Likelihood P(E | H): how expected the evidence is if H is true.
- Posterior P(H | E): the updated belief.
- P(E) comes from the law of total probability (last post).
The base-rate trap. Take a disease with 1% prevalence and a test with 95% sensitivity and 95% specificity. If you test positive, the chance you have the disease is far below 95%. The base rate dominates, because there are so many more healthy people who can produce false positives.
Updating is also sequential: today’s posterior becomes tomorrow’s prior.
def posterior(prior, sens, spec):
p_pos = sens * prior + (1 - spec) * (1 - prior)
return sens * prior / p_pos
p = posterior(prior=0.01, sens=0.95, spec=0.95)
print("P(disease | 1 positive) =", round(p, 4))
p2 = posterior(prior=p, sens=0.95, spec=0.95)
print("P(disease | 2 positives) =", round(p2, 4))
# counting view with 10,000 people
n = 10_000
sick = int(n * 0.01)
tp = round(sick * 0.95)
fp = round((n - sick) * 0.05)
print(f"true positives {tp}, false positives {fp}, share sick among positives {tp / (tp + fp):.4f}")
Output:
P(disease | 1 positive) = 0.161
P(disease | 2 positives) = 0.7848
true positives 95, false positives 495, share sick among positives 0.1610Sources
- MIT 18.05 Reading 3: Bayes' theorem — MIT OpenCourseWare
- Seeing Theory: Bayesian inference — Brown University
- Think Bayes 2 (Allen B. Downey) — Allen B. Downey