Core Idea
Given a document d and a set of classes C, find the class with the highest posterior probability:
c∗=c∈Cargmax P(c∣d)
By Bayes’ Theorem:
P(c∣d)=P(d)P(c)×P(d∣c)
Since P(d) is constant across all classes for a given document, we drop it:
c∗=c∈Cargmax P(c)×P(d∣c)
The Naive Assumption
The “naive” part: all features (words) are independent given the class. This lets us decompose the document probability into a product:
P(d∣c)=∏i=1nP(wi∣c)
where w1,w2,…,wn are the words in the document. The full classification rule:
c∗=c∈Cargmax P(c)×∏i=1nP(wi∣c)
Parameter Estimation (Unsmoothed MLE)
Prior:
P(c)=NtotalNc
where Nc is the number of training documents of class c.
Likelihood (Maximum Likelihood Estimate, no smoothing):
P(w∣c)=∑w′∈Vcount(w′,c)count(w,c)
where count(w,c) is the number of times word w appears in documents of class c, and V is the vocabulary.
The Zero-Probability Problem
If any word w in the test document never appeared with class c in training, then count(w,c)=0, so P(w∣c)=0. The product ∏P(wi∣c) becomes zero. If this happens for all classes, the classifier cannot decide.
Even one unseen word kills the entire product. This is not a rare edge case: Zipf’s Law and Heaps’ Law guarantee that test documents will contain unseen words.
Worked Example (Small 3-Word Problem)
Training data (2 classes, SPAM and HAM):
SPAM documents: “buy now”, “free buy”
HAM documents: “meeting now”, “free lunch”
Vocabulary V={buy,now,free,meeting,lunch}, ∣V∣=5.
Token counts per class:
- SPAM: buy=2, now=1, free=1 (3 tokens total)
- HAM: meeting=1, now=1, free=1, lunch=1 (4 tokens total)
Priors: P(SPAM)=2/4=0.5, P(HAM)=2/4=0.5.
Likelihoods (unsmoothed):
- P(buy∣SPAM)=2/3≈0.667
- P(buy∣HAM)=0/4=0 (never seen with HAM)
Classify test document “buy lunch”:
P(SPAM∣"buy lunch")∝0.5×(2/3)×P(lunch∣SPAM)
P(lunch∣SPAM)=0/3=0 (never seen with SPAM).
P(HAM∣"buy lunch")∝0.5×P(buy∣HAM)×(1/4)
P(buy∣HAM)=0/4=0 (never seen with HAM).
Both products collapse to zero. The classifier cannot choose. This is exactly what Laplace smoothing prevents.
Interpretability
Naive Bayes is interpretable: every parameter P(w∣c) is directly inspectable. If a feature value appears only with one class in training, you can identify the bias mathematically (e.g., P(Gender=F∣Success)=0 means the classifier can never predict Success for Female applicants). This property is invaluable for fairness auditing and is not available in black-box models.
Summary
| Component | Formula | Notes |
|---|
| Prior | P(c)=Nc/Ntotal | Fraction of training docs in class c |
| Likelihood (MLE) | P(w∣c)=count(w,c)/∑count(w′,c) | Zero for unseen words |
| Classification rule | c∗=argmaxc P(c)∏P(wi∣c) | Product collapses if any P(wi∣c)=0 |
Past paper questions: y2021p3q9(a), y2023p3q8(a), y2024p3q9(a)