Bayes' theorem
Bayes' theorem reverses a conditional probability: P(A | B) = P(B | A) * P(A) / P(B), where P(B) = P(B | A) * P(A) + P(B | not A) * P(not A). It turns the probability of the evidence given a cause into the probability of the cause given the evidence.
Syntax
P(A | B) = P(B | A) * P(A) / [P(B | A) * P(A) + P(B | not A) * P(not A)]| Part | What it is |
|---|---|
P(A) | the prior probability, before seeing the evidence |
P(B | A) | the likelihood: how probable the evidence is if A is true |
P(B | not A) | the false positive rate: how probable the evidence is if A is false |
P(A | B) | the posterior probability, after the evidence |
Examples
Disease prevalence 1%, test sensitivity 90%, false positive rate 5%
P(disease | positive) = 0.9 * 0.01 / (0.9 * 0.01 + 0.05 * 0.99)
= 0.009 / (0.009 + 0.0495) = 0.009 / 0.0585Result: 0.1538, about 15%
Same problem with natural frequencies, 1,000 people:
10 sick -> 9 test positive; 990 healthy -> 49.5 test positive
9 / (9 + 49.5)Result: 0.1538
Spam filter: P(spam) = 0.2, P(word | spam) = 0.5, P(word | not spam) = 0.05
P(spam | word) = 0.1 / (0.1 + 0.04)Result: 0.714
Common mistakes
- Answering with the sensitivity (90%) when asked P(disease | positive); the base rate makes the true answer much lower.
- Leaving out the P(B | not A) * P(not A) term in the denominator; without the false positives the result is wrong.
- Using prevalence from a different population; the prior must match the group the person comes from.
Practice Bayes' theorem
These drills check your answer on live data, free, no account needed.
- Bayes: a positive screening resultSuppose a screening test checks patients for a condition. 0.5% of items are affected. The screen flags 85% of affected items (sensitivity) and correctly clears 99% of the others (specificity). A patient is flagged. What is the probability it is really affected? Round to 4 decimal places.
- Total probability across groupsAn online store ships with three carriers. Carrier A handles 20% of orders, B 17% and C 63%. Their late delivery rates are 12%, 9% and 7%. What is the probability that a randomly chosen order arrives late? Round to 4 decimal places.
Questions
Why is a positive test often wrong for a rare disease?
Because the many healthy people produce more false positives than the few sick people produce true positives. With 1% prevalence and a 5% false positive rate, about 85% of positives are healthy.
What is the easiest way to apply Bayes' theorem?
Imagine 1,000 (or 10,000) people, count how many land in each branch, and divide the true positives by all positives. It is the same arithmetic without the fractions.
What are prior and posterior probabilities?
The prior is your probability before the evidence (the base rate). The posterior is the updated probability after applying Bayes' theorem.
Related
- Conditional probabilityHow to calculate conditional probability P(A|B) = P(A and B) / P(B), read it from a two-way table, check independence and avoid confusing P(A|B) with P(B|A).
- Probability rulesThe basic probability rules with dice and coin examples: the addition rule for A or B, the multiplication rule for independent events and the complement rule.
- Type I and Type II errorsA Type I error rejects a true null (false positive, probability alpha); a Type II error misses a false null (false negative, probability beta). Examples.
- p-valueA p-value is the probability of a result at least as extreme as the observed one if the null is true. How to compute it from z and t, and common misreadings.