S14 · Lesson 17 · Generative classifiers
Modeling each class's data instead of the boundary, Gaussian classes with a shared covariance giving a logistic posterior with linear weights, separate covariances giving quadratic boundaries, naive Bayes with its sum of log-likelihood ratios, and the parameter counts of each approach
Logistic regression models the class probability directly. The alternative is to model how each class generates its data, , and apply Bayes' theorem. For Gaussian classes sharing a covariance, the two approaches give exactly the same form of answer, a sigmoid of a linear function, but they fit it differently: the generative model estimates means and a covariance, which takes many more parameters but less data when its assumptions hold. Naive Bayes, the extreme generative model, still filters spam.
- Two classes in one dimension with Gaussian densities of shared variance σ², means 2 and 0, and prior P on class 1. Bayes' theorem gives the posterior of class 1 as a sigmoid of a linear function of x.
- Its weight is : well-separated, tight classes give a sharp transition. Its offset includes ln(P/(1 − P)).
- This is exactly the logistic regression form of lesson 16, derived from class models.
Predict
Classes 1 and 2 are Gaussian with means 2 and 0 and shared variance 1, equally likely. What posterior does class 1 get at x = 1.5?
The log of the likelihood ratio is , which is 1 at x = 1.5, so the posterior is σ(1) = 0.731. The quadratic terms cancel because the variances are equal; that's why the boundary is linear.
Name it
Shared covariance (LDA)
With for both classes, the posterior is with
In one dimension: and . The maximum-likelihood fit just needs class means, a pooled covariance and class frequencies.
Separate covariances (QDA)
With different covariances the quadratic terms don't cancel: the boundary is a conic.
Naive Bayes
Assume features are independent given the class. Then the log-odds is the prior log-odds plus a sum over features:
Each feature adds its evidence independently: fast, robust with little data, and often well ranked even when its probabilities are overconfident.
Parameter counts
In D dimensions: LDA needs 2D means, D(D + 1)/2 covariance entries and a prior; QDA needs D(D + 1) covariance entries; logistic regression needs only D + 1 weights.
Worked example
A spam filter's word A appears in 20% of spam and 1% of good mail; word B in 5% of spam and 10% of good mail. Spam is 30% of traffic. A message contains both words. What's its spam probability under naive Bayes?
Show the solution
- Add the log-odds.
- Probability.
- Read the terms. Word A is strong evidence for spam, word B weak evidence against, and the prior counts against too.
Answer under naive Bayes the message is spam with probability 0.811.
Worked example
How many parameters do LDA, QDA and logistic regression need with 10 features?
Show the solution
- LDA. 20 means, 55 covariance entries and 1 prior:
- QDA.
- Logistic regression. It needs 11.
- Why it matters. The generative models must describe the data's full shape; the discriminative one only the boundary, so with plenty of data and a wrong class model, logistic regression usually wins.
Answer LDA needs 76 parameters, QDA 131 and logistic regression 11.
Worked example
Two equally likely classes are both centered at 0, but class 1 has σ = 1 and class 2 has σ = 2. Where are the decision boundaries, and what's the Bayes error?
Show the solution
- Boundaries. The densities are equal where so = 1.848 and |x| = 1.36.
- Decision regions. Class 1 wins between −1.36 and 1.36 and class 2 outside: a quadratic boundary in one dimension is two thresholds, something no linear classifier can produce.
- Bayes error.
- What it shows. The classes overlap heavily, but the shape difference alone is still informative.
Answer the boundaries are at x = ±1.36, and the Bayes error is 0.339.
Check
Gaussian classes with means 1 and 0 share variance 3². What weight does the posterior's linear function put on x?
Check
For those classes (means 1 and 0, variance 2²) with class 1's prior 0.9, what's the offset ?
Check
Equally likely Gaussian classes with means 1 and 0 and variance 2²: what's the posterior of class 1 at x = 1.5?
Check
Spam has prior 0.3. A message has two features whose likelihood ratios (spam over good) are 5 and 0.8. What's its naive Bayes spam probability?
Check
How many parameters does a shared-covariance Gaussian classifier (two classes) need in 5 dimensions?
Check
And with a separate covariance for each class (QDA), in 5 dimensions?
Check
Two equally likely zero-mean Gaussian classes have standard deviations 0.5 and 1.5.
(a)
At what |x| are their densities equal?
(b)
What is the Bayes error?
Check
An event has prior probability p; two conditionally independent features have likelihood ratios a and b.
Step 1
What are the prior odds?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What are the posterior odds?
Step 3
What is the posterior probability?
Explain why
When might a generative classifier beat logistic regression, and when the reverse?
A generative classifier fits the full distribution of each class, so it has many more parameters, but it also uses more of the data's structure: when its assumptions are close to true, such as Gaussian classes with a shared covariance, its estimates converge with fewer examples, and it can cope with missing features. Logistic regression fits only the boundary, with D + 1 parameters, and makes no assumption about how the inputs are distributed. With plenty of data and a class model that's wrong, logistic regression's fewer, more targeted assumptions win; with little data and roughly correct class models, the generative model's extra structure wins.
Practice
Keep practicing
10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.