Skip to content

Bayes’ theorem

Turn “how likely is the evidence if the cause is real” into “how likely is the cause, given the evidence”, counting people first.

Helps to know firstConditional probability

Helps to know first
This topic

First, the picture

A test finds 99 % of the ill and wrongly flags 5 % of the healthy. You test positive. How likely is it that you are ill?

A positive test, and probably healthy

10 000 people; the test flags 99 % of the ill and 5 % of the healthy.

10 000 people, and 100 of them (1 %) are ill. The test flags 99 % of the ill and 5 % of the healthy.

ill
1 %
ill among positives
–
positive: ill + healthy
–

Predict first. A person tests positive. What is the chance they are ill?

0.00 / 14.00 s
Describe this picture

A grid of 100 by 100 small squares, 10 000 people, of whom 1 % are ill: 100 squares in the accent colour, the rest grey. Before the clip, a question asks: a person tests positive, what is the chance they are ill? Three small bar drawings offer “about 99 %”, “about 50 %” and “about 1 in 6”, and a tap picks one and starts the clip. Then the test sweeps down the grid: 99 of the 100 ill people stay lit, 495 of the 9900 healthy people light up in a second colour as “healthy, flagged”, and everyone else dims. The flagged people gather in a box labelled “tested positive: 594”, the ill first. A bar under the box shows the share of the positives who are ill, “99 ill” against “495 healthy”, one in six, with your guess as a dotted mark labelled “your guess”. Readouts give the share ill, “99 + 495” positives, and the share ill among the positives, 0.167. After the clip a slider, “How common the illness is”, steps through 0.1, 0.2, 0.5, 1, 2, 5, 10, 20 and 50 %, and the grid, the box and the bar follow. Counts are whole people, so at 0.1, 0.2 and 0.5 % the caption also gives the formula’s value.

Count the people first

Before the clip you guessed. Most people pick 99 %, because the test is right 99 % of the time for the ill. The picture says one in six.

Count with it. Of 10 000 people, 100 are ill and 9900 are healthy. The test flags 99 of the ill. It also flags 5 % of the healthy, and 5 % of 9900 is 495. So 594 people are flagged, and only 99 of them are ill:

99594=16=0.167.\frac{99}{594}=\frac16=0.167.

Five in six of the flagged people are healthy. The test is good; the trouble is that the healthy are 99 times as many as the ill, so 5 % of them outnumber 99 % of the ill.

Watch out

How common a condition is before any test, its base rate, matters as much as how good the test is. Leaving it out is a common mistake with tests, and it turns “one in six” into “99 %”.

After the clip, drag how common the illness is. At 10 %, a positive test means ill about 69 times in 100; at 0.1 %, only about 2 times in 100. The rarer the illness, the more the false alarms from the healthy crowd the box.

Counting people like this, whole numbers out of a crowd instead of percentages, is called using natural frequencies. In studies, far more people get these questions right when they are asked in counts than in percentages. That is why this page counts first and writes the formula second.

From the box to Bayes’ formula

Name the events. Let AA be “the person is ill” and BB be “the test flags them”. The test’s makers tell you Pr⁡{B∣A}=0.99\Pr\{B\mid A\}=0.99 and Pr⁡{B∣Ac}=0.05\Pr\{B\mid A^\mathrm{c}\}=0.05. You want the other direction, Pr⁡{A∣B}\Pr\{A\mid B\}.

By the definition of Conditional probability, Pr⁡{A∣B}=Pr⁡{A∩B}/Pr⁡{B}\Pr\{A\mid B\}=\Pr\{A\cap B\}/\Pr\{B\}. The top is the ill people who are flagged; the multiplication rule gives it as

Pr⁡{A∩B}=Pr⁡{B∣A} Pr⁡{A}=0.99×0.01=0.0099,\Pr\{A\cap B\}=\Pr\{B\mid A\}\,\Pr\{A\}=0.99\times0.01=0.0099,

99 people in 10 000. The bottom is everyone in the box, ill or healthy, which the law of total probability adds up:

Pr⁡{B}=Pr⁡{B∣A}Pr⁡{A}+Pr⁡{B∣Ac}Pr⁡{Ac}=0.0099+0.05×0.99=0.0594,\begin{aligned} \Pr\{B\}&=\Pr\{B\mid A\}\Pr\{A\}+\Pr\{B\mid A^\mathrm{c}\}\Pr\{A^\mathrm{c}\}\\ &=0.0099+0.05\times0.99=0.0594, \end{aligned}

594 people in 10 000.

Put the two together and you have Bayes’ theorem:

Pr⁡{A∣B}=Pr⁡{B∣A} Pr⁡{A}Pr⁡{B}=0.00990.0594=16.\Pr\{A\mid B\}=\frac{\Pr\{B\mid A\}\,\Pr\{A\}}{\Pr\{B\}}=\frac{0.0099}{0.0594}=\frac16.

Each piece has a name, because it plays the same part in every use:

  • Pr⁡{A}\Pr\{A\} is the prior: how likely the cause was before the evidence. Here, the base rate, 1 %.
  • Pr⁡{B∣A}\Pr\{B\mid A\} is the likelihood: how likely the evidence is if the cause is real. Here, 99 %.
  • Pr⁡{A∣B}\Pr\{A\mid B\} is the posterior: how likely the cause is after the evidence. Here, one in six.

The theorem turns the likelihood, which the test’s makers can measure, into the posterior, which the person tested wants to know.

Odds

There is a shorter way to do the same sum. The odds of an event compare its chance with the chance against it. Here 1 person in 100 is ill and 99 are not, so the odds of being ill are 1 to 99, written 1:991:99.

Write Bayes’ theorem once for AA and once for AcA^\mathrm{c}, and divide one by the other. Pr⁡{B}\Pr\{B\} cancels, and what is left is

Pr⁡{A∣B}Pr⁡{Ac∣B}=Pr⁡{A}Pr⁡{Ac}×Pr⁡{B∣A}Pr⁡{B∣Ac}.\frac{\Pr\{A\mid B\}}{\Pr\{A^\mathrm{c}\mid B\}}=\frac{\Pr\{A\}}{\Pr\{A^\mathrm{c}\}}\times\frac{\Pr\{B\mid A\}}{\Pr\{B\mid A^\mathrm{c}\}}.

In words: the odds after the evidence are the odds before, times the likelihood ratio, how many times more likely the evidence is if the cause is real than if it is not.

For the test, the likelihood ratio is 0.99/0.05=19.80.99/0.05=19.8. The odds go from 1:991:99 to 19.8:9919.8:99, which is 1:51:5: one ill person for every five healthy ones in the box, a chance of 1/61/6.

The box said the same: 99 ill to 495 healthy is 1:51:5.

Worked example

1. How common the illness is. Keep the same test and change the base rate. Counting 100 000 people makes every count a whole number:

How commonIll and flaggedHealthy and flaggedChance ill if flagged
0.1 %9949950.0194
1 %99049500.1667
10 %990045000.6875
50 %49 50025000.9519

The same test is nearly useless for a rare illness and very good for a common one. When half the people tested are ill, a positive result means ill 95 times in 100.

2. Testing again. You tested positive once; your chance of being ill is now one in six. You take a second, separate test of the same kind, and it is positive too. I assume the two tests make their mistakes separately: given that you are ill, or given that you are healthy, the first result tells nothing about the second.

Then the first posterior is the second test’s prior. The odds go from 1:51:5 to 19.8:5=3.9619.8:5=3.96, a chance of 3.96/4.96=0.7983.96/4.96=0.798. A third positive gives odds 78.478.4, a chance of 0.9870.987.

Watch the same thing as bars for one person.

Test again, and again

One person who tests positive, 1 % of such people ill; the tests are assumed to make their mistakes separately.

Before any test, 1 in 100 people like this one is ill: odds 1 : 99, a chance of 1.0 %.

positive tests
0
chance ill
0.010
odds, ill : healthy
1 : 99
0.00 / 9.00 s
Describe this picture

Stacked bars for one person, the ill part at the bottom and the healthy part above, their heights the chances of each, one bar for each number of positive tests. The clip adds the bars one at a time, for no test, 1, 2 and 3 positives, each growing from the one before with an arrow marked ”× 19.8” between them: the ill part goes from 1 % to 16.7 %, then 79.8 %, then 98.7 %. Readouts give the number of positive tests, the chance ill and the odds, ill to healthy (1 : 99, 1 : 5, 3.96 : 1, 78.4 : 1). The subtitle says the tests are assumed to make their mistakes separately. After the clip a slider, “Positive tests”, sets the number from 0 to 3.

Each positive test multiplies the odds by 19.8. Three in a row turn one in a hundred into nearly certain.

3. A spam filter. Of the mail you receive, 30 % is spam. The word “prize” is in 50 % of spam and in 5 % of other mail. A message contains “prize”. How likely is it to be spam?

Count 1000 messages: 300 are spam and 150 of those contain “prize”; 700 are not, and 35 of those contain it. So 185 messages contain “prize”, and 150 of them are spam:

Pr⁡{spam∣prize}=150185=0.811.\Pr\{\text{spam}\mid\text{prize}\}=\frac{150}{185}=0.811.

Where you’ll meet this

Medical screening. Before a screening programme starts, its planners work out how many of the people it flags will be healthy. For a rare condition it is most of them, so a positive screen leads to a second, more careful test, not straight to treatment.

Spam filters. A filter starts from the share of mail that is spam, the prior, and multiplies its odds by a likelihood ratio for each word it sees, as in the third example, assuming the words make their mistakes separately.

Fault diagnosis and alarms. A warning light on a machine is a test with a base rate: if the fault is rare, most alarms are false. Engineers who set an alarm’s threshold trade missed faults against false alarms.

In signal processing · false alarms in detection

A radar or a matched filter in the signal processing course decides “target present” when its output crosses a threshold. Targets are rare, so even a small chance of a false alarm on noise alone can mean most alarms are false: the same sum as the box. Significance tests here set such thresholds on data.

Reference card

QuantityFormulaNotes
Bayes’ theoremPr⁡{A∣B}=Pr⁡{B∣A}Pr⁡{A}/Pr⁡{B}\Pr\{A\mid B\}=\Pr\{B\mid A\}\Pr\{A\}/\Pr\{B\}posterior from likelihood and prior
Total probabilityPr⁡{B}=Pr⁡{B∣A}Pr⁡{A}+Pr⁡{B∣Ac}Pr⁡{Ac}\Pr\{B\}=\Pr\{B\mid A\}\Pr\{A\}+\Pr\{B\mid A^\mathrm{c}\}\Pr\{A^\mathrm{c}\}everyone in the box
Prior, likelihood, posteriorPr⁡{A}\Pr\{A\}, Pr⁡{B∣A}\Pr\{B\mid A\}, Pr⁡{A∣B}\Pr\{A\mid B\}before, evidence, after
Odds formodds after = odds before × likelihood ratioPr⁡{B}\Pr\{B\} cancels
Likelihood ratioPr⁡{B∣A}/Pr⁡{B∣Ac}\Pr\{B\mid A\}/\Pr\{B\mid A^\mathrm{c}\}19.8 for the test here
Rare illness, good test1 %, 99 %, 5 %: posterior 1/61/6five in six flagged are healthy

Keep reading with a free account.

By creating an account you accept the terms of use.

Sign in

Already have an account? Use the service you signed up with.

Or keep reading: get new lessons by email

Are you 13 or over?

Accounts and email updates are for people aged 13 or over. We keep only your answer, never your date of birth.

End of the lesson

Where to go next.

Phasorium

Account

Sign inKeep your place on any device

Subjects

Site

Look