Bayes' Theorem
Bayes' theorem reverses a conditional probability. Tests and classifiers tell us ; we want . The theorem connects the two through the base rate of the cause, which intuition ignores far too often.
The formula
For events (a hypothesis) and (evidence):
The denominator is the law of total probability. In words: posterior likelihood prior.
The medical test
A disease affects 1 in 1000 people. A test detects it 99% of the time (sensitivity) and gives a false positive 2% of the time. You test positive. How likely are you to have the disease?
Under 5%. Most positives are false positives because healthy people vastly outnumber sick ones. A table of counts for 100,000 people makes it obvious:
N = 100_000
prior, sens, fpr = 0.001, 0.99, 0.02
sick = N * prior
healthy = N - sick
tp, fn = sick * sens, sick * (1 - sens)
fp, tn = healthy * fpr, healthy * (1 - fpr)
print(f" positive negative")
print(f"sick {tp:10.0f} {fn:10.0f}")
print(f"healthy {fp:10.0f} {tn:10.0f}")
print("P(sick | positive) =", round(tp / (tp + fp), 4))
print("P(healthy | negative) =", round(tn / (tn + fn), 6)) positive negative
sick 99 1
healthy 1998 97902
P(sick | positive) = 0.0472
P(healthy | negative) = 0.99999
Updating step by step
Bayes' theorem can be applied repeatedly: today's posterior is tomorrow's prior. If a second, independent test is also positive, start from :
def update(prior, sens=0.99, fpr=0.02, positive=True):
like_h = sens if positive else 1 - sens
like_not = fpr if positive else 1 - fpr
return like_h * prior / (like_h * prior + like_not * (1 - prior))
p = 0.001
for i, result in enumerate([True, True, True], 1):
p = update(p, positive=result)
print(f"after positive test {i}: P(sick) = {p:.4f}")
print("after a positive then a negative:", round(update(update(0.001), positive=False), 5))after positive test 1: P(sick) = 0.0472
after positive test 2: P(sick) = 0.7104
after positive test 3: P(sick) = 0.9918
after a positive then a negative: 0.00051
Odds form
Bayes is simplest with odds :
The ratio on the right is the likelihood ratio (Bayes factor). For the test it is ; prior odds become posterior odds , matching 4.7%. Independent pieces of evidence multiply their likelihood ratios, which is the idea behind naive Bayes classifiers.
Bayes with a continuous prior
A coin has unknown bias , with a uniform prior on . After heads in tosses, the posterior density is proportional to , a beta distribution (Gamma, Beta and Chi-Squared). Its mean, , is Laplace's rule of succession: after 10 heads in 10 tosses, the probability of heads next time is , not 1.
In software
Spam filters, fraud detection and medical AI all face the base-rate problem: with rare positives, even accurate classifiers produce mostly false alarms, which is why precision and recall are reported separately. Bayesian A/B testing, Bayesian optimization of hyperparameters and probabilistic programming all rest on this one formula.
Exercises
Exercise 1: Rarer disease
Repeat the medical test calculation with prevalence 1 in 10,000.
Answer
About 0.0049, roughly 0.5%.
Solution
.
Exercise 2: Which machine?
Machine A makes 70% of the parts with defect rate 2%; machine B makes 30% with defect rate 5%. A part is defective. What is the probability it came from B?
Answer
.
Solution
.
Exercise 3: Check by simulation (code)
Simulate 1,000,000 people for the original medical test and estimate with NumPy.
Answer
About 0.047.
Solution
import numpy as np
rng = np.random.default_rng(5)
n = 1_000_000
sick = rng.random(n) < 0.001
positive = np.where(sick, rng.random(n) < 0.99, rng.random(n) < 0.02)
print(round(sick[positive].mean(), 4), int(positive.sum()))0.0477 20755
Test yourself
A test with 99% sensitivity and 2% false positive rate is used for a 1-in-1000 disease. A positive result means the disease is…
- 99% likely
- 98% likely
- about 5% likely
- about 50% likely
In odds form, the posterior odds equal…
- prior odds plus likelihood ratio
- prior odds times likelihood ratio
- likelihood ratio alone
- prior odds divided by likelihood ratio
With a uniform prior, after 10 heads in 10 tosses the rule of succession gives =
- 1
- 10/11
- 11/12
- 1/2