← All examples

Machine Learning: Generative vs Discriminative

This is the bridge from the rest of the collection into machine learning, and it is worth crossing deliberately. Almost everything else here is generative: the hierarchical models, the mixtures, the latent-variable models all write down a probability model for how the data were produced and reason backward. Machine learning usually goes discriminative — it models the thing you actually want and never bothers with a model of the features at all.

Classification is where the two philosophies meet head-on, and they make a genuine trade rather than one being better. Committing to a model of the features buys low variance and fast convergence and costs asymptotic accuracy when the model is wrong. Committing to less is more robust and more data-hungry. Arranged by how much each method commits to, the classifiers form a ladder: generative models everything, logistic models the conditional, the SVM models only the boundary.

One thing to be clear about, because the pairing invites the opposite conclusion. Logistic regression is discriminative: it estimates p(y | x) directly and never models how the features are distributed. The confusion is understandable, though, because a generative model implies it — assume Gaussian class-conditionals with one shared covariance matrix, which is exactly linear discriminant analysis (LDA), and the posterior that falls out is precisely the logistic function. Same functional form, different estimator: LDA fits class means and a covariance and derives the boundary, while logistic fits the boundary coefficients directly by conditional likelihood and learns nothing about the features at all. That shared form with different fitting is what makes them a matched pair, and it is the whole reason the convergence comparison below is a fair one.

That ladder frames every classifier in the arc that follows, and it turns out to be a calibration story as well as a convergence one — with the ordering coming out differently from the way it is usually told.

The data. Both examples run on the same cross-section: 30,000 Taiwanese credit-card clients, 23 features, and a 22.1% default rate (Yeh & Lien, 2009; UCI). The features are a credit limit, four demographics — sex, education, marital status and age — and then eighteen columns of payment history: six months of repayment status (PAY_1PAY_6, coded in months of delay), six of bill amounts and six of payments made. The target is whether the client defaults the following month.

It is a good dataset for this comparison for two reasons. The features are strongly dependent — the six bill amounts correlate at about 0.89, and repayment status persists from month to month — so Naive Bayes' conditional-independence assumption is emphatically false, which is exactly the condition under which the generative/discriminative trade-off is supposed to bite. And the classes are imbalanced and heavily overlapping, so ranking and thresholded accuracy come apart, which is what lets the miscalibration story surface at all. The same cross-section is used by the tree ensembles, calibration and interpretability sections, so results here can be read directly against those, and the support vector machine gets its full treatment in SVM and Kernel Methods.

Three numbers, three different questions. Every comparison below is reported in the same three metrics, and the whole point of the section is that they do not have to agree. AUC is the area under the ROC curvereceiver operating characteristic, a name inherited from wartime radar and worth ignoring; the curve plots the true-positive rate against the false-positive rate as the threshold sweeps from one extreme to the other. The area under it has a clean reading: it is the probability that a randomly chosen defaulter is scored above a randomly chosen non-defaulter. It asks only whether the ordering is right, so it never looks at a threshold and never looks at whether the numbers themselves mean anything; 0.5 is a coin flip. Accuracy is the fraction of correct calls once a threshold is fixed, conventionally at 0.5, and it is therefore a statement about a decision rather than about the model. It has to be read against what you would get for nothing: 22.1% of these clients default, so predicting “no default” for everybody scores 0.779, and any accuracy below that is worse than having no model at all. ECE, the expected calibration error, asks the third question: are the probabilities true? Group the predictions into bins by what they claimed, compare each bin’s claim with what actually happened in it, and average the gaps. Zero is perfect. Because it lives on the probability scale it reads directly — an ECE of 0.38 means the stated probabilities are off by about 0.38 on average, which is not a subtle defect.

Why that matters here. Naive Bayes on this data scores an AUC of 0.719, the best of the three models, and an accuracy of 0.519, well below the 0.779 you get by predicting nothing. Both are true at once, and they are not in tension: the model ranks clients correctly and then attaches nonsense probabilities to that ranking, so a cut at 0.5 falls in the wrong place. Moving the threshold recovers 0.804 without changing the model at all. The ECE of 0.378 is what names the actual defect, and the notebook makes it concrete: among the clients Naive Bayes calls essentially safe, 12.7% default; among those it calls essentially certain, only 59.2% do. It is most wrong exactly where it is most confident, which is the worst arrangement a probability can have. Report only AUC and this model looks like the best of the three; report only accuracy and it looks broken; you need all three to say what is actually happening.

Native probabilities, and supplied ones. The models differ in whether they produce probabilities at all. Naive Bayes and logistic regression have them built in — they are fitted by maximising a likelihood, so a probability falls out of the fit, which is what “native” means below. A support vector machine has none: it returns a signed distance from the boundary, which orders cases but is not on the probability scale and does not pretend to be. Platt scaling supplies one after the fact by fitting a one-dimensional logistic regression of the outcome on those distances, using held-out data. That sets up the section’s least expected result: the SVM’s bolted-on probabilities come out better calibrated than logistic’s built-in ones, 0.017 against 0.054. Having a probability natively is not the same as having a good one, and an explicit calibration step fitted for that one job can beat a link function that was fitted for a different one.

The ladder, where it bites, and where it inverts

Every value here is committed notebook output. A orders the four classifiers — Naive Bayes, linear discriminant analysis (LDA), logistic regression and the support vector machine — by how much each one assumes. B comes from the Naive Bayes project’s redundant-feature simulation; C from the SVM project’s four-way comparison, fitted on the same 4,000 training rows so the paradigms are compared rather than the sample sizes.

A · The ladder — how much each model commits to Naive Bayes models p(x, y) features independent given the class LDA models p(x, y) Gaussian, one shared covariance Logistic models p(y | x) no model of the features at all SVM models the boundary not even a probability assumes more each rung drops an assumption the one above it made B · Redundant features — where the trade-off bites training size n (log scale) test error Naive Bayes floors at 0.322 — its asymptotic bias logistic overtakes n = 20,000 30 300 2,000 20,000 0.32 0.35 0.38 0.41 Naive Bayes logistic C · Same four models, same 4,000 rows — the three metrics disagree AUC (ranking) 0.73 0.71 0.69 0.71 0.68 0.71 0.74 best: Naive Bayes accuracy (a decision) 0.44 0.81 0.81 0.82 do-nothing 0.779 0.40 0.60 0.80 best: RBF SVM ECE (are the probabilities true?) 0.44 0.054 0.068 0.017 0.00 0.20 0.40 best: RBF SVM Naive Bayes Logistic Linear SVM RBF SVM

A is the organising idea, and each rung is defined by an assumption the one above it drops. Naive Bayes models the full joint and asserts the features are independent given the class. LDA keeps the joint model and drops the independence, buying a shared covariance matrix for p(p+1)/2 extra parameters. Logistic stops modelling the features altogether and goes straight for the conditional. The SVM declines even that and returns a boundary. Reading down, you are giving up assumptions; reading up, you are buying them back in exchange for needing less data.

B is that exchange, measured. On features built to violate the independence assumption — one informative variable plus ten near-copies — Naive Bayes is decisively better at n = 30 and stays better for two orders of magnitude, because it has fewer effective parameters to pin down. Then it stops: its error flattens at 0.322 and never improves again. That floor is the asymptotic bias, the standing cost of a wrong model of the features. Logistic keeps descending past it and first overtakes at n = 20,000. Both halves of the Ng–Jordan trade-off in one picture — and worth noting that a design with genuinely independent features cannot show the second half at all, because there Naive Bayes is asymptotically optimal too.

C is why the section reports three numbers rather than one. Across the four models the three metrics do not merely differ in magnitude, they disagree about who wins. On AUC the field is nearly flat and Naive Bayes ranks best. On accuracy it collapses to 0.444, far below the 0.779 available for free, while the other three cluster around 0.81. On calibration the ladder inverts outright: the RBF SVM — the support vector machine with a radial basis function kernel, which measures similarity by distance so its boundary can curve instead of being a straight line, and which has no probability model at all until Platt scaling supplies one — is the best calibrated of the four at 0.017, ahead of logistic’s native 0.054. Three defensible metrics, three different winners, one dataset. Pick your metric before you look, or you will pick the model that flatters the one you happened to report.

Generative vs Discriminative — Naive Bayes vs Logistic

The Ng–Jordan trade-off, demonstrated in both directions rather than the usual one. A design with independent features shows the small-n advantage but cannot show the other half, since there Naive Bayes is asymptotically optimal too. Merely correlating the features does not break it either — the optimal direction is still the unweighted sum Naive Bayes forms, so its density model is wrong and its boundary is right. What breaks it is redundancy: one informative feature plus ten near-copies, counted as eleven votes, where its error hits a floor and logistic overtakes at n = 20,000. On real credit data the extra bias surfaces not as worse ranking but as miscalibration — accuracy 0.519 against a do-nothing baseline of 0.779, which the right threshold restores to 0.804. Linear discriminant analysis sits between the two and shows the trade is about the assumption rather than the paradigm: it keeps the generative Gaussian model but replaces independence with one shared covariance, and that single change takes calibration error from 0.378 to 0.049 — better than logistic’s own 0.055. The R companion pins the cross-language gap entirely on the split: given Python's exact indices, e1071 reproduces it to three decimals.

View example →

SVM and the Discriminative Spectrum

The support vector machine completes the ladder by modelling only the boundary — no probability model, and nonlinear separation via the kernel trick without ever describing the features. It appears here for what it says about the spectrum; the machinery itself — margins, the dual, kernels built from scratch, and the identity between a kernel machine and a Gaussian-process posterior mean — is developed in SVM and Kernel Methods. Two claims get checked rather than repeated. The advertised sparsity is a property of separable problems: 4% of points are support vectors on the clean 2-D example, 46% on overlapping credit data. And the calibration ladder inverts — a Platt-scaled SVM reaches ECE 0.017 against logistic's native 0.054, so an explicit calibration step beats a fitted link, and “native” is not the same as “good”. The comparison is also equalised for training size first, since an O(n²) kernel makes it routine to fit the SVM on a subsample and quietly compare sample sizes instead of paradigms.

View example →