Skip to main content
Core Pattern · Interactive Simeasy•DECEMBER 2026•15 MIN

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.

generative classifierbayes rulelaplace smoothinglog-oddsconditional independenceclassical mlnlpprobabilistic modelstext classification

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.

LOADING NAIVE BAYES LIKELIHOODS & LAPLACE SMOOTHING...

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).

LOADING NAIVE BAYES LOG-ODDS WATERFALL...

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%+.

LOADING NAIVE BAYES DOUBLE-COUNTING STRESS TEST...

5. Training & Optimization

Unlike Logistic Regression or CRFs, Multinomial Naive Bayes requires no iterative gradient optimization:

  1. Single pass over the training corpus: Count documents per class (N_c) and token occurrences per class (count(w, c)).
  2. Closed-form MLE / MAP estimate: Divide counts (with α smoothing) in O(N · T) time.
  3. Incremental online updates: New training documents simply increment existing count tables—no full retraining needed.

6. Comparison & Failure Modes

PropertyNaive Bayes (Generative)Logistic Regression (Discriminative)
ModelsJoint P(x, y) = P(y) Π P(x_j | y)Conditional P(y | x) = σ(w · x + b)
TrainingClosed-form counting (O(N))Iterative IRLS / L-BFGS
Small-data regimeConverges faster (O(log d) samples)Needs more data (O(d) samples)
Correlated featuresDouble-counts (overconfident posteriors)Weights split cleanly across correlated features
Missing valuesNaturally marginalizes out missing x_jRequires imputation

Common Failure Modes

  • Zero-frequency collapse: Always use α > 0 (typically α = 0.1..1.0 tuned 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.

Related Lab Entries