ENع
←Bayes' theorem
Lesson 18 · Probability and statistics

Bayes' theorem

15 min10 steps5 questionsIntermediate
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.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 18 · Interactive
01 / 10

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
Loading figure…
or ← →
Read the lesson step by step
  1. 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
  2. The 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.8
  3. How 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.6
  4. Normal 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.05
  5. How 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.16
  6. Keep 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"
    16
  7. The 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.75
  8. Bayes' 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.75
  9. The 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.19672131147540983
  10. The 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

Quiz Unlocks after the last step.