Bayes' theorem
About this lesson
Bayes' theorem tells you how to change your mind when new evidence arrives. Follow a spam filter that sees the word "free": lay 100 emails out as squares, count the spam and the emails that say "free", keep only the ones that fit the clue, and watch the chance of spam jump from 0.2 to 0.75. Then get the same answer from the formula, see why a rare cause stays unlikely, and write the rule as a Python function.
A word in an email
A spam filter sees the word "free" in an email. How sure can it be that the email is spam? Bayes' theorem answers questions like this. To see it, picture 100 emails as 100 squares.
>>> emails = 10 * 10 >>> emails 100
Read the lesson step by step
A word in an email
A spam filter sees the word "free" in an email. How sure can it be that the email is spam? Bayes' theorem answers questions like this. To see it, picture 100 emails as 100 squares.
>>> emails = 10 * 10 >>> emails 100The prior
Suppose 20 of the 100 emails are spam. Before reading any words, the chance an email is spam is 20/100 = 0.2. This starting belief is called the prior, written P(spam).
>>> prior = 20 / 100 # P(spam) >>> prior 0.2 >>> 1 - prior # P(ham), not spam 0.8How often spam says "free"
Suppose 60% of spam says "free": 12 of the 20 spam emails get a ring. This is the likelihood P(free | spam). The bar | means "given": the chance of "free", given that the email is spam.
>>> like = 12 / 20 # P(free | spam) >>> like 0.6Normal mail says it too
Normal email, called ham, says "free" too, just less often. Suppose 5% of the 80 ham emails do: 4 more rings. These are false alarms: P(free | ham) = 4/80 = 0.05.
>>> alarm = 4 / 80 # P(free | ham) >>> alarm 0.05How common is "free"?
How often does "free" appear at all? Add both groups: 12 spam plus 4 ham is 16 of 100 emails, so P(free) = 0.16. Adding up over every possible cause like this is the law of total probability.
>>> p_free = like * prior + alarm * (1 - prior) >>> p_free # 0.6 × 0.2 + 0.05 × 0.8 0.16Keep only what fits
Now use the clue: this email says "free". So it must be one of the 16 ringed emails, and the other 84 drop out. Conditioning on evidence means shrinking the world to the outcomes that fit it.
>>> 12 + 4 # emails that say "free" 16The posterior
Among the 16 emails left, 12 are spam. So P(spam | free) = 12/16 = 0.75. Seeing "free" raised the chance of spam from 0.2 to 0.75. This updated belief is called the posterior.
>>> 12 / 16 # spam among the emails left 0.75Bayes' theorem
Bayes' theorem does the same sum with probabilities: P(spam | free) = P(free | spam) × P(spam) ÷ P(free). Put in the numbers: 0.6 × 0.2 ÷ 0.16 = 0.12 ÷ 0.16 = 0.75. Same answer as counting.
>>> like * prior / p_free 0.75The prior matters
Now suppose only 2% of mail is spam. Then P(free) = 0.6 × 0.02 + 0.05 × 0.98 = 0.061, and P(spam | free) = 0.012 ÷ 0.061 ≈ 0.20. When the cause is rare, it stays fairly unlikely even after good evidence.
>>> 0.6 * 0.02 / (0.6 * 0.02 + 0.05 * 0.98) 0.19672131147540983The rule in Python
The whole rule fits in a short Python function: multiply the likelihood by the prior, then divide by the total chance of the evidence. Naive Bayes spam filters make an update like this for every word in an email.
>>> def bayes(prior, like, alarm): ... hit = like * prior ... miss = alarm * (1 - prior) ... return hit / (hit + miss) ... >>> bayes(0.2, 0.6, 0.05) 0.75 >>> bayes(0.02, 0.6, 0.05) 0.19672131147540983
5 quick questions
Choose one answer per question. You can change your choices before checking them.
011% of people have a disease. A test finds 90% of people who have it, but also gives a positive result for 9% of people who do not. You test positive. About how likely is it that you have the disease?
Out of 1,000 people, 10 have it and 9 of them test positive; 990 do not and about 89 of them test positive. Only 9 of the roughly 98 positives are ill: about 9%. The low prior (1%) keeps the posterior low.
02In Bayes' theorem, what is the prior?
The prior P(H) is the starting belief. P(E | H) is the likelihood, P(E) the evidence, and P(H | E), the belief after the evidence, is the posterior.
0360% of spam says "free". Which statement follows?
"60% of spam says free" is the chance of free given spam. The reverse, P(spam | free), needs the prior and the false-alarm rate too; mixing the two up is a common mistake.
04P(A) = 0.4, P(B | A) = 0.5 and P(B | not A) = 0.25. What is P(B)?
By the law of total probability: P(B) = 0.5 × 0.4 + 0.25 × 0.6 = 0.2 + 0.15 = 0.35.
05A spam filter keeps the same likelihood and false-alarm rate, but spam becomes much more common in the inbox. What happens to P(spam | free)?
The posterior grows with the prior. In the lesson, a prior of 0.02 gives about 0.20 and a prior of 0.2 gives 0.75, with the same 0.6 and 0.05.
Glossary
- Conditional probability
- P(A | B), read "the probability of A given B": the chance of A once you know B happened. It equals P(A and B) ÷ P(B).
- Prior
- Your belief in a cause before seeing the evidence, such as P(spam) = 0.2.
- Likelihood
- How likely the evidence is if the cause is true, such as P(free | spam) = 0.6.
- Evidence
- The total chance of seeing the clue, over every possible cause, such as P(free) = 0.16.
- Posterior
- Your updated belief in the cause after seeing the evidence, such as P(spam | free) = 0.75.
- Bayes' theorem
- P(H | E) = P(E | H) × P(H) ÷ P(E): how to turn a likelihood and a prior into a posterior.
- Law of total probability
- P(E) = P(E | H) × P(H) + P(E | not H) × P(not H): add up the chance of the evidence over every cause.
- Base rate
- How common a cause is to begin with. A low base rate keeps the posterior low, even after good evidence.
Notes and sources
- The rates in this example (20% spam, 60% of spam and 5% of normal mail saying "free") are round numbers chosen to make the counting easy. Real rates depend on the mailbox.
- P(free | spam) = 0.6 and P(spam | free) = 0.75 are different numbers. Mixing up P(A | B) with P(B | A) is a common mistake, sometimes called confusing the inverse.
- The law of total probability: P(free) = P(free | spam) × P(spam) + P(free | ham) × P(ham) = 0.6 × 0.2 + 0.05 × 0.8 = 0.12 + 0.04 = 0.16.
- Counting emails instead of multiplying percentages is called using natural frequencies. Gigerenzer and Hoffrage (1995) found that people solve Bayes problems far more often when they are written this way.
- The 2% case in counts: out of 1,000 emails, 20 are spam and 12 of them say "free"; 980 are ham and 49 of them say "free". So 12 of the 61 emails that say "free" are spam: 12/61 ≈ 0.197.
- Python floats are rounded binary fractions. 0.6 * 0.2 / 0.16 happens to print 0.75 exactly, but 0.05 * 0.8 prints 0.04000000000000001.
- The Python lines in the steps build on each other: run them in order in one session.
- MIT OCW 18.05 Introduction to Probability and Statistics (Orloff & Bloom), class 3: conditional probability, independence and Bayes' theorem
- Grinstead & Snell, Introduction to Probability (AMS), section 4.1 Discrete Conditional Probability
- Lehman, Leighton & Meyer, Mathematics for Computer Science (MIT 6.042J), chapter 18 Conditional Probability
- Gigerenzer & Hoffrage (1995), How to improve Bayesian reasoning without instruction: frequency formats, Psychological Review
- Jurafsky & Martin, Speech and Language Processing (3rd ed. draft), Appendix B: Naive Bayes Classification
- Python tutorial: floating-point arithmetic, issues and limitations
Quiz Unlocks after the last step.