Probabilistic & Generative Classifiers: Naive Bayes, LDA & QDA
Generative vs. Discriminative modeling, Multinomial & Gaussian Naive Bayes, Laplace smoothing, log-sum-exp stability, and Linear vs. Quadratic Discriminant Analysis.
Generative classifiers (Naive Bayes, LDA, QDA) model how each class generates its features P(x|y) and apply Bayes' theorem to infer the class probability P(y|x).
They train in milliseconds, shine on small datasets and high-dimensional text (spam/sentiment), and handle missing features gracefully.
Generative classifiers are blazingly fast, shine on tiny datasets, and teach the probabilistic backbone behind modern AI!
Multinomial Naive Bayes trains on millions of text documents in milliseconds with O(N·d) counting — making it the ultimate ultra-low-latency spam/intent baseline before spinning up a GPU transformer.
Understanding Generative vs. Discriminative tradeoffs (Ng & Jordan 2002: Naive Bayes beats Logistic Regression at small N, Logistic Regression wins at large N) shows deep statistical maturity.
Interviewers frequently ask: 'If features are correlated, why does Naive Bayes still achieve high classification accuracy even though its probabilities are badly calibrated?'
In a spam filter using Naive Bayes WITHOUT smoothing (α = 0), an email contains 'FREE', 'WINNER', and one brand-new word 'CRYPTO2026' that never appeared in spam training data. What is the predicted P(Spam | email)?
- Distinguish Generative models P(X|y)P(y) from Discriminative models P(y|X)
- Derive Naive Bayes under conditional independence and explain why it still ranks well despite 'naive' assumptions
- Prevent zero-frequency collapse with Laplace smoothing (+α) and numeric underflow with log-probabilities
- Compare Multinomial, Bernoulli, and Gaussian Naive Bayes for text vs. continuous data
- Derive why shared covariance Σ in LDA produces a linear boundary while class-specific Σ_k in QDA produces a quadratic boundary
Discriminative classifiers (like Logistic Regression) only learn the fence separating cats from dogs. Generative classifiers (like Naive Bayes, LDA, QDA) learn a full statistical blueprint of what a cat looks like P(X | cat) and what a dog looks like P(X | dog) — and then use Bayes' Rule to ask which blueprint best explains a new photo!
1. Generative vs. Discriminative Classifiers (Ng & Jordan Tradeoff)
If you only have 40 training rows, Logistic Regression can easily overfit a crazy hyperplane. Naive Bayes makes a strong structural assumption (conditional independence), which gives it higher bias but much lower variance — so it converges in samples vs. for Logistic Regression!
Bayes' Rule in Log-Space
- Why logs? Multiplying 500 small word probabilities ( underflows IEEE-754 float64 to exact
0.0! Adding log-probabilities stays numerically rock-solid.
2. Naive Bayes Variants & Laplace Smoothing Live
Pick the likelihood family matching your data: Multinomial NB for word counts/TF-IDF, Bernoulli NB for binary word presence, and Gaussian NB for continuous numeric features. Toggle words and flip α = 0 vs α = 1 below to see Laplace smoothing rescue an email!
Laplace (Additive) Smoothing Formula
- Why Naive Bayes still classifies well with correlated features: Even if two duplicate features double-count the log-odds (pushing predicted probabilities toward extreme 0.001 or 0.999 — poor calibration!), the argmax class often remains completely unchanged!
Toggle Email Words & Watch Unseen-Word Collapse Disappear
3. Linear vs. Quadratic Discriminant Analysis (LDA vs. QDA)
Gaussian Naive Bayes assumes features are uncorrelated (diagonal covariance matrix). What if features ARE correlated? Model each class as a Multivariate Gaussian ! Share one across all classes → LDA (linear boundary). Give each class its own → QDA (curved quadratic boundary)!
Why the x² Term Cancels in LDA (Interview Favorite!)
Comparing log-posteriors of two Gaussians and :
- In LDA (): the quadratic term appears identically in both classes and cancels out, leaving a purely linear decision boundary!
- In QDA (): the terms do NOT cancel, creating a parabola/ellipse/hyperbola boundary — at the cost of estimating covariance parameters (high variance when is large!).
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
Naive Bayes blindly multiplies that feature's likelihood 5 times, making its probabilities wildly overconfident! Regularized Logistic Regression simply splits the weight across the 5 copies (w/5 each) and stays well-calibrated.
Shared Variance (LDA) vs. Class-Specific Variance (QDA)
LDA forces one pooled variance σ, so the x² terms cancel out and you ALWAYS get a single linear boundary!
Ultra-Fast Text & Spam Filtering
Multinomial Naive Bayes + TF-IDF trains in milliseconds on 100k+ vocabulary features.
Small-Sample Medical Diagnostics
LDA excels when N is small and Gaussian class-conditionals roughly hold.
Real-Time Streaming Baselines
Naive Bayes supports exact online updates (partial_fit) by simply incrementing counts.
from sklearn.naive_bayes import MultinomialNB, GaussianNB
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis, QuadraticDiscriminantAnalysis
# Text / count features with Laplace smoothing (alpha=1.0)
nb_text = MultinomialNB(alpha=1.0)
# Continuous correlated features: LDA (shared Sigma) vs QDA (separate Sigma_k)
lda = LinearDiscriminantAnalysis()
qda = QuadraticDiscriminantAnalysis(reg_param=0.05) # mild shrinkage prevents singular Sigma_k
lda.fit(X_train, y_train)
qda.fit(X_train, y_train)
reg_param > 0).