The Problem
Without smoothing, any word never seen with class c in training gets P(w∣c)=0. One unseen word makes the entire Naive Bayes product collapse to zero. Zipf’s Law and Heaps’ Law guarantee that unseen words are ubiquitous.
Laplace (Add-One) Smoothing
Add 1 to every word count and add ∣V∣ to the denominator:
P(w∣c)=∑w′∈Vcount(w′,c)+∣V∣count(w,c)+1
- ∣V∣: total number of unique word types across all training data (all classes combined)
- Effect: shifts a small amount of probability mass from seen words to unseen ones
- Every word type, even those never seen in training, now has P(w∣c)>0
General Smoothing Parameter ω
P(w∣c)=∑w′∈Vcount(w′,c)+ω×∣V∣count(w,c)+ω
Laplace smoothing is the special case ω=1. The parameter ω can be tuned on the development set: try ω=0.1,0.5,1.0,2.0 and pick the value that maximises dev-set accuracy or F1.
Worked Example
Revisiting the small 2-class problem from the Naive Bayes note.
Training data:
- SPAM documents: “buy now”, “free buy”
- HAM documents: “meeting now”, “free lunch”
Vocabulary V={buy,now,free,meeting,lunch}, ∣V∣=5.
Smoothed likelihoods:
SPAM class (total tokens = 3):
P(buy∣SPAM)=3+52+1=83=0.375
P(now∣SPAM)=3+51+1=82=0.25
P(free∣SPAM)=3+51+1=82=0.25
P(meeting∣SPAM)=3+50+1=81=0.125
P(lunch∣SPAM)=3+50+1=81=0.125
HAM class (total tokens = 4):
P(meeting∣HAM)=4+51+1=92≈0.222
P(now∣HAM)=4+51+1=92≈0.222
P(free∣HAM)=4+51+1=92≈0.222
P(lunch∣HAM)=4+51+1=92≈0.222
P(buy∣HAM)=4+50+1=91≈0.111
Classify “buy lunch”:
Priors: P(SPAM)=0.5, P(HAM)=0.5.
P(SPAM∣d)∝0.5×0.375×0.125=0.5×0.046875=0.02344
P(HAM∣d)∝0.5×0.111×0.222=0.5×0.02464=0.01232
The classifier predicts SPAM. Without smoothing, both products were zero; the classifier could not choose. With Laplace smoothing, every word has non-zero probability, and the classifier works correctly.
Exam Rule
Apply smoothing whenever the question does not explicitly say “without smoothing”. Always state ∣V∣ explicitly as the denominator correction.
Summary
| Smoothing | Formula | ω |
|---|
| Laplace (add-one) | (count+1)/(total+∣V∣) | 1 |
| Add-ω (general) | (count+ω)/(total+ω∣V∣) | Tuned on dev set |
Past paper questions: y2021p3q9(a), y2023p3q8(a), y2024p3q9(a)