2.4.9SVM, Naive Bayes & Probabilistic Models

Multinomial and Bernoulli Naive Bayes

1,918 words9 min readdifficulty · medium2 backlinks

WHY do we need these at all?

We want P(classdocument)P(\text{class} \mid \text{document}). By Bayes' theorem:

P(cx)=P(c)P(xc)P(x)P(c \mid \mathbf{x}) = \frac{P(c)\,P(\mathbf{x}\mid c)}{P(\mathbf{x})}

The hard part is P(xc)P(\mathbf{x}\mid c) — a joint distribution over all words. That's astronomically many combinations. The "naive" assumption rescues us:

Now we only estimate one word at a time. The two variants differ in what P(xic)P(x_i\mid c) looks like.


Multinomial Naive Bayes

Deriving the likelihood from first principles

A document is x=(x1,,xV)\mathbf{x} = (x_1,\dots,x_{|V|}) where xtx_t = count of word tt, and n=txtn=\sum_t x_t is the length. Rolling a die nn times with face-probabilities ptcp_{t\mid c} gives the multinomial distribution:

P(xc)=n!txt!tptcxtP(\mathbf{x}\mid c) = \frac{n!}{\prod_t x_t!}\prod_t p_{t\mid c}^{\,x_t}

Why this form? tptcxt\prod_t p_{t\mid c}^{x_t} is the probability of one specific ordering of the rolls; n!txt!\frac{n!}{\prod_t x_t!} counts how many orderings give the same counts. The factorial part is the same for every class, so it cancels in classification and we usually drop it.

We take logs (products underflow, sums are stable):

logP(cx)    logP(c)+txtlogptc\log P(c\mid \mathbf{x}) \;\propto\; \log P(c) + \sum_t x_t \,\log p_{t\mid c}

HOW we derive the MLE (why counts/total?): maximize tNtclogptc\sum_t N_{tc}\log p_{t\mid c} subject to tptc=1\sum_t p_{t\mid c}=1. Lagrangian L=tNtclogptλ(tpt1)\mathcal{L}=\sum_t N_{tc}\log p_{t} - \lambda(\sum_t p_t -1). Setting L/pt=Ntc/ptλ=0\partial\mathcal{L}/\partial p_t = N_{tc}/p_t - \lambda = 0 gives pt=Ntc/λp_t = N_{tc}/\lambda; the constraint forces λ=Nc\lambda=N_c, so ptc=Ntc/Ncp_{t\mid c}=N_{tc}/N_c. Smoothing just perturbs this.


Bernoulli Naive Bayes

Deriving the likelihood

Features are binary: xt{0,1}x_t\in\{0,1\} (present/absent). Each is a Bernoulli trial:

P(xtc)=ptcxt(1ptc)1xtP(x_t\mid c) = p_{t\mid c}^{\,x_t}\,(1-p_{t\mid c})^{\,1-x_t}

Why this trick? When xt=1x_t=1 it selects ptcp_{t\mid c}; when xt=0x_t=0 it selects (1ptc)(1-p_{t\mid c}). One formula, two cases. Over all words:

logP(cx)logP(c)+tV[xtlogptc+(1xt)log(1ptc)]\log P(c\mid \mathbf{x}) \propto \log P(c) + \sum_{t\in V}\Big[x_t\log p_{t\mid c} + (1-x_t)\log(1-p_{t\mid c})\Big]


Figure — Multinomial and Bernoulli Naive Bayes

Worked Example (both models on the same data)

Vocabulary V={cheap, meeting, free}V=\{\text{cheap},\ \text{meeting},\ \text{free}\}, V=3|V|=3.

Spam docs: D1 = "cheap cheap free", D2 = "free free cheap" Ham docs: D3 = "meeting free", D4 = "meeting meeting"

Priors: P(spam)=P(ham)=1/2P(\text{spam})=P(\text{ham})=1/2.

Multinomial (α=1\alpha=1)

Counts in spam: cheap=3, free=3, meeting=0, so Nspam=6N_{\text{spam}}=6. pcheaps=3+16+3=49,pfrees=49,pmeetings=19p_{\text{cheap}\mid s}=\frac{3+1}{6+3}=\tfrac49,\quad p_{\text{free}\mid s}=\tfrac49,\quad p_{\text{meeting}\mid s}=\tfrac19 Counts in ham: meeting=3, free=1, cheap=0, Nham=4N_{\text{ham}}=4. pmeetingh=3+14+3=47, pfreeh=27, pcheaph=17p_{\text{meeting}\mid h}=\frac{3+1}{4+3}=\tfrac47,\ p_{\text{free}\mid h}=\tfrac27,\ p_{\text{cheap}\mid h}=\tfrac17

Classify "cheap free" (counts: cheap=1, free=1, meeting=0):

  • Spam score 12pcheapspfrees=124949=16162=0.0988\propto \tfrac12 \cdot p_{\text{cheap}\mid s}\,p_{\text{free}\mid s} = \tfrac12\cdot\tfrac49\cdot\tfrac49 = \tfrac{16}{162}=0.0988
  • Ham score 121727=298=0.0204\propto \tfrac12\cdot\tfrac17\cdot\tfrac27 = \tfrac{2}{98}=0.0204

Why this step? Only words present (nonzero count) contribute; "meeting" has count 0 so p0=1p^0=1 and drops out. → SPAM.

Bernoulli (α=1\alpha=1)

Document-presence counts, spam (Ds=2D_s=2): cheap in {D1,D2}=2, free=2, meeting=0. pcheaps=2+12+2=34, pfrees=34, pmeetings=14p_{\text{cheap}\mid s}=\frac{2+1}{2+2}=\tfrac34,\ p_{\text{free}\mid s}=\tfrac34,\ p_{\text{meeting}\mid s}=\tfrac14 Ham (Dh=2D_h=2): meeting=2, free=1, cheap=0. pmeetingh=34, pfreeh=24=12, pcheaph=14p_{\text{meeting}\mid h}=\tfrac34,\ p_{\text{free}\mid h}=\tfrac24=\tfrac12,\ p_{\text{cheap}\mid h}=\tfrac14

Classify "cheap free" → binary vector cheap=1, free=1, meeting=0:

  • Spam 12pcheappfree(1pmeeting)=12343434=0.2109\propto \tfrac12\cdot p_{\text{cheap}}\cdot p_{\text{free}}\cdot(1-p_{\text{meeting}}) = \tfrac12\cdot\tfrac34\cdot\tfrac34\cdot\tfrac34 = 0.2109
  • Ham 121412(134)=12141214=164=0.0156\propto \tfrac12\cdot\tfrac14\cdot\tfrac12\cdot(1-\tfrac34)=\tfrac12\cdot\tfrac14\cdot\tfrac12\cdot\tfrac14 = \tfrac{1}{64}=0.0156

Why this step? Notice the extra (1pmeeting)(1-p_{\text{meeting}}) factor — Bernoulli rewards the absence of "meeting" (a hammy word). Multinomial ignored it entirely. → SPAM (even more confidently).



Recall Feynman: explain to a 12-year-old

Imagine sorting letters into "junk" and "real" piles.

  • Multinomial is like counting how many times each word shows up. The word "FREE!!!" appearing 5 times screams junk louder than appearing once.
  • Bernoulli just ticks a checklist: "Does the word 'free' show up? Yes/No." It doesn't care if it's 1 or 5 times — and it also notices when an expected friendly word like "meeting" is missing, which is a clue too. Both then say: "Which pile makes this exact set of words most likely?" and pick that pile.

Flashcards

What is the naive assumption in Naive Bayes?
Given the class, all features are conditionally independent, so P(xc)=iP(xic)P(\mathbf{x}\mid c)=\prod_i P(x_i\mid c).
In Multinomial NB, what does each feature xtx_t represent?
The count (frequency) of word tt in the document.
In Bernoulli NB, what does each feature xtx_t represent?
A binary indicator: whether word tt is present (1) or absent (0).
Write the Multinomial NB parameter estimate with Laplace smoothing.
ptc=Ntc+αNc+αVp_{t\mid c}=\dfrac{N_{tc}+\alpha}{N_c+\alpha|V|}, where NtcN_{tc} is total count of word tt in class cc.
Write the Bernoulli NB parameter estimate.
ptc=Dtc+αDc+2αp_{t\mid c}=\dfrac{D_{tc}+\alpha}{D_c+2\alpha}, where DtcD_{tc} is number of docs of class cc containing word tt.
Why is the denominator Dc+2αD_c+2\alpha in Bernoulli but Nc+αVN_c+\alpha|V| in Multinomial?
Bernoulli word has 2 outcomes (present/absent) → 2α2\alpha; Multinomial has V|V| possible faces → αV\alpha|V|.
Why does Multinomial ignore absent words but Bernoulli doesn't?
In Multinomial, pxtp^{x_t} with xt=0x_t=0 gives 1 (no contribution). In Bernoulli, absent words contribute the factor (1ptc)(1-p_{t\mid c}), explicitly penalizing/rewarding absence.
Why do we take logarithms of the Naive Bayes score?
Products of many small probabilities underflow numerically; logs turn products into stable sums.
Why is smoothing (α>0\alpha>0) necessary?
A single zero probability makes the whole product 0 (log0=\log 0=-\infty), so unseen words would veto a class. Smoothing keeps estimates finite.
Derive the MLE ptc=Ntc/Ncp_{t\mid c}=N_{tc}/N_c — what optimization gives it?
Maximize tNtclogpt\sum_t N_{tc}\log p_t subject to tpt=1\sum_t p_t=1 (Lagrange); yields pt=Ntc/Ncp_t=N_{tc}/N_c.
When should you prefer Multinomial over Bernoulli?
When word frequency carries signal and documents vary in length (typical for longer text / TF features); Bernoulli suits short texts where mere presence matters.

Connections

  • Bayes Theorem — the underlying inference rule both models apply.
  • Naive Bayes Classifier — the general framework; these are two likelihood choices.
  • Gaussian Naive Bayes — the continuous-feature cousin (P(xc)P(x\mid c) is a normal density).
  • Laplace Smoothing — the regularization used in both estimates.
  • Bag of Words / TF-IDF — feature representations feeding these models.
  • Log-Sum-Exp trick — numerically stable normalization of the log-scores.
  • Maximum Likelihood Estimation — source of the parameter formulas.

Concept Map

needs

too many combos

factorizes into

frequency model

presence model

likelihood is

log form

params via

avoid zero probs

counts word occurrences

binary presence/absence

Bayes theorem

P x given c

Naive independence assumption

Per-word P xi given c

Multinomial NB

Bernoulli NB

Multinomial distribution

Sum xt log p

MLE counts over total

Laplace smoothing alpha

Word frequencies

Word present or not

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, dono Naive Bayes text classify karne ke liye same Bayes theorem use karte hain — bas ye difference hai ki word ko kaise dekhte hain. Multinomial NB word ki ginti (count/frequency) dekhta hai: agar "free" 5 baar aaya to signal zyada strong. Isse socho ek loaded dice ki tarah jiske har face pe ek word hai, aur document banane ke liye tum n baar roll karte ho. Isliye formula mein ptc=(Ntc+1)/(Nc+V)p_{t|c}=(N_{tc}+1)/(N_c+|V|) aata hai — total count divided by total words, plus Laplace smoothing.

Bernoulli NB sirf presence dekhta hai: word aaya ya nahi (1 ya 0). Har word ke liye ek coin flip. Ye ek chhoti par important baat karta hai — jab koi word absent hai to bhi wo score ko affect karta hai, kyunki formula mein (1pt)(1-p_t) factor lagta hai. Matlab agar "meeting" (ham-type word) missing hai to spam ka score badh jata hai. Multinomial isko ignore kar deta (p0=1p^0=1).

Smoothing kyun? Agar koi word training mein kabhi nahi dikha, uski probability 0 ho jaati, aur ek bhi 0 poore product ko 0 bana deta (log0=\log 0 = -\infty). Isliye α\alpha (usually 1) add karte hain — jaise har word ko thoda extra baar dekh liya ho. Multinomial mein denominator mein αV\alpha|V| add hota (kyunki V|V| faces), Bernoulli mein 2α2\alpha (kyunki sirf 2 outcomes: present/absent). Ye mat mix karna, warna probabilities galat ho jaati.

Rule of thumb: lambe documents / frequency matter kare to Multinomial, chhote text jahan sirf word ka hona-na-hona matter kare to Bernoulli. Yaad rakho — "Multi = Many (counts), Bern = Binary".

Test yourself — SVM, Naive Bayes & Probabilistic Models

Connections