Exercises — Generative vs discriminative models
Before we start, one figure to keep in your head the whole time — it shows the two "shapes of question" each model asks.

How to read this figure: the horizontal axis is a single feature (say, "how spammy one word looks" — one number describing the data point). The vertical axis is probability density — how likely each class is to produce that feature value. The blue and green bumps are a generative model: it learns the full shape of each class's data. The red vertical line is a discriminative model: it only cares about the wall between classes, and here that wall falls out exactly where the two bumps cross (equal density → the classes are equally plausible).
Level 1 — Recognition
Exercise 1.1 (L1)
For each model, write whether it is generative (G) or discriminative (D): (a) Logistic Regression, (b) Naive Bayes, (c) a plain feed-forward neural net with softmax output, (d) a Gaussian Mixture Model, (e) a Support Vector Machine.
Recall Solution 1.1
(a) D — models straight through the sigmoid. See 3.3.01-Logistic-Regression. (b) G — models and , then applies Bayes. See 4.5.04-Naive-Bayes-Classifier. (c) D — softmax outputs directly. (d) G — models the density (as a mixture of bumps). See 4.5.05-Gaussian-Mixture-Models. (e) D — learns a boundary (a hyperplane maximizing margin), no data density.
Exercise 1.2 (L1)
State, in words, what quantity each side learns: (a) discriminative learns , (b) generative learns and .
Recall Solution 1.2
(a) The posterior directly. (b) The likelihood and the prior . From these it reconstructs the posterior via Bayes' theorem.
Level 2 — Application
Exercise 2.1 (L2)
A Naive Bayes spam filter learned these numbers from training. Prior: , . For the single word "free": An email contains the word "free". Compute .
Recall Solution 2.1
WHAT we do: apply Bayes with a two-term denominator (marginalize over spam and ham). WHY: the email could have come from either class; we sum both possibilities for the evidence . Numerator: . Denominator: . So one word "free" pushes belief from the prior up to about .
Exercise 2.2 (L2)
Same filter. A new email contains both "free" and "meeting", with Using the Naive Bayes independence assumption, compute .
Recall Solution 2.2
WHAT: multiply per-word likelihoods (Naive Bayes assumes features independent given the class). WHY the product: — this is the "naive" step. It is independence conditional on , not overall. Spam score: . Ham score: . The word "meeting" (strongly ham) overturned the earlier lean toward spam — belief drops to about .
Level 3 — Analysis
Exercise 3.1 (L3)
You have 50 labeled training images. You must classify into 3 classes. Argue which family (generative vs discriminative) is likely to generalize better here, and why, referencing the parameter-count / data trade-off.
Recall Solution 3.1
With only 50 examples, discriminative models are usually safer for pure classification. WHY: a full generative model must estimate the entire distribution for each class — for high-dimensional that can be a huge number of parameters (means, variances, and off-diagonal covariances). With 50 points you cannot estimate all of them reliably; the estimates are noisy and overfit. Important nuance: not every generative model is this heavy. Naive Bayes makes the conditional-independence assumption , which throws away all off-diagonal covariances and keeps only a mean (and maybe a variance) per feature per class. That drastically cuts the parameter count — often to the same order as a discriminative model. So the "generative needs more data" rule is really about how much structure the generative model tries to capture: a full-covariance Gaussian is data-hungry; Naive Bayes can actually be very data-efficient (sometimes beating logistic regression at tiny sample sizes). A discriminative model only needs to place the boundary, which is a small object (a hyperplane needs numbers, not a full density). Fewer parameters = it can be pinned down with less data. Caveat that shows real understanding: if many unlabeled images are also available, a generative model can exploit them (it can score on unlabeled data), flipping the advantage — see 4.6.01-Semi-Supervised-Learning.
Exercise 3.2 (L3)
The parent note claims generative models "handle missing features via marginalization". A patient record is missing the "blood-pressure" feature . Show mathematically how a generative model copes, and why a plain logistic-regression cannot do the same trick natively.
Recall Solution 3.2
A generative model owns . If is missing, we integrate it out: WHAT this is: summing the joint density over every possible value the missing feature could have taken. WHY it works: because the generative model knows the joint distribution, "not knowing " is expressible as "average over ". For Naive Bayes it's even simpler — the missing factor just drops out of the product . Logistic regression only owns the conditional , a single function that requires all inputs to be present to compute . It has no distribution over to average against, so a missing feature breaks the dot product — you must impute a value first, which is an outside patch, not a native capability.
Level 4 — Synthesis
Exercise 4.1 (L4)
Show that Gaussian Naive Bayes with a shared variance across both classes (binary , one feature ) produces a posterior of exactly the logistic (sigmoid) form. This links a generative model to a discriminative one.
Recall Solution 4.1
Start from Bayes for the binary case: WHY this rewrite: dividing top and bottom by the term exposes a single ratio — that ratio is what will become the sigmoid's exponent. Now let . WHY the normalizing constants cancel: the prefactor is identical for both classes because they share the same . So when we form the ratio , the two prefactors are the same number top and bottom and divide out exactly — leaving only the ratio of the exponentials. (If differed between classes, the prefactors would not match and would survive as an extra constant — this is exactly what breaks the clean logistic form; see the edge-case note below.) With the prefactors gone, We want the whole posterior in the form , so the denominator's exponent must equal . The denominator is . Write the prior ratio as so the whole denominator becomes where Expand: , which is linear in . Dividing by and matching coefficients of and the constant to : (The prior term is — note the sign: the that appeared in flips when we solve for itself.) Substituting back: The punchline: a generative Gaussian model, under the shared-variance assumption, yields exactly the discriminative logistic form. They are two doors into the same room — but the generative one made an extra assumption (Gaussian, shared ) to get there, while logistic regression fits freely. See 3.3.01-Logistic-Regression.
Edge case — unequal variances: this whole result hinges entirely on the shared-variance assumption. If , two things break: (1) the prefactors no longer cancel, and (2) the terms in no longer cancel either — leaving an term in the exponent. The posterior is then not a simple logistic but a sigmoid of a quadratic , giving a curved (possibly two-piece) boundary. Exercise 5.1 works exactly this unequal-variance case.
Exercise 4.2 (L4)
A GAN contains a discriminator and a generator. Classify each sub-network as generative or discriminative and explain why the whole system is called generative.
Recall Solution 4.2
- Generator: generative — it learns to produce samples , i.e. it models (implicitly) by mapping noise data.
- Discriminator: discriminative — it learns , a boundary between real and fake. The system is called generative because its deliverable is new data from the generator; the discriminator is merely a training critic. This is why the parent note lists GANs as a "hybrid": the learning signal comes from a discriminative component, but the goal is generation. Contrast with VAEs, which are generative through an explicit likelihood.
Level 5 — Mastery
Exercise 5.1 (L5)
A generative classifier over one continuous feature uses class-conditional Gaussians with different variances: (a) Find every value of where (the decision boundary). (b) Describe the shape of the region classified as class 1, in interval notation. Cover all cases.
Recall Solution 5.1
(a) The boundary is where the two weighted likelihoods are equal (priors are equal, so they cancel): Take logs (monotone, safe): Collect the terms on one side and constants on the other: Left side . So . Solving this quadratic in gives , hence two threshold points (b) The boundary condition is quadratic in , and solving that quadratic yields the two threshold points above. These two thresholds carve the line into three pieces. The class-1 region is the union of the outer two pieces (a disconnected region). Why each piece belongs where it does: the wider Gaussian (class 1, variance 4) decays slowly, so it dominates in the tails; the narrower one (class 0) is tall near the center, so it dominates there. Writing , the explicit regions are:
- Class 0: , i.e. the open interval — the center belongs to the tall, narrow bump.
- Class 1: or , i.e. — the two tails belong to the flat, wide bump.
So class 1's region is two half-lines — not a single interval. That disconnectedness is the whole point: unequal variances give curved boundaries with possibly two crossings, unlike the single-line boundary of Exercise 4.1.
The figure below traces exactly this: the two red dashed lines are the thresholds , the blue-shaded center is class 0, and the two green-shaded tails are the disconnected class-1 region.

Exercise 5.2 (L5)
Fill the decision table: for a task, choose G or D and justify in one line each. (a) You must generate synthetic training images. (b) You have 10 million labeled tweets and only need sentiment labels. (c) You have 200 labeled + 100k unlabeled molecules. (d) At test time some sensors randomly fail (missing features).
Recall Solution 5.2
(a) G — only generative models can synthesize (VAE/GAN). (b) D — abundant labels + pure classification → the sample-efficient, boundary-focused choice wins asymptotically. (c) G — the huge unlabeled pool can be scored under ; this is exactly 4.6.01-Semi-Supervised-Learning's home turf. (d) G — missing features are handled natively by marginalizing them out of (Exercise 3.2).
Recall Quick self-test
Which side of the boundary owns the tails when class variances differ? ::: The class with the larger variance (its density decays slower, so it wins far from the mean). A generative Gaussian with shared variance reduces to which discriminative model? ::: Logistic regression (sigmoid posterior). Why can generative models use unlabeled data? ::: They model , so unlabeled points still contribute to fitting the density.
Connections
- Parent topic
- 4.1.01-Bayes-Theorem — engine behind every L2 computation
- 4.5.04-Naive-Bayes-Classifier — the L2/L4 generative example
- 3.3.01-Logistic-Regression — the L4 discriminative limit
- 4.5.05-Gaussian-Mixture-Models — densities as sums of bumps (L1, L5)
- 4.5.02-Variational-Autoencoders, 4.5.03-Generative-Adversarial-Networks — L4/L5 synthesis
- 4.6.01-Semi-Supervised-Learning — the L3/L5 unlabeled-data advantage
#flashcards/ai-ml