Foundations to Frontier
Tests
H. Generative Models › H1 Foundations

Generative vs discriminative models

A discriminative model learns p(y | x) directly: it only needs to tell classes apart. A generative model learns how the data themselves arise, p(x | y) and p(y) or simply p(x), and gets p(y | x) from Bayes' rule. It can therefore also sample new data, score how typical an input is and cope with missing inputs. This page sets out the two views, the classic pairs (naive Bayes vs logistic regression, LDA vs logistic regression), the Ng and Jordan (2001) result on sample efficiency vs asymptotic error, and how modern generative models (diffusion, autoregressive transformers) blur the line again. In the exercises you fit Bernoulli naive Bayes, apply Bayes' rule and sample digits from it. You then compare both model types on synthetic 8×8 digits. When the naive Bayes assumption held, it was ahead at 500 examples (error 0.276 vs 0.311) and both ended near 0.25. When glyphs varied in width and position, logistic regression won by a factor of three to four (0.064 vs 0.223). With half of the pixels missing, naive Bayes marginalised them and kept the lower error (0.425 vs 0.474).

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Learning the data vs learning the boundary

Suppose you must tell satellite photos of cloud from photos of snow. One approach studies what cloud images look like and what snow images look like, then asks which story explains a new photo better: that is a generative model. The other approach only learns the line that separates the two piles of photos: that is a discriminative model. The first can also invent new photos and notice when a photo looks like neither; the second is usually more accurate when there is lots of data. The demo shows two class densities and the posterior probability that Bayes’ rule makes from them.

Generative models lab
Class densities weighted by priors (red class 0, green class 1) and the posterior p(1 | x) in yellow (scaled). Bayes' rule gives a logistic posterior with w = 2.00, b = 0.00; the boundary is at x = 0.00. A discriminative model fits w and b directly without modelling the densities.
Why engineers care

Generative models of sensor data can flag inputs unlike anything seen in training, fill in dropped telemetry channels and simulate rare fault conditions, which pure classifiers cannot do.

📝 Quick check (Level 0)
0/3 answered
  1. 1. A discriminative model learns…
  2. 2. A generative classifier predicts with…
  3. 3. Which can sample new inputs?

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Discriminative: model p(y | x) (logistic regression, SVMs in spirit, most deep classifiers); trained by maximising the conditional likelihood Σ log p(yᵢ | xᵢ).
  • Generative: model the joint p(x, y) = p(y) p(x | y) (naive Bayes, LDA/QDA, Gaussian mixtures, HMMs) or just p(x) (unconditional image, audio and text models); classify by Bayes’ rule p(y | x) ∝ p(x | y) p(y).
  • What a generative model buys: sampling; density scores for outlier detection; marginalising missing features; using unlabelled data (semi-supervised EM); class-conditional generation.
  • What it costs: you must model x, which is usually much harder than the boundary; wrong assumptions about p(x | y) bias the classifier.
  • Classic pairs: Gaussian class-conditionals with shared covariance give exactly a logistic posterior, so LDA and logistic regression share the same model family for p(y | x) but fit it differently; Bernoulli naive Bayes and logistic regression likewise.
  • Ng and Jordan (2001): the generative member of the pair approaches its (higher) asymptotic error with O(log d) examples, the discriminative member needs O(d) but reaches a lower asymptote when the generative assumptions are wrong.
  • Today (2026): large generative models (diffusion, autoregressive transformers) are used as classifiers (diffusion classifiers, 2023) and discriminative networks as energy models (JEM, 2020); the distinction is about what is modelled and optimised, not about architecture.
📝 Quick check (Level 1)
0/3 answered
  1. 1. LDA's posterior has the form of…
  2. 2. Naive Bayes assumes features are…
  3. 3. Ng and Jordan: the generative model typically wins…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Building both kinds of classifier

Exercise 1.

  • Fit Bernoulli naive Bayes with add-α smoothing.
  • Compute log p(x, c) and the posterior by Bayes’ rule.
  • Sample new images from a class.

Practice.

  • In scikit-learn: BernoulliNB, GaussianNB, LinearDiscriminantAnalysis for the generative side; LogisticRegression for the discriminative side.
  • Smooth counts (α = 1) so that an unseen pixel value does not veto a class.
  • Work in log space and normalise with log-sum-exp.
  • For missing features, a generative model simply drops their terms; a discriminative model needs imputation.
  • Compare on learning curves (error vs training size), not on one split.
from sklearn.naive_bayes import BernoulliNB
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import learning_curve
for model in (BernoulliNB(alpha=1.0), LogisticRegression(max_iter=1000)):
    n, _, test = learning_curve(model, X, y, train_sizes=[30, 100, 500, 2000], cv=5)
    print(type(model).__name__, 1 - test.mean(1))
🐍 Exercise 1: naive Bayes as a generative model
Fit Bernoulli naive Bayes, compute the joint log-likelihood and the posterior, and sample images of one class.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Why smooth naive Bayes counts?
  2. 2. Missing feature in naive Bayes:
  3. 3. Normalising log-joints uses…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Same family, different fit. With Gaussian classes N(μ_c, σ²) sharing σ², log p(1 | x) − log p(0 | x) = w x + b with w = (μ₁ − μ₀)/σ² and b = (μ₀² − μ₁²)/(2σ²) + log(π₁/π₀). Logistic regression searches the same family of posteriors directly. Bernoulli naive Bayes likewise gives a posterior that is linear in x.

Two objectives. Since log p(x, y) = log p(y | x) + log p(x), the generative fit maximises Σ log p(yᵢ | xᵢ) plus Σ log p(xᵢ). The extra term ties the parameters to the input distribution: if the model of x is right, it is extra information (faster learning); if wrong, it pulls the boundary away from the best one (worse asymptote).

Rates (Ng and Jordan, 2001). For d binary features, naive Bayes parameters each converge independently, so O(log d) examples bring it close to its asymptotic error; logistic regression’s error converges like O(√(d/n)), needing O(d) examples, but its asymptotic error is never worse than naive Bayes’ within the same family.

model models trains by strengths
naive Bayes, LDA p(x | y), p(y) counting / closed form small data, missing inputs, sampling
logistic regression p(y | x) convex optimisation lower asymptotic error, fewer assumptions
📝 Quick check (Level 3)
0/3 answered
  1. 1. The generative objective adds…
  2. 2. Logistic regression error converges in about…
  3. 3. If the model of x is wrong, the generative classifier's asymptote is…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

When does a generative classifier beat a discriminative one, and what can it do that the other cannot?

History.

  • Y. D. Rubinstein and T. Hastie, “Discriminative vs informative learning” (1997).
  • A. Y. Ng and M. I. Jordan, “On discriminative vs. generative classifiers: a comparison of logistic regression and naive Bayes” (NeurIPS 2001).
  • J. A. Lasserre, C. M. Bishop and T. Minka, “Principled hybrids of generative and discriminative models” (2006).
  • W. Grathwohl et al., “Your classifier is secretly an energy based model” (JEM, 2020); A. C. Li et al., “Your diffusion model is secretly a zero-shot classifier” (2023).

Exercise 2. Synthetic 8×8 seven-segment digits, 10 classes, 2000 test images.

  • Regime A (naive Bayes holds): fixed glyph, each pixel flipped independently with probability 0.15, which is exactly the naive Bayes model.
  • Regime B (misspecified): glyph width 3 or 4 and position shifted, 2% flips; pixels are now strongly dependent given the class.
  • Models: Bernoulli naive Bayes (α = 1) and softmax logistic regression (L2 1e-3, 150 Adam steps), trained on the first n examples.
  • Missing pixels: at test time half the pixels are hidden; naive Bayes drops them, logistic regression gets the training mean.
n A: NB error A: LR error B: NB error B: LR error
10 0.732 0.711 0.832 0.827
30 0.552 0.557 0.816 0.690
100 0.374 0.414 0.615 0.257
500 0.276 0.311 0.414 0.073
2000 0.250 0.252 0.223 0.064
half the pixels missing (n 2000) 0.425 0.474 0.483 0.538
  • Correct assumptions: naive Bayes was ahead at moderate n (100: 0.374 vs 0.414; 500: 0.276 vs 0.311) and both met near 0.25, as Ng and Jordan predict.
  • Wrong assumptions: logistic regression was three to four times better at n 2000 (0.064 vs 0.223), and was already far ahead at n 100.
  • Small n: the expected naive Bayes advantage at 10 and 30 examples did not appear; both were near chance with one to three examples per class. This is a null result for the smallest sizes.
  • Missing inputs: marginalising beat mean imputation in both regimes.
  • Seeds 1 and 2: A at n 500 0.262 vs 0.291 and 0.270 vs 0.315; B at n 2000 0.220 vs 0.070 and 0.230 vs 0.063; A missing 0.423 vs 0.482 and 0.429 vs 0.468.
  • Caveat: linear models only; deep discriminative networks would handle the shifts in regime B even better, and deep generative classifiers are far costlier to evaluate.
🐍 Exercise 2 (research): when does a generative classifier beat a discriminative one, and what can it do that the other cannot?
Compare Bernoulli naive Bayes and logistic regression on synthetic digits across training-set sizes, with correct and with wrong generative assumptions, and with missing pixels.

Generative vs discriminative in AI, robotics and aerospace

  • Fault detection: a generative model of nominal telemetry flags anomalies by low likelihood; a classifier needs labelled faults.
  • Sensor dropout: generative models marginalise or impute lost channels on spacecraft and robots.
  • Perception: most deployed detectors are discriminative; generative models supply synthetic training data and uncertainty.

References

  1. A. Y. Ng and M. I. Jordan (2001). “On discriminative vs. generative classifiers: a comparison of logistic regression and naive Bayes.” NeurIPS.
  2. Y. D. Rubinstein and T. Hastie (1997). “Discriminative vs informative learning.” KDD.
  3. J. A. Lasserre, C. M. Bishop and T. Minka (2006). “Principled hybrids of generative and discriminative models.” CVPR.
  4. W. Grathwohl et al. (2020). “Your classifier is secretly an energy based model and you should treat it like one.” ICLR.
  5. A. C. Li et al. (2023). “Your diffusion model is secretly a zero-shot classifier.” ICCV.