Bayes' Theorem

Bayes' theorem tells you how to update a belief when new evidence arrives. It weighs how likely the evidence is under each explanation against how likely each explanation was to begin with.

The idea

Imagine a disease that 1 in 100 people have, and a test that catches 99% of real cases but also flags 5% of healthy people. You test positive. Are you probably sick? Most people say yes. The real answer is about 17%.

The trick is that healthy people vastly outnumber sick ones. Even a small false-positive rate applied to a huge healthy group produces more positives than the true cases do. Bayes' theorem makes that bookkeeping exact.

How to think about it

  1. Start with the prior: how common is the condition before you look at any evidence?
  2. Ask how likely the evidence is if the condition is true (the sensitivity) and if it is false (the false-positive rate).
  3. Count everyone who would see this evidence: true positives plus false positives.
  4. The posterior is the share of that group who really have the condition.
  5. Got more evidence? Today's posterior becomes tomorrow's prior.

Reading the picture

The grid is 1,000 people. Coral squares are sick, the rest are healthy. Solid squares tested positive. Look only at the solid squares: the fraction that are coral is exactly P(sick∣positive)P(\text{sick} \mid \text{positive}).

Try this

  1. Leave the defaults and read P(sick∣positive)P(\text{sick} \mid \text{positive}). Then raise Prevalence to 20% and watch the same test become far more convincing.
  2. Push Specificity to 99.9%. False positives nearly vanish, and a positive result becomes strong evidence even for a rare disease.
  3. Set Positive tests in a row to 2. Each extra positive multiplies the odds by the likelihood ratio, so belief climbs fast.
  4. Make the disease very rare (0.1%). Even an excellent test gives mostly false alarms. This is why screening programmes confirm with a second test.

Two bags of balls

The same reasoning works without any medicine. A weighted coin secretly picks one of two bags. Bag A has 3 red balls out of 10, bag B has 8. You pull a ball out, look at its colour and put it back. Which bag are you holding?

Before any ball, all you have is the coin: that is the prior. A red ball is more likely to come from bag B, so it shifts belief towards B by the ratio 0.8/0.30.8 / 0.3. A blue ball pushes the other way by 0.7/0.20.7 / 0.2. Every draw is one more application of Bayes' theorem, with the last posterior as the new prior.

Try this

  1. Draw five or six balls and watch the belief bar lurch with each colour, then reveal the bag.
  2. Make the coin very biased towards bag A (P(A)=0.9P(A) = 0.9) and draw a single red ball. The evidence points to B, but the prior is strong enough to keep A in front.
  3. Give both bags the same number of red balls. Now the balls carry no information and the bar never moves away from the coin's bias.
  4. Press Play 1,000 rounds. Counting which bag the red balls really came from lands on the same number the formula gives.

The maths

Bayes' theorem

P(A∣B)=P(B∣A) P(A)P(B)P(A \mid B) = \frac{P(B \mid A)\, P(A)}{P(B)}

Total probability

P(B)=P(B∣A) P(A)+P(B∣¬A) P(¬A)P(B) = P(B \mid A)\, P(A) + P(B \mid \neg A)\, P(\neg A)

Medical test

P(D∣+)=sens⋅prevsens⋅prev+(1−spec)(1−prev)P(D \mid +) = \frac{\text{sens} \cdot \text{prev}}{\text{sens} \cdot \text{prev} + (1 - \text{spec})(1 - \text{prev})}

Odds form

posterior odds=prior odds×likelihood ratio\text{posterior odds} = \text{prior odds} \times \text{likelihood ratio}

Likelihood ratio

LR+=sensitivity1−specificity\mathrm{LR}_{+} = \frac{\text{sensitivity}}{1 - \text{specificity}}