2.4.10SVM, Naive Bayes & Probabilistic Models

Laplace smoothing

1,612 words7 min readdifficulty · medium3 backlinks

WHY do we need it?

Naive Bayes classifies using:

P(Cx1,,xn)P(C)i=1nP(xiC)P(C \mid x_1,\dots,x_n) \propto P(C)\prod_{i=1}^{n} P(x_i \mid C)

The Maximum Likelihood estimate for one conditional is just a count ratio:

PML(xi=vC)=count(xi=v,C)count(C)P_{ML}(x_i = v \mid C) = \frac{\text{count}(x_i=v, C)}{\text{count}(C)}

WHAT is Laplace smoothing?


HOW to derive it (Bayesian first principles)

Model the counts of the KK outcomes as a Multinomial with unknown probabilities θ=(θ1,,θK)\theta=(\theta_1,\dots,\theta_K). Put a Dirichlet prior with all parameters equal to α\alpha:

θDir(α,,α),p(θ)vθvα1\theta \sim \text{Dir}(\alpha,\dots,\alpha),\qquad p(\theta)\propto \prod_{v}\theta_v^{\alpha-1}

Observe counts c1,,cKc_1,\dots,c_K (total NN). The Dirichlet is conjugate to the Multinomial, so the posterior is again Dirichlet:

θdataDir(c1+α,,cK+α)\theta \mid \text{data} \sim \text{Dir}(c_1+\alpha,\dots,c_K+\alpha)

The posterior mean of θv\theta_v (the natural point estimate) is:

E[θvdata]=cv+αu(cu+α)=cv+αN+αK\mathbb{E}[\theta_v \mid \text{data}] = \frac{c_v+\alpha}{\sum_{u}(c_u+\alpha)} = \frac{c_v+\alpha}{N+\alpha K}
Figure — Laplace smoothing

Worked Example 1 — Spam word

Vocabulary size K=5K=5: {buy, free, meeting, project, lunch}. In the spam class we saw N=8N=8 words total, with counts: buy=4, free=3, meeting=1, project=0, lunch=0.

Raw MLE for "project":

PML(projectspam)=08=0→ kills the productP_{ML}(\text{project}\mid \text{spam}) = \frac{0}{8}=0 \quad \text{→ kills the product}

Why this step? Shows the failure: any email containing "project" would get spam-score 0 regardless of other words.

Add-one (α=1\alpha=1):

P(projectspam)=0+18+15=1130.077P(\text{project}\mid \text{spam}) = \frac{0+1}{8+1\cdot 5}=\frac{1}{13}\approx 0.077

Why this step? Denominator is N+αK=8+5N+\alpha K = 8+5. Now the impossible becomes merely unlikely.

Check "buy":

P(buyspam)=4+18+5=5130.385P(\text{buy}\mid\text{spam})=\frac{4+1}{8+5}=\frac{5}{13}\approx 0.385

Why this step? Smoothing shrinks confident estimates slightly toward uniform (1/5=0.21/5=0.2), which is desirable regularization.

Sum check: 5+4+2+1+113=1313=1\frac{5+4+2+1+1}{13}=\frac{13}{13}=1Why this step? Confirms it's a valid distribution — the whole point of using αK\alpha K.


Worked Example 2 — Effect of α\alpha

Same "project" count = 0, N=8N=8, K=5K=5:

α\alpha P(projectspam)=0+α8+5αP(\text{project}\mid\text{spam}) = \dfrac{0+\alpha}{8+5\alpha}
0.0010.001 0.000125\approx 0.000125 (barely smoothed)
11 1/130.0771/13 \approx 0.077
100100 100/5080.1971/5100/508 \approx 0.197 \approx 1/5 (almost uniform)

Why this step? As α\alpha\to\infty, every estimate 1K\to \frac1K (max smoothing, ignores data). As α0\alpha\to 0, we recover raw MLE. So α\alpha is a bias–variance knob, tuned by cross-validation.


Worked Example 3 — Test word never seen at all

Suppose a test email contains "hello", which is not in the vocabulary for either class. Handling: either drop unknown tokens, or reserve an <UNK> slot so KK counts it. If included with count 0:

P(hellospam)=0+1Nspam+(K+1)P(\text{hello}\mid\text{spam})=\frac{0+1}{N_{spam}+ (K{+}1)}

Why this step? Consistency: you must use the same KK (including the UNK slot) in both numerator normalization and every class, otherwise probabilities across classes are on different scales.


Recall Feynman: explain to a 12-year-old

Imagine you're guessing which cereal box a toy comes in. You opened 8 boxes, saw some toys but never saw a robot. Does that mean a robot is impossible? No! You just haven't opened enough boxes. So before you start, you pretend you already found one of every toy. Now nothing is "impossible," just rare. That's Laplace smoothing — adding one imaginary sighting of everything so a surprise doesn't break your whole guess.


Forecast-then-Verify

Before reading further, predict: if I double the vocabulary KK but keep counts fixed, does a zero-count word's smoothed probability go up or down?Down, because denominator N+αKN+\alpha K grows while numerator α\alpha stays — more possible outcomes means each unseen one is individually rarer. (Verified in the VERIFY block.)


Flashcards

Why does unsmoothed Naive Bayes fail on unseen feature values?
The conditional probability becomes 0, and since NB multiplies conditionals, one 0 makes the entire class posterior 0.
Write the Laplace-smoothed conditional probability.
P(xi=vC)=count(xi=v,C)+αcount(C)+αKP(x_i=v\mid C)=\dfrac{\text{count}(x_i=v,C)+\alpha}{\text{count}(C)+\alpha K}.
Why is αK\alpha K (not α\alpha) added to the denominator?
To keep the smoothed distribution normalized — summing over the KK outcomes gives exactly 1.
What is KK in Laplace smoothing?
The number of possible values the feature can take (e.g. vocabulary size for text).
What is add-one smoothing?
Laplace smoothing with α=1\alpha=1.
What is Lidstone smoothing?
Laplace smoothing with a general 0<α<10<\alpha<1.
What Bayesian model yields Laplace smoothing exactly?
Multinomial likelihood with a symmetric Dirichlet(α,,α\alpha,\dots,\alpha) prior; the smoothed estimate is the posterior mean.
What happens as α\alpha\to\infty?
Every estimate approaches the uniform 1/K1/K (maximum smoothing, data ignored).
What happens as α0\alpha\to 0?
You recover the raw Maximum Likelihood count ratio (no smoothing).
How do you tune α\alpha?
Via cross-validation; it's a bias–variance regularization knob.

Connections

Concept Map

uses

can produce

due to product

fixed by

defined as

alpha K guarantees

prior

conjugate to

gives

point estimate

equals

Naive Bayes multiplies P xi given C

MLE count ratio estimate

Unseen outcome gives probability 0

One zero annihilates class score

Laplace add-alpha smoothing

Add pseudocount alpha and alpha K

Ensures probabilities sum to 1

Dirichlet prior Dir alpha

Multinomial counts model

Posterior Dir c plus alpha

Posterior mean estimate

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, Naive Bayes ka simple funda hai: har class ke liye hum saare feature probabilities ko multiply karte hain. Ab problem yeh hai — agar training data mein koi word kisi class ke saath ek baar bhi nahi dikha, to uski probability seedha 0 ban jaati hai. Aur 0 ko kisi bhi cheez se multiply karo, poora answer 0 ho jaata hai. Matlab ek anjaan word puri prediction ko barbaad kar deta hai. Yeh overconfident behaviour hai — "maine nahi dekha, iska matlab impossible hai" — jo galat hai.

Laplace smoothing isko fix karta hai bahut hi cute tareeke se: har outcome ko pehle se hi thoda count de do, jaise humne har cheez ek baar (ya α\alpha baar) dekh li ho. Formula banta hai count+αN+αK\frac{count+\alpha}{N+\alpha K}. Yahan KK = total possible values (text mein vocabulary size). Neeche αK\alpha K isliye add karte hain taaki saari probabilities milke exactly 1 banein — warna distribution valid hi nahi rahega.

Deep baat yeh hai ki yeh koi jugaad nahi hai — yeh actually ek Dirichlet prior ke saath Bayesian estimate ka posterior mean hai. α\alpha ek knob hai: chhota α\alpha = data pe zyada bharosa (kam smoothing), bada α\alpha = sab kuch uniform (1/K1/K) ki taraf khinch jaata hai (zyada smoothing). Isko cross-validation se tune karte ho. Bas yaad rakhna: "+α\alpha upar, +αK\alpha K neeche" — zero kabhi nahi aayega, aur sab kuch sum-to-one rahega. Simple, powerful, exam-favourite.

Test yourself — SVM, Naive Bayes & Probabilistic Models

Connections