Sub topic 2
Bayes' theorem: priors, likelihoods, and posteriors
This chapter assumes you know events, joint probability, and conditional probability P(A | B), and that you have seen supervised classification with features and labels. We use Bayes' theorem to turn "how likely is this evidence under each class?" into "how likely is each class given this evidence?"
From conditional probability to Bayes' theorem
The joint probability of two events can be written in two ways:
- P(A, B) = P(A | B) · P(B)
- P(A, B) = P(B | A) · P(A)
Setting these equal and dividing by P(B) gives Bayes' theorem:
P(A | B) = P(B | A) · P(A) / P(B)
For classification, let y be the class and x be the observed features:
P(y | x) = P(x | y) · P(y) / P(x)
The four terms
| Term | Symbol | Meaning | Column |
|---|---|---|---|
| Prior | P(y) | How common the class is before looking at the features | |
| Likelihood | P(x \ | y) | How probable these features are if the class is y |
| Evidence | P(x) | How probable these features are across all classes | |
| Posterior | P(y \ | x) | How probable the class is after seeing the features |
The evidence is the sum over all classes: P(x) = Σ P(x | y) · P(y). It is the same for every class, so it only rescales the posteriors so they add up to 1.
A worked example
Suppose 20% of emails are spam. The word "free" appears in 60% of spam and 5% of non-spam (ham). What is P(spam | "free")?
- Prior: P(spam) = 0.2 and P(ham) = 0.8.
- Likelihoods: P("free" | spam) = 0.6 and P("free" | ham) = 0.05.
- Evidence: P("free") = 0.6 · 0.2 + 0.05 · 0.8 = 0.12 + 0.04 = 0.16.
- Posterior: P(spam | "free") = 0.12 / 0.16 = 0.75.
Seeing "free" moved the probability of spam from 0.2 to 0.75.
p_spam, p_ham = 0.2, 0.8
like_spam, like_ham = 0.6, 0.05
joint_spam = like_spam * p_spam
joint_ham = like_ham * p_ham
evidence = joint_spam + joint_ham
print("P(spam | free) =", joint_spam / evidence)
print("P(ham | free) =", joint_ham / evidence)This prints 0.75 and 0.25 (up to floating-point rounding).
Why the evidence can be dropped
To pick the most likely class, you only compare posteriors. Because P(x) is identical for all classes:
P(y | x) ∝ P(x | y) · P(y)
Here ∝ means "proportional to". In the example, the unnormalized scores 0.12 and 0.04 give the same winner as 0.75 and 0.25. Later chapters use this shortcut, and it is the basis of the naive Bayes decision rule.
Common errors
- Confusing P(x | y) with P(y | x). A word that is common in spam does not make most messages containing it spam. The prior matters.
- Ignoring the prior. A strong likelihood for a rare class can still lose to a common class.
- Treating unnormalized scores as probabilities. They sum to less than 1 until you divide by the evidence.
Exercise
1% of patients have a disease. A test is positive for 90% of patients with the disease and for 9% of patients without it. A patient tests positive. What is the posterior probability of disease?
Show answer
Evidence = 0.9 · 0.01 + 0.09 · 0.99 = 0.009 + 0.0891 = 0.0981. Posterior = 0.009 / 0.0981 ≈ 0.092, about 9%. The low prior keeps the posterior low despite the accurate-looking test.