- A generative classifier models P(x | y) and the prior P(y), then applies Bayes rule.
- Gaussian Naive Bayes uses one Gaussian per feature and assumes features are independent given the class.
- GDA uses a full covariance; shared covariance gives a linear boundary, class-specific covariance a quadratic one.
This lesson follows How Good Is the Model? and leads into Support Vector Machines & Kernels. The probability background is in Probability & Statistics.
1. Generate, then classify
A discriminative classifier learns the boundary directly. A generative classifier tells a story about how the data could have been produced: pick a class according to its prior, then draw features from that class’s distribution. At prediction time it asks which class’s story makes the observed features more likely.
This sounds indirect, but it has advantages. It can work with little data, handle missing features naturally, and produce a probability model. It also exposes its assumptions, which is both a strength and a failure mode.
Before reading the formulas, look at the two classes below. Which class would you assign to x = 1.5? The answer depends on both the likelihoods and the priors.
2. Bayes rule
Bayes rule turns a class-conditional likelihood into a posterior over classes:
P(y) is the prior, P(x | y) is the likelihood, and P(y | x) is the posterior. The denominator P(x) is the same for every class, so it only normalizes; it does not change the ranking.
A generative classifier picks the class with the largest posterior. With two classes, it is enough to compare the log posterior ratio, which turns products into sums and avoids underflow.
3. Gaussian likelihood
If a feature is continuous, a common model is a Gaussian with a class-specific mean and variance:
μ moves the bell curve; σ² controls its width. The Gaussian is a modeling choice, not a law. A feature with two peaks or a hard boundary may be poorly served by one bell curve.
The log of the Gaussian is easier to add: a constant minus a squared distance divided by twice the variance. A point far from the mean gets a low likelihood, and a small variance makes the penalty steeper.
4. Naive Bayes
With several features, modeling the full joint distribution is expensive. Naive Bayes makes the strong assumption that features are conditionally independent given the class:
“Naive” refers to that independence assumption, not to the arithmetic. Each feature gets its own one-dimensional model, so the number of parameters grows linearly with the number of features. The model can be trained from small counts and updated one feature at a time.
When features are correlated, the model double-counts evidence. The ranking can still be useful, but the probabilities are often overconfident. That is why a naive Bayes probability should not be read as a calibrated risk without checking.
5. Worked example
Use one feature, equal priors, and two Gaussians: class 0 has mean 0 and variance 1; class 1 has mean 2 and variance 1. At x = 1.5, the likelihoods are:
Worked example: posterior at x = 1.5
| quantity | class 0 | class 1 |
|---|---|---|
| prior | 0.5 | 0.5 |
| log likelihood | -2.044 | -1.044 |
| likelihood | 0.1295 | 0.3521 |
| prior × likelihood | 0.0648 | 0.1760 |
| posterior | 0.269 | 0.731 |
The log likelihood difference is 1, so the posterior ratio is e¹. With equal priors, the class-1 posterior is e¹/(1+e¹) ≈ 0.731. The boundary is at x = 1, where the two Gaussians have equal density.
Note that 0.731 is a posterior under this model. It is not a statement that 73.1% of all x = 1.5 examples are class 1 in the real world.
With two features, the same calculation adds two log likelihoods. With d features it adds d terms. The independence assumption is what makes the sum cheap.
6. GDA and QDA
Gaussian Discriminant Analysis models each class with a multivariate Gaussian. It keeps the correlations between features instead of forcing them to zero:
If every class shares the same covariance matrix, the quadratic terms cancel and the boundary is linear. This is linear discriminant analysis (LDA). If each class has its own covariance, the boundary can curve; this is quadratic discriminant analysis (QDA). If the covariances are diagonal, QDA reduces to Gaussian Naive Bayes.
The trade-off is bias and variance. LDA has fewer parameters and is more stable with small samples. QDA is more flexible but needs more data per class to estimate each covariance matrix. A full covariance in d dimensions has O(d²) parameters.
Why can a shared covariance matrix produce a linear boundary while class-specific covariances produce a quadratic one?
7. Where it fails
Correlated features break the naive independence assumption. A Gaussian may be a poor fit for counts, categories, or multimodal features. Zero variance in a feature can make a likelihood explode or vanish, so implementations add smoothing or a variance floor.
Generative models can be badly calibrated when their assumptions are wrong. A high posterior is only as good as the model that produced it. Check calibration and held-out performance rather than trusting the probability surface.
The next lesson takes a different route: instead of modeling each class, it looks for the boundary with the largest margin.
Next on the recommended path: Support Vector Machines & Kernels
8. Going deeper
The Gaussian discriminant is a quadratic classifier in disguise. Comparing two log posteriors gives a quadratic function of x; the sign of the quadratic coefficient determines whether the boundary is a line, a parabola, or a more complex surface in higher dimensions.
Regularized discriminant analysis shrinks class covariance matrices toward a shared or diagonal target. Shrinkage is especially useful when a class has fewer examples than features, where the empirical covariance is singular or unstable.
Discriminative classifiers often win when data are plentiful and the boundary is the only thing that matters. Generative classifiers can win when data are scarce or when the model’s assumptions are close to true. The useful question is which assumption is safer for the task.
9. Practice
Two classes have equal priors and the same variance but means 0 and 4. Where is the boundary?
Why is the naive Bayes posterior often overconfident when features are correlated?
What happens to the number of covariance parameters when you move from LDA to QDA with two classes and d features?
10. Resources
The scikit-learn Naive Bayes chapter covers Gaussian and multinomial variants. The LDA/QDA chapter derives the shared and class-specific covariance models. Both links were opened and checked for this lesson.
Continue the track
The mathematics behind this topic: Probability & statistics.