Reversing a condition
Lesson 5.1.3 had a bag with a fair coin and a biased coin that lands heads with probability 0.8. One coin is picked at random and tossed, and it shows heads. Which coin was it?
You know the chance of heads for each coin. You want the reverse: the chance of each coin, given heads. The multiplication rule (lesson 5.1.3) writes in two ways:
Divide by , which lesson 5.1.3 found by the law of total probability. Then . Heads moved the biased coin from 0.5 to 0.6154.
The table of counts from lesson 5.1.3 agrees. Of the 650 heads in 1,000 repeats, 400 came from the biased coin, and .
Rule: Bayes' rule
It turns into . The bottom is the law of total probability over "" and "not ". It needs .
Tails works the same way: . Evidence can push a probability either way.
Prior, likelihood and posterior
Bayes' rule has names for its parts.
- The prior : how likely is before the evidence. Here, 0.5 for the biased coin.
- The likelihood : how likely the evidence is if is true. Heads has likelihood 0.8 for the biased coin and 0.5 for the fair one.
- The posterior : how likely is after the evidence. Here, .
In words: multiply each prior by its likelihood, then divide by the total of those products. The total is , which makes the posteriors add up to 1.
The same steps work with more than two cases. A box holds three coins: fair, biased (heads with 0.8) and two-headed. One is picked at random and shows heads.
| coin | prior | likelihood of heads | prior × likelihood | posterior |
|---|---|---|---|---|
| fair | 0.5 | 0.1667 | 0.2174 | |
| biased | 0.8 | 0.2667 | 0.3478 | |
| two-headed | 1 | 0.3333 | 0.4348 | |
| total | 1 | 0.7667 | 1 |
The total, 0.7667, is . Each posterior is its row's product divided by it: , and . With equal priors, the posteriors are in the ratio of the likelihoods.
Try it
bayes takes a list of priors and a list of likelihoods, one per case, and returns the posteriors and their total. The first two lines are the two coins after heads and after tails; the rest is the table above.
The base-rate trap
A condition affects 1% of people: that is its base rate, the prior for someone picked at random. A test is positive for 90% of the people who have it, its hit rate. It is also positive for 5% of the people who do not, its false-alarm rate.
Someone tests positive. Many people guess they have the condition with probability 90%. Bayes' rule says:
About 15%: most positives are false alarms. A table of counts for 10,000 people shows why.
| positive | negative | total | |
|---|---|---|---|
| has it | 90 | 10 | 100 |
| does not | 495 | 9,405 | 9,900 |
| total | 585 | 9,415 | 10,000 |
Only 100 people have the condition, and 90 of them test positive. But 5% of the 9,900 others is 495 false alarms. So 90 of the 585 positives have it: .
The guess of 90% swaps the condition: it is , not . Ignoring the base rate in this way is the base-rate trap. A negative, on the other hand, is very reassuring: .
The same test means different things in different groups. At a 1% base rate, a positive from the 90% and 5% test leaves the condition at 0.1538; a sharper test reaches 0.5.
The rarer the condition, the more the false alarms dominate. At a 0.1% base rate the test gives only 0.0177; at 10% it gives 0.6667. Case study 5.1.6 meets the same effect with an early-warning signal for loan defaults.
Try it
posterior is Bayes' rule with a hit rate and a false-alarm rate. The first lines give the positive and negative cases and the table of counts.
Then a seeded simulation of a million people: each has the condition with probability 0.01, and tests positive with probability 0.9 or 0.05.
In the seeded run, 9,955 people had the condition and 58,466 tested positive. Among the positives, a share 0.1536 had it, against the exact 0.1538.
Updating twice
After a positive, the doctor orders a second test of the same kind. Suppose its result is independent of the first, once you know whether the person has the condition. Then the first posterior, 0.1538, is the prior for the second test:
Two positives give 0.766, and a third would give 0.9833. Doing both tests in one step, with likelihoods and , gives the same 0.766.
The independence matters. If the second test shares the first one's errors, say because a cause of false alarms affects both, it adds much less than this.
The odds form
The odds of are : a probability of 0.01 is odds of , "1 to 99". Odds turn back into a probability . Bayes' rule is shortest in odds.
Rule: Bayes' rule in odds
The likelihood ratio LR says how many times more likely the evidence is if is true than if it is not. For a positive test, it is the hit rate over the false-alarm rate.
For the test, . Prior odds become , a probability of . A second positive multiplies by 18 again: , or .
When evidence cannot move a probability
- LR = 1: a test that is positive as often with the condition as without it tells you nothing. The posterior equals the prior.
- No false alarms: if , a positive rules out "not ", and the posterior is 1.
- A prior of 0 or 1 never moves: . Give a prior of 0 only to what is truly impossible.
Try it
Two positives, by updating twice and in one step, then the odds form. The last line runs the three cases above: equal rates, no false alarms, and a prior of 0.
Common mistakes
- Swapping the condition. is the hit rate; is what a patient wants.
- Ignoring the base rate. The same positive means 0.0177 at a 0.1% base rate and 0.6667 at 10%.
- Leaving out the false alarms. The bottom of Bayes' rule adds every way the evidence can happen: , not just .
- Hit and false-alarm rates need not add up to 1. They describe different groups: 0.9 and 0.05 here.
- Updating twice on shared errors. Two tests multiply their likelihood ratios only if their results are independent given the truth.
- A prior of 0 cannot be updated. No evidence moves it, so keep small but non-zero priors for rare things.
Bayes' rule
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- Bayes' rule: , with .
- Posterior = prior × likelihood, divided by the total over all cases. With equal priors, the posteriors follow the likelihoods.
- A good test for a rare condition gives mostly false alarms: 90% hit, 5% false alarm and a 1% base rate give . Check with a table of counts.
- Odds form: posterior odds = likelihood ratio × prior odds. Independent tests multiply their likelihood ratios.
- A likelihood ratio of 1 changes nothing; a prior of 0 or 1 never changes.