Bayes' theorem
Step 13 of 18 in Foundations · Theorem · Easy
P(H|E) = P(E|H)·P(H) / P(E): update a prior belief with evidence. The basis of spam filters, medical diagnosis and Bayesian ML.
The lesson
Bayes' theorem turns P(evidence | hypothesis), which you usually know, into P(hypothesis | evidence), which you usually want: P(H | E) = P(E | H)·P(H) / P(E). The bottom is every way the evidence can arise: P(E) = P(E | H)·P(H) + P(E | not H)·P(not H).
Counting people is easier than juggling the formula.
- Take 10,000 people: 100 are sick, 9,900 are healthy.
- Positive tests: 95 of the sick (95%) and 990 of the healthy (the 10% false positives).
- P(sick | positive) = 95 / (95 + 990) ≈ 8.8%
The answer is low because the healthy outnumber the sick 99 to 1. Even a modest false-positive rate, applied to that many people, swamps the true positives. That is the base-rate effect, and the prior P(H) is what carries it.
Common mistake. A test's sensitivity is P(positive | sick). It is not P(sick | positive). Confusing the two is the single most common error in reading test results, fraud alerts and anomaly detectors.
A spam filter is this theorem applied to the words of an email. Bayesian ML applies it to model weights, with the data as evidence.
Check yourself
Spam is 20% of mail. "Free" appears in 50% of spam and 5% of other mail. What is P(spam | "free")?
Show the answer
0.7143. Spam with free: 0.2 × 0.5 = 0.10. Other mail with free: 0.8 × 0.05 = 0.04. 0.10 / 0.14 ≈ 0.71.
Same test as the example, but the disease now affects 30% of people. P(sick | positive):
- stays at 8.8%
- rises sharply
- falls
Show the answer
rises sharply. The prior changed, so the answer does: 0.3 × 0.95 = 0.285 against 0.7 × 0.10 = 0.07, giving about 80%.
Prevalence 1%. The test catches every sick person and has a 1% false-positive rate. What is P(sick | positive)?
Show the answer
0.5025. Per 10,000: 100 true positives and 99 false ones. 100 / 199 ≈ 0.50. A 99% accurate test, and a positive is a coin flip.
Build it
Medical test
A disease affects 1% of people; a test is 95% sensitive and 90% specific. Compute P(disease | positive) by hand and by simulation.
Done when: Both give about 8.8% and you can explain the base-rate effect.
Runs in your browser: write the code on the map and the tests mark it.
Case study: Base-rate fallacy in medicine
- Most doctors in studies overestimate P(disease | positive test).
- The prior (prevalence) dominates when the condition is rare.
- Same trap appears in fraud and anomaly detection: rare positives mean many false alarms.
Resources
- Wikipedia: Bayes' theorem Article
- Seeing Theory Article
- StatQuest Video
- Think Bayes (free book) Book
Earn this star
On the map, your answers are marked and you log what you built. Finish the step and its star lights.
Open this step on the map