Generative vs Discriminative Classifiers
LDA, naive Bayes and when modeling p(x | y) pays off.
A classifier can model the boundary between the classes, , or it can model each class, , and let Bayes' rule draw the boundary. The second road leads to discriminant analysis and naive Bayes, among the oldest and fastest classifiers there are. This lesson derives them, shows that linear discriminant analysis secretly produces a logistic regression, and asks when modeling more than you need pays off.
What you’ll learn
- Distinguish generative from discriminative classifiers, and classify by Bayes' rule from class-conditional models.
- Derive the posterior of Gaussian discriminant analysis, logistic with a shared covariance and quadratic without it, and connect it to Fisher's discriminant.
- Derive naive Bayes for Gaussian, Bernoulli and multinomial features, show that its log-odds are linear in suitable features, and explain why it can classify well although its independence assumption is false.
- Summarize what theory says about the trade-off: Efron's efficiency result, Ng & Jordan's two regimes and their critics, and the high-dimensional case.
Two ways to build a classifier
A generative classifier models the class proportions and the class-conditional densities , then applies Bayes' rule:
If the model is right, this is the Bayes-optimal classifier. A discriminative classifier, such as logistic regression or an SVM, models or the boundary directly and never says how the inputs themselves are distributed. For two classes every generative model has a logistic form:
Everything depends on what function of the log-likelihood ratio turns out to be.
| Question | Generative | Discriminative |
|---|---|---|
| What is modeled? | and | or the boundary only |
| Fitting | often closed form: counts, means, covariances | iterative optimization |
| Missing features at test time | marginalize them out of | needs imputation or a separate model |
| Unlabeled data | usable, e.g. through the EM algorithm | not directly |
| Class proportions change | replace , keep the rest | shift the intercepts by the log prior ratio |
| Model wrong in | the boundary inherits the error | unaffected, if the boundary family is right |
Gaussian discriminant analysis: LDA and QDA
Let each class be Gaussian, , so that
With a shared covariance , the quadratic term and the determinants cancel in the log-likelihood ratio, leaving a linear function of :
Add the log prior ratio: the posterior is exactly a logistic regression, with a linear boundary. This is linear discriminant analysis (LDA):
With separate covariances nothing cancels, and the log-odds are quadratic in : quadratic discriminant analysis (QDA), whose boundaries are conic sections in the plane.
Fitting is closed form. The maximum-likelihood estimates are the class frequencies , the class means , and for LDA the pooled within-class covariance (divide by instead for an unbiased estimate). One pass over the data, no iterations. The catch is the parameter count: a covariance in dimensions has entries, 5,050 for ; QDA needs one per class, and both need to be invertible, which fails when . Regularized discriminant analysis (Friedman, 1989) shrinks the class covariances toward the pooled one and toward a multiple of the identity.
Fisher's view. Fisher (1936), working with measurements of iris flowers, asked a question without any distributional assumption: which projection separates the class means most, relative to the spread within the classes? Maximizing , with the within-class scatter matrix, gives by the Cauchy–Schwarz inequality: the same direction as LDA. With two classes, it is also proportional to the least-squares coefficient vector for regressing a numerically coded label on .
Two equally likely Gaussian classes share a covariance, and their Mahalanobis distance is . What is the Bayes error rate?
Show the answer
15.9 %
Projected onto the LDA direction, the classes become two unit-variance Gaussians apart, and the optimal threshold sits halfway, from each mean. The error is . At it is about 30.9%, at about 6.7%: only the Mahalanobis distance matters, whatever the dimension.
Naive Bayes
Naive Bayes makes one sweeping assumption: the features are independent given the class, . Each factor is estimated on its own, so the model needs only parameters, trains in one pass, copes with , handles mixed feature types, and ignores a missing feature by simply dropping its factor.
| Variant | Model for | Log-odds (two classes) |
|---|---|---|
| Gaussian | a sum of one-variable quadratics, ; linear if each is shared across classes | |
| Bernoulli (word present or not) | with | : linear in |
| Multinomial (word counts) | , with | : linear in the counts |
In the discrete cases, naive Bayes is a linear classifier in the same features that logistic regression would use. The two differ only in how they choose the weights: naive Bayes maximizes the joint likelihood under the independence model, feature by feature; logistic regression maximizes the conditional likelihood , all weights together. That makes them a natural generative–discriminative pair.
Why does it work so often? The independence assumption is nearly always false, yet naive Bayes is often a strong classifier. Domingos & Pazzani (1997) showed that it is optimal under 0–1 loss in a far larger region than the one where independence holds: a classifier only has to rank the right class first, and the errors in the estimated probabilities often leave the ranking intact. The probabilities themselves are another matter: correlated features count the same evidence several times, which drives them toward 0 and 1, so naive Bayes is typically overconfident and needs recalibration if its probabilities are to be used.
Think first You accidentally include every feature twice in a naive Bayes model. With equal class priors, what happens to its predicted classes, and to its predicted probabilities?
Each log-likelihood ratio is now counted twice, so the log-odds become instead of . The sign, and so every predicted class, is unchanged, but the posterior odds are squared: a 3-to-1 call becomes 9-to-1. That is overconfidence from double counting in its purest form. With unequal priors the decisions can change too, because the prior term is no longer doubled.
Which is better? What the theory says
When the generative model is right, it is more efficient. If the classes really are Gaussian with a shared covariance, LDA is the full maximum-likelihood estimator, while logistic regression conditions on and discards the information in its distribution. Efron (1975) computed the asymptotic relative efficiency: for statistically interesting values of the parameters, logistic regression is "between one half and two thirds as effective as normal discrimination," so it needs roughly 1.5 to 2 times as many examples to match LDA's expected error rate.
When it is wrong, the discriminative model is safer. LDA assumes Gaussian classes; logistic regression assumes only that the log-odds are linear. That holds far more widely: any exponential family with a common base measure gives log-odds linear in the sufficient statistics . Outliers, skewed classes or binary features can distort LDA's means and covariance without touching the logistic form.
Two regimes. Ng & Jordan (2002) compared naive Bayes with logistic regression as a generative–discriminative pair and found that "while discriminative learning has lower asymptotic error, a generative classifier may also approach its (higher) asymptotic error much faster." In their analysis, naive Bayes needs a number of examples logarithmic in the number of features to get close to its asymptotic error, against a number linear in for logistic regression, so naive Bayes can win with little data and lose with a lot. Xue & Titterington (2008) cautioned that their own simulations and experiments did not support two distinct regimes in general, and that conclusions about one pair need not carry over to other generative–discriminative pairs.
Very high dimensions. When grows faster than , the sample covariance is singular and noisy, and Bickel & Levina (2004) showed that the naive Bayes independence rule can greatly outperform Fisher's discriminant, despite ignoring every correlation. Estimating fewer things badly can beat estimating more things very badly.
You must classify emails with only 30 labeled examples and a 5,000-word vocabulary. Which first choice is most defensible?
- Unregularized logistic regression on word counts, because discriminative models have lower asymptotic error
- Quadratic discriminant analysis, because it is the most flexible Gaussian model
- Multinomial naive Bayes with smoothing: few, separately estimated parameters that approach their limit quickly
- Linear discriminant analysis with the sample covariance
Show the answer
C. Multinomial naive Bayes with smoothing: few, separately estimated parameters that approach their limit quickly
With , the data are almost surely linearly separable, so unregularized logistic regression has no maximum-likelihood estimate, and the sample covariance needed by LDA and QDA is singular. Naive Bayes estimates each word's class rates on its own, a small-sample-friendly design, exactly the regime where generative models tend to win. A strongly regularized logistic regression would be the natural second candidate.
Key takeaways
- Generative classifiers model and and classify by Bayes' rule; discriminative ones model directly. For two classes the posterior log-odds equal the log-likelihood ratio plus the log prior ratio.
- Gaussian classes with a shared covariance give exactly a logistic posterior with (LDA, the direction of Fisher's discriminant); separate covariances give quadratic boundaries (QDA). The Bayes error is in the Mahalanobis distance .
- Naive Bayes assumes conditional independence; its Bernoulli and multinomial versions are linear classifiers in the features. It is often accurate under 0–1 loss despite a false assumption (Domingos & Pazzani), but its probabilities are overconfident.
- When the generative model is right it is more efficient (Efron: logistic regression is one half to two thirds as efficient as LDA); when it is wrong the discriminative model is safer. Naive Bayes can win with little data (Ng & Jordan), a pattern later shown not to be universal, and the independence rule beats Fisher's discriminant when (Bickel & Levina).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Two classes are Gaussian with means and a shared covariance . What is the form of ?
- A Gaussian density in
- with : a logistic regression
- A quadratic function of passed through the sigmoid
- A step function at the midpoint of the means, whatever is
Show the answer
B. with : a logistic regression
With a shared covariance the quadratic terms and determinants cancel in the log-likelihood ratio, leaving log-odds linear in . LDA and logistic regression share a functional form; they differ in how they estimate and .
Naive Bayes often classifies well even though its features are clearly dependent. What is the best explanation?
- Dependence between features cannot affect a classifier
- Naive Bayes detects and removes dependent features automatically
- Its probability estimates are accurate whenever its classifications are
- Under 0–1 loss only the ranking of the classes matters, and biased probability estimates often preserve it
Show the answer
D. Under 0–1 loss only the ranking of the classes matters, and biased probability estimates often preserve it
Domingos & Pazzani (1997) showed that naive Bayes is optimal under 0–1 loss far beyond the independence region, because classification only needs the most probable class on top. Its probabilities are usually overconfident, so the explanation covers decisions, not probabilities.
With features and classes, how many covariance parameters must QDA estimate?
- 300
- 5,050
- 15,150
- 30,000
Show the answer
C. 15,150
A symmetric covariance has free entries, and QDA needs one per class: . LDA shares one covariance (5,050), and Gaussian naive Bayes needs only the variances.
What did Ng & Jordan (2002) conclude about naive Bayes versus logistic regression?
- Logistic regression has lower asymptotic error, but naive Bayes can approach its own higher asymptotic error much faster, so it may win with little data
- Logistic regression is better at every training-set size
- Naive Bayes is better at every training-set size
- They always reach the same asymptotic error
Show the answer
A. Logistic regression has lower asymptotic error, but naive Bayes can approach its own higher asymptotic error much faster, so it may win with little data
Two regimes: with few examples naive Bayes (about samples to approach its limit, in their analysis) can beat logistic regression (about ), and with many examples logistic regression's lower asymptotic error wins. Later work cautioned that the pattern is not universal.
Further reading
- R. A. Fisher, "The use of multiple measurements in taxonomic problems," Annals of Eugenics 7(2), 179–188 (1936).
- B. Efron, "The efficiency of logistic regression compared to normal discriminant analysis," Journal of the American Statistical Association 70(352), 892–898 (1975).
- P. Domingos & M. Pazzani, "On the optimality of the simple Bayesian classifier under zero-one loss," Machine Learning 29, 103–130 (1997).
- A. Y. Ng & M. I. Jordan, "On discriminative vs. generative classifiers: a comparison of logistic regression and naive Bayes," Advances in Neural Information Processing Systems 14 (NIPS 2001; MIT Press, 2002), 841–848.
- P. J. Bickel & E. Levina, "Some theory for Fisher's linear discriminant function, 'naive Bayes', and some alternatives when there are many more variables than observations," Bernoulli 10(6), 989–1010 (2004).