Naive Bayes: Generative Priors, Laplace Smoothing & Log-Odds
Deep-dive into Multinomial Naive Bayes: how class priors and conditional word likelihoods multiply via Bayes' rule, why unseen words collapse probabilities without Laplace smoothing, and how correlated features cause double-counting.
1. Problem & Generative Framing
Naive Bayes is the simplest generative classifier. Instead of learning a direct boundary between classes (P(y | x)), it models how each class generates its features (P(x | y)) and flips the direction using Bayes' rule:
P(c | x) = P(c) · P(x | c) / P(x)For a document x = (w_1, ..., w_T) with T words, modeling the full joint distribution P(w_1, ..., w_T | c) requires estimating |V|^T combinations—completely intractable. Naive Bayes makes one bold simplifying assumption: conditional independence of every feature given the class c:
P(x | c) ≈ Π_{t=1..T} P(w_t | c)2. Scoring & Likelihood Machinery
In Multinomial Naive Bayes, each class c maintains a frequency table over the vocabulary V. With Laplace (Lidstone) smoothing α ≥ 0:
P(w | c) = ( count(w, c) + α ) / ( Σ_{w' ∈ V} count(w', c) + α |V| )Why is α > 0 mandatory in practice? If a test message contains a word like "lottery" that appeared 0 times in Ham during training, then at α = 0:
P("lottery" | Ham) = 0, which multiplies the entire product P(Ham) · Π P(w_t | Ham) to zero—wiping out all other evidence in the message!
Interactive Likelihood & Laplace Smoothing Explorer
Click "Trigger α = 0 Collapse" on "lottery meeting lunch" to watch a single unseen word force P(Spam | x) = 100%, then slide α up to 1.0 to see smoothing restore balance.
3. From Raw Counts to Posterior Probabilities
Multiplying dozens of small probabilities (0.02 × 0.004 × ...) underflows float64 rapidly. Instead, Naive Bayes converts products into sums in log-space, and normalizes two classes via the log-odds log(P(Spam | x) / P(Ham | x)):
log P(c | x) ∝ log P(c) + Σ_{t=1..T} log P(w_t | c)
log [ P(Spam | x) / P(Ham | x) ]
= log [ P(Spam) / P(Ham) ] + Σ_{t=1..T} log [ P(w_t | Spam) / P(w_t | Ham) ]Notice something remarkable: Naive Bayes in log-space is a linear model! The prior ratio is the bias b, and each word's log-likelihood ratio log(P(w | Spam) / P(w | Ham)) acts as a weight w_j.
Interactive Log-Odds Waterfall
Inspect how the class prior (log(0.4 / 0.6) = -0.405) combines additively with each token's signed log-odds to produce the final sigmoid probability P(Spam | x).
4. Inference & Calibration Stress Test
At inference time, Naive Bayes computes log P(c) + Σ_t log P(w_t | c) for each class c in O(T) time and picks argmax_c (or applies softmax / logsumexp for posteriors).
However, because it assumes conditional independence, correlated or repeated evidence gets double-counted. Repeating "urgent" or pairing two synonymous spam phrases multiplies the same log-odds repeatedly, pushing P(Spam | x) toward 99.9%+.
5. Training & Optimization
Unlike Logistic Regression or CRFs, Multinomial Naive Bayes requires no iterative gradient optimization:
- Single pass over the training corpus: Count documents per class (
N_c) and token occurrences per class (count(w, c)). - Closed-form MLE / MAP estimate: Divide counts (with
αsmoothing) inO(N · T)time. - Incremental online updates: New training documents simply increment existing count tables—no full retraining needed.
6. Comparison & Failure Modes
| Property | Naive Bayes (Generative) | Logistic Regression (Discriminative) |
|---|---|---|
| Models | Joint P(x, y) = P(y) Π P(x_j | y) | Conditional P(y | x) = σ(w · x + b) |
| Training | Closed-form counting (O(N)) | Iterative IRLS / L-BFGS |
| Small-data regime | Converges faster (O(log d) samples) | Needs more data (O(d) samples) |
| Correlated features | Double-counts (overconfident posteriors) | Weights split cleanly across correlated features |
| Missing values | Naturally marginalizes out missing x_j | Requires imputation |
Common Failure Modes
- Zero-frequency collapse: Always use
α > 0(typicallyα = 0.1..1.0tuned on validation). - Poor probability calibration: Do not use raw Naive Bayes probabilities as calibrated risk scores without Platt scaling or isotonic calibration when features are correlated.
- Imbalanced priors: A dominant prior
P(c)can overwhelm weak word counts; inspect prior log-odds separately from likelihood log-odds.