---
title: Generative classifiers
heading: 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
eyebrow: S14 · Lesson 17 · Generative classifiers
xp: 50
---

::: hook
Logistic regression models the class probability directly. The alternative is to model how each class generates its data, $p(\mathbf{x}|\mathcal{C}_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.
:::

::: play
- 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 $(\mu_1 - \mu_2)/\sigma^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.
:::

```sim plot
view: graph
x: x = -3..5
y: p = 0..1.05
slider: s = 0.3..3 step 0.1 = 1
slider: P = 0.05..0.95 step 0.01 = 0.5
plot: 1/(1 + exp(-(2/s^2*x - 2/s^2 + ln(P/(1 - P))))) | samples: 400 | label: P(class 1 | x)
readout: weight = 2/s^2 | digits: 3
readout: boundary = 1 - s^2*ln(P/(1 - P))/2 | digits: 3
```

::: 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?

- [ ] 0.5
- [ ] 0.6
- [x] About 0.73
- [ ] 1.0

::: reveal
The log of the likelihood ratio is $[-(x - 2)^2 + x^2]/2 = 2x - 2$, 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.
:::
:::

::: nameit
## Shared covariance (LDA)

With $p(\mathbf{x}|\mathcal{C}_k) = \mathcal{N}(\boldsymbol{\mu}_k, \Sigma)$ for both classes, the posterior is $\sigma(\mathbf{w}^T\mathbf{x} + w_0)$ with

$$\mathbf{w} = \Sigma^{-1}(\boldsymbol{\mu}_1 - \boldsymbol{\mu}_2), \qquad w_0 = -\tfrac{1}{2}\boldsymbol{\mu}_1^T\Sigma^{-1}\boldsymbol{\mu}_1 + \tfrac{1}{2}\boldsymbol{\mu}_2^T\Sigma^{-1}\boldsymbol{\mu}_2 + \ln\frac{P_1}{P_2}$$

In one dimension: $w = (\mu_1 - \mu_2)/\sigma^2$ and $w_0 = -(\mu_1^2 - \mu_2^2)/(2\sigma^2) + \ln(P_1/P_2)$. 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\frac{p(\mathcal{C}_1|\mathbf{x})}{p(\mathcal{C}_2|\mathbf{x})} = \ln\frac{P_1}{P_2} + \sum_i\ln\frac{p(x_i|\mathcal{C}_1)}{p(x_i|\mathcal{C}_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.
:::

::: 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?

::: solution
1. **Add the log-odds.**
   $$\begin{aligned} a &= \ln(0.3/0.7) \\ &\quad + \ln(0.2/0.01) \\ &\quad + \ln(0.05/0.1) \\ &= -0.8473 + 2.9957 - 0.6931 \\ &= 1.455 \end{aligned}$$
2. **Probability.**
   $$\begin{aligned} p &= \sigma(1.455) \\ &= 0.811 \end{aligned}$$
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.
:::
:::

::: example
## How many parameters do LDA, QDA and logistic regression need with 10 features?

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

::: 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?

::: solution
1. **Boundaries.** The densities are equal where
   $$\frac{x^2}{2}\left(1 - \frac14\right) = \ln 2$$
   so $x^2$ = 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.**
   $$\begin{aligned} P_e &= \tfrac12 P(|x| > 1.36 \mid \sigma = 1) \\ &\quad + \tfrac12 P(|x| < 1.36 \mid \sigma = 2) \\ &= 0.087 + 0.252 \\ &= 0.339 \end{aligned}$$
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
given: m = 1 | 3; n = 0 | -1; s = 0.5 | 2 | 3
answer: (m - n)/s^2
tolerance: 0.5%

## Gaussian classes with means {m} and {n} share variance {s}². What weight does the posterior's linear function put on x?

::: right
Right. $(\mu_1 - \mu_2)/\sigma^2$ = {answer}.
:::

::: misses
- [(m - n)/s] Divide by the variance.
- [any] Not yet. The x coefficient of the log likelihood ratio.
:::
:::

::: check
given: m = 1 | 3; n = 0 | -1; s = 0.5 | 1 | 2; P = 0.2 | 0.7 | 0.9
answer: -(m^2 - n^2)/(2*s^2) + ln(P/(1 - P))
tolerance: 0.5%

## For those classes (means {m} and {n}, variance {s}²) with class 1's prior {P}, what's the offset $w_0$?

::: right
Right. $-(\mu_1^2 - \mu_2^2)/(2\sigma^2) + \ln(P_1/P_2)$ = {answer}.
:::

::: misses
- [-(m^2 - n^2)/(2*s^2)] Include the log prior ratio.
- [any] Not yet. Constant terms of the log likelihood ratio, plus the priors.
:::
:::

::: check
given: m = 1 | 3; n = 0 | -1; s = 0.5 | 1 | 2; x = 0.5 | 1.5
answer: 1/(1 + exp(-((m - n)/s^2*x - (m^2 - n^2)/(2*s^2))))
tolerance: 0.5%

## Equally likely Gaussian classes with means {m} and {n} and variance {s}²: what's the posterior of class 1 at x = {x}?

::: right
Right. $\sigma(wx + w_0)$ = {answer}.
:::

::: misses
- [any] Not yet. Build w and $w_0$, then the sigmoid.
:::
:::

::: check
given: p = 0.1 | 0.3 | 0.8; a = 5 | 20; b = 0.3 | 0.8
answer: 1/(1 + exp(-(ln(p/(1 - p)) + ln(a) + ln(b))))
tolerance: 0.5%

## Spam has prior {p}. A message has two features whose likelihood ratios (spam over good) are {a} and {b}. What's its naive Bayes spam probability?

::: right
Right. σ(prior log-odds + Σ log ratios) = {answer}.
:::

::: misses
- [1/(1 + exp(-(ln(a) + ln(b))))] Include the prior's log-odds.
- [any] Not yet. Add the log-odds contributions.
:::
:::

::: check
given: D = 2 | 5 | 20
answer: 2*D + D*(D + 1)/2 + 1
tolerance: 0.5%

## How many parameters does a shared-covariance Gaussian classifier (two classes) need in {D} dimensions?

::: right
Right. $2D + D(D + 1)/2 + 1$ = {answer}.
:::

::: misses
- [D + 1] That's logistic regression's count.
- [any] Not yet. Two means, one symmetric covariance, one prior.
:::
:::

::: check
given: D = 2 | 5 | 20
answer: 2*D + D*(D + 1) + 1
tolerance: 0.5%

## And with a separate covariance for each class (QDA), in {D} dimensions?

::: right
Right. $2D + D(D + 1) + 1$ = {answer}.
:::

::: misses
- [2*D + D*(D + 1)/2 + 1] That's the shared-covariance count; each class now has its own.
- [any] Not yet. Two symmetric covariances.
:::
:::

::: check
kind: parts
given: s = 0.5 | 1; u = 1.5 | 2 | 3

## Two equally likely zero-mean Gaussian classes have standard deviations {s} and {u}.

::: part
answer: sqrt(2*ln(u/s)/(1/s^2 - 1/u^2))
tolerance: 0.5%

### At what |x| are their densities equal?

::: misses
- [sqrt(ln(u/s)/(1/s^2 - 1/u^2))] The exponents carry a factor 1/2.
- [any] $x^2(1/\sigma_1^2 - 1/\sigma_2^2)/2 = \ln(\sigma_2/\sigma_1)$.
:::
:::

::: part
answer: 0.5*erfc(sqrt(2*ln(u/s)/(1/s^2 - 1/u^2))/s/sqrt(2)) + 0.5*(1 - erfc(sqrt(2*ln(u/s)/(1/s^2 - 1/u^2))/u/sqrt(2)))
tolerance: 0.5%

### What is the Bayes error?

::: misses
- [any] Half of each class's probability on the wrong side.
:::
:::
:::

::: check
kind: derivation
vars: p = 0.01..0.99, a = 0.1..20, b = 0.1..20

## An event has prior probability p; two conditionally independent features have likelihood ratios a and b.

::: step
answer: p/(1 - p)

### What are the prior odds?
:::

::: step
answer: p/(1 - p)*a*b

### What are the posterior odds?
:::

::: step
answer: p*a*b/(p*a*b + 1 - p)

### What is the posterior probability?
:::

::: right
Right. In odds form, naive Bayes multiplies one ratio per feature, which is why it's usually computed as a sum of log ratios.
:::
:::

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

::: model
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

::: check
given: m = 0.5 | 5; n = -2 | 0; s = 0.3 | 3
answer: (m - n)/s^2
tolerance: 0.5%

## LDA weight: μ₁ = {m}, μ₂ = {n}, σ = {s}?

::: misses
- [any] $(\mu_1 - \mu_2)/\sigma^2$.
:::
:::

::: check
given: p = 0.01 | 0.6; a = 2 | 50; b = 0.1 | 3
answer: 1/(1 + exp(-(ln(p/(1 - p)) + ln(a) + ln(b))))
tolerance: 0.5%

## Naive Bayes: prior {p}, ratios {a} and {b}?

::: misses
- [any] σ(sum of log-odds).
:::
:::

::: check
given: D = 2 | 3 | 5 | 10 | 20 | 50
answer: 2*D + D*(D + 1)/2 + 1
tolerance: 0.5%

## LDA parameters in {D} dimensions?

::: misses
- [any] $2D + D(D + 1)/2 + 1$.
:::
:::
:::
