Bayes' theorem

Bayes' theorem turns a conditional probability around:

P(A | B) = P(B | A) × P(A) ÷ P(B)

The medical test puzzle

A disease affects 1% of people. A test finds 99% of sick people (sensitivity), and wrongly says "positive" for 5% of healthy people. You test positive. How likely are you to be sick?

Most people guess about 99%. Count 10,000 people instead:

  • 100 are sick; 99 of them test positive.
  • 9,900 are healthy; 5% of them, 495, also test positive.
  • Positives: 99 + 495 = 594. Sick among them: 99.

P(sick | positive) = 99 ÷ 594 ≈ 0.167: only about 1 in 6.

people = 10000
prevalence, sensitivity, false_alarm = 0.01, 0.99, 0.05

sick = people * prevalence
true_pos = sick * sensitivity
false_pos = (people - sick) * false_alarm
print("true positives:", round(true_pos), " false positives:", round(false_pos))
print("P(sick | positive) =", round(true_pos / (true_pos + false_pos), 3))
Open in compiler

The base rate (how rare the disease is) matters enormously. Because healthy people are so many, their few false alarms outnumber the true positives. Doctors confirm a positive screening with a second, different test for this reason.

Common mistakes

  • Ignoring the base rate (the base rate fallacy).
  • Treating P(positive | sick) and P(sick | positive) as the same number.

Quick check

1. In the example, why is P(sick | positive) so low?
2. If the disease were common (50%), P(sick | positive) would…
3. Bayes' theorem lets you compute…

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.