S1417 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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, p(𝐱|𝒞k), 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 (μ1−μ2)/σ2: 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?

Name it

Shared covariance (LDA)

With p(𝐱|𝒞k)=𝒩(𝝁k,Σ) for both classes, the posterior is σ(𝐰T𝐱+w0) with

𝐰=Σ−1(𝝁1−𝝁2),w0=−12𝝁1TΣ−1𝝁1+12𝝁2TΣ−1𝝁2+ln⁡P1P2

In one dimension: w=(μ1−μ2)/σ2 and w0=−(μ12−μ22)/(2σ2)+ln⁡(P1/P2). 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:

ln⁡p(𝒞1|𝐱)p(𝒞2|𝐱)=ln⁡P1P2+∑iln⁡p(xi|𝒞1)p(xi|𝒞2)

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
  1. Add the log-odds. a=ln⁡(0.3/0.7)+ln⁡(0.2/0.01)+ln⁡(0.05/0.1)=−0.8473+2.9957−0.6931=1.455
  2. Probability. p=σ(1.455)=0.811
  3. 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
  1. LDA. 20 means, 55 covariance entries and 1 prior: 20+55+1=76
  2. QDA. 20+110+1=131
  3. Logistic regression. It needs 11.
  4. 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
  1. Boundaries. The densities are equal where x22(1−14)=ln⁡2 so x2 = 1.848 and |x| = 1.36.
  2. 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.
  3. Bayes error. Pe=12P(|x|>1.36∣σ=1)+12P(|x|<1.36∣σ=2)=0.087+0.252=0.339
  4. 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 w0?

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

Explain why

When might a generative classifier beat logistic regression, and when the reverse?

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.