Skip to content
Part IA Lent Term

Laplace Add-One Smoothing

The Problem

Without smoothing, any word never seen with class cc in training gets P(wc)=0P(w \mid 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\lvert V \rvert to the denominator:

P(wc)=count(w,c)+1wVcount(w,c)+VP(w \mid c) = \frac{\text{count}(w, c) + 1}{\sum_{w' \in V} \text{count}(w', c) + \lvert V \rvert}

  • V\lvert V \rvert: 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(wc)>0P(w \mid c) > 0

General Smoothing Parameter ω\omega

P(wc)=count(w,c)+ωwVcount(w,c)+ω×VP(w \mid c) = \frac{\text{count}(w, c) + \omega}{\sum_{w' \in V} \text{count}(w', c) + \omega \times \lvert V \rvert}

Laplace smoothing is the special case ω=1\omega = 1. The parameter ω\omega can be tuned on the development set: try ω=0.1,0.5,1.0,2.0\omega = 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 = \{\text{buy}, \text{now}, \text{free}, \text{meeting}, \text{lunch}\}, V=5\lvert V \rvert = 5.

Smoothed likelihoods:

SPAM class (total tokens = 3):

P(buySPAM)=2+13+5=38=0.375P(\text{buy} \mid \text{SPAM}) = \frac{2 + 1}{3 + 5} = \frac{3}{8} = 0.375

P(nowSPAM)=1+13+5=28=0.25P(\text{now} \mid \text{SPAM}) = \frac{1 + 1}{3 + 5} = \frac{2}{8} = 0.25

P(freeSPAM)=1+13+5=28=0.25P(\text{free} \mid \text{SPAM}) = \frac{1 + 1}{3 + 5} = \frac{2}{8} = 0.25

P(meetingSPAM)=0+13+5=18=0.125P(\text{meeting} \mid \text{SPAM}) = \frac{0 + 1}{3 + 5} = \frac{1}{8} = 0.125

P(lunchSPAM)=0+13+5=18=0.125P(\text{lunch} \mid \text{SPAM}) = \frac{0 + 1}{3 + 5} = \frac{1}{8} = 0.125

HAM class (total tokens = 4):

P(meetingHAM)=1+14+5=290.222P(\text{meeting} \mid \text{HAM}) = \frac{1 + 1}{4 + 5} = \frac{2}{9} \approx 0.222

P(nowHAM)=1+14+5=290.222P(\text{now} \mid \text{HAM}) = \frac{1 + 1}{4 + 5} = \frac{2}{9} \approx 0.222

P(freeHAM)=1+14+5=290.222P(\text{free} \mid \text{HAM}) = \frac{1 + 1}{4 + 5} = \frac{2}{9} \approx 0.222

P(lunchHAM)=1+14+5=290.222P(\text{lunch} \mid \text{HAM}) = \frac{1 + 1}{4 + 5} = \frac{2}{9} \approx 0.222

P(buyHAM)=0+14+5=190.111P(\text{buy} \mid \text{HAM}) = \frac{0 + 1}{4 + 5} = \frac{1}{9} \approx 0.111

Classify “buy lunch”:

Priors: P(SPAM)=0.5P(\text{SPAM}) = 0.5, P(HAM)=0.5P(\text{HAM}) = 0.5.

P(SPAMd)0.5×0.375×0.125=0.5×0.046875=0.02344P(\text{SPAM} \mid d) \propto 0.5 \times 0.375 \times 0.125 = 0.5 \times 0.046875 = 0.02344

P(HAMd)0.5×0.111×0.222=0.5×0.02464=0.01232P(\text{HAM} \mid d) \propto 0.5 \times 0.111 \times 0.222 = 0.5 \times 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\lvert V \rvert explicitly as the denominator correction.

Summary

SmoothingFormulaω\omega
Laplace (add-one)(count+1)/(total+V)(\text{count}+1) / (\text{total}+\lvert V \rvert)1
Add-ω\omega (general)(count+ω)/(total+ωV)(\text{count}+\omega) / (\text{total}+\omega\lvert V \rvert)Tuned on dev set

Past paper questions: y2021p3q9(a), y2023p3q8(a), y2024p3q9(a)