← All examples

Machine Learning: Model Evaluation & Interpretability

Every earlier subsection built models. This one asks the questions that decide whether a model is usable, and they are all versions of the same question: does this number mean what it appears to mean?

A selection criterion that recovers the true model on one dataset may do so 70% of the time. A feature-importance ranking can be produced by an algorithm's internal accounting rather than by the data. A classifier with excellent discrimination can emit probabilities that are badly wrong as probabilities. And a point prediction says nothing at all about how far off it might be.

The methods here are the standard answers — and because they are diagnostics, each one is itself put through the check it recommends. A criterion is asked to repeat its verdict on fresh draws; an attribution is compared against a permutation baseline and against what the model actually uses; a probability is scored against observed frequency; and an interval is checked for whether its stated coverage is delivered.

Choosing complexity, and the four criteria that do it. A more flexible model always fits the data it was trained on better, so training error cannot decide how complex a model should be — past some point the extra flexibility is spent reproducing noise that will not recur, which is overfitting. Every honest criterion therefore estimates performance on data the model has not seen. Cross-validation does it directly, refitting on subsets and scoring the held-out part. AIC (the Akaike information criterion) and BIC (the Bayesian information criterion) do it from a single fit, taking the likelihood and subtracting a penalty for the number of parameters — the difference between them is the size of that penalty, and it is not a detail: AIC charges a fixed amount per parameter, BIC charges an amount that grows with the sample size. PSIS-LOO is leave-one-out cross-validation computed from a Bayesian posterior without refitting, using Pareto-smoothed importance sampling to reweight the existing draws. It reports an ELPD, the expected log pointwise predictive density — the average log probability the model assigns to a new observation, where higher is better — and comes with a self-diagnostic: the Pareto-k statistic flags when that reweighting is unreliable, conventionally at 0.7, so an ELPD is only worth comparing once the k values are below it.

Two questions that sound like one. The criteria above split into two camps, and the split explains almost everything that happens on the first page. CV, AIC and LOO all estimate how well the model will predict. BIC estimates which model generated the data. These come apart whenever a model slightly too large predicts about as well as the right one, which is usually. A criterion is consistent if the probability it picks the true model goes to 1 as the sample grows, and efficient if it picks the model that predicts best. BIC is consistent, AIC is not — more data does not rescue it, because its penalty per parameter never grows — and no criterion is both. That is a property of the question each one asks, not a bug in either.

Attribution: three ways to ask which features mattered. Impurity importance, also called MDI (mean decrease in impurity), adds up how much each feature improved the splitting criterion every time a tree used it. It is free, because the model recorded it while fitting — and it is computed entirely from the training data, which is the source of its trouble. Permutation importance asks the question empirically instead: shuffle one column, re-score on held-out data, and see how much performance drops. A column carrying nothing should cost nothing, and can score slightly negative. SHAP (SHapley Additive exPlanations) comes from cooperative game theory: a Shapley value divides a payout among players by averaging each player's marginal contribution over every possible order of joining, and here the players are features and the payout is the prediction. That gives it a property the others lack — contributions for a single prediction that sum exactly to it — at much greater cost. One distinction runs through all three: SHAP is faithful to the model, describing what the model does; permutation is faithful to the data, describing what actually helps. When a model has learned something spurious those are different answers, and both are correct.

Calibration: ranking well and being right are different achievements. Discrimination is whether a model orders cases correctly, and is what AUC — the area under the receiver-operating-characteristic curve — measures: the probability a randomly chosen positive case is ranked above a randomly chosen negative one. Calibration is whether the numbers mean what they say: among cases given a 30% chance, does about 30% actually happen? A model can be excellent at the first and badly wrong at the second, because nothing in a ranking metric inspects the values themselves. The usual summary is ECE, the expected calibration error: sort predictions into bins, compare each bin's average claim with the frequency observed in it, and average the gaps weighted by bin size. Because it averages absolute gaps it can never be zero on finite data, which is why a simulated floor matters. The Brier score is the mean squared error of the probabilities, mixing both properties into one number, and a reliability diagram is the picture: claimed probability on one axis, observed frequency on the other, with the diagonal as perfection. Two repairs recur. Platt scaling fits a sigmoid to the model's scores — two parameters, so it can stretch or shift confidence but not change the shape of the distortion. Isotonic regression fits any non-decreasing function, so it can absorb a distortion that reverses direction, at the price of far more flexibility and a tendency to overfit small samples.

Conformal prediction, and the assumption nobody states. A point prediction says nothing about how wrong it might be. Conformal prediction wraps any model in an interval — or, for classification, a prediction set of labels — whose coverage is guaranteed in finite samples. It works by choosing a nonconformity score, a measure of how unusual an observation looks to the fitted model (for regression, typically just the absolute error), computing it on a held-out calibration set, and taking the appropriate quantile as the threshold. It is distribution-free: nothing is assumed about the shape of the errors, and nothing is assumed about the model being any good — a terrible model simply produces wide intervals. But distribution-free is not assumption-free. The guarantee requires exchangeability: that the calibration data and the new case are drawn in a way where the order does not matter, so a future point is no more surprising than a calibration point was. Independent draws from one population satisfy it; a time series with drift, or a deployment on a population that has shifted since calibration, does not — and when exchangeability fails the guarantee fails with it, silently. The second limit is what the guarantee covers. Marginal coverage is an average over all cases; conditional coverage would hold within every subgroup. Only the first is promised, and the gap between them is measurable. CQR (conformalized quantile regression) narrows it by conformalising a quantile model, so interval width varies with the case, and APS (adaptive prediction sets) is the classification analogue, accumulating predicted probabilities until they cross the calibrated threshold.

Four diagnostics, each put through the check it recommends

Every value is committed notebook output. A comes from the model-selection example, B from interpretability and calibration, C from conformal prediction. Each panel carries its own measure and its own axis.

A · Model selection: more data fixes one criterion and not the others 0.6 0.7 0.8 0.9 1.0 n=150 n=400 n=1,000 n=2,500 BIC AIC CV P(picks the true degree) BIC asks which model GENERATED the data, and its penalty grows with the sample, so more data settles it. CV and AIC ask which model PREDICTS best. A degree-5 polynomial predicts a cubic about as well as a cubic does, so they keep taking the larger model — at every n tried. That is consistency against efficiency. Neither is a bug; they are answering different questions. No criterion ever picks too SIMPLE. Every error is over-selection. B · Give the measure nothing, and see what it reports feature importance given a column of pure noise as a share of the strongest genuine feature, on each measure's own scale +10% impurity +5% SHAP −1.5% permutation Only permutation is scored on held-out data, and only permutation gets it right: shuffling a column of nothing slightly HELPS. calibration error, as a multiple of the simulated noise floor the floor is what a perfectly calibrated model scores at this sample size 29× naive Bayes 4.6× logistic 2.2× SVM (pre-cal.) 1.2× forest floor Repairing the logistic model: Platt scaling moves it 0.0551 → 0.0560, which is nothing. Isotonic takes it to 0.0071 — an eight-fold cut. C · A guarantee that holds on average and fails everywhere in particular 0.70 0.80 0.90 1.00 0 1 2 3 4 5 6 7 8 9 coverage of a nominal 90% interval decile of predicted house value — cheapest on the left split conformal: constant width CQR: width varies with the case target Both hit the marginal target almost exactly — 0.897 and 0.895 against 0.90 — and that is the whole problem. Averaged over everyone the constant-width interval is a valid 90% interval; for the eighth decile it is a 72% interval. CQR is 9% wider on average, which reads as a regression, and cuts the spread across deciles from 0.26 to 0.08. The average was never the quantity anyone needed.

A is the panel that reframes a disagreement as a design choice. Four criteria all pick the right model on one dataset; repeated on fresh draws they separate, and the separation does not close with more data. BIC sits at 0.97–1.00 at every sample size tried, while AIC and cross-validation stay near 0.7 from n = 150 all the way to 2,500. That is not AIC losing a race. AIC and cross-validation estimate which model predicts best, and a degree-5 polynomial predicts a cubic almost exactly as well as a cubic does, so a predictive criterion has little reason to prefer the smaller one. BIC estimates which model generated the data, and its penalty grows with the sample, so more data resolves it. The pair of properties has names — BIC is consistent, AIC is efficient — and no criterion has both. Worth noting what never happens: not one of them ever picks a model that is too simple. Every error in the experiment is over-selection.

B is the same experiment run twice on different diagnostics: hand the measure nothing, and see what it reports. On the left, a column of pure noise is injected into the credit data. Impurity importance awards it 10% of the strongest genuine feature’s score and SHAP 5%, while permutation returns a negative value — shuffling a column of nothing slightly improves held-out performance, which is the right answer. The reason is not that two measures are broken: permutation is the only one of the three scored on data the model never saw, and SHAP is faithful to the model, which really did split on that column. On the right, the same move applied to calibration. Because the error metric averages absolute gaps it can never reach zero on finite data, so a floor is simulated and everything is read against it: naive Bayes lands at 29× the floor at the same discriminative power as logistic regression — and logistic regression itself, the model most often assumed to be calibrated by construction, is the second worst of the four.

C is the sharpest of the four, because the guarantee is real and still not what a user wants. Both methods hit their marginal target almost exactly, 0.897 and 0.895 against 0.90. Split by decile of predicted house value, the constant-width interval covers 98% of the cheapest houses and 72% of the eighth decile. It is a valid 90% interval averaged over everyone and a 72% interval for expensive properties, which is the group most likely to be priced individually. Conformalized quantile regression spends its width where the uncertainty is, and the case for it has to be made carefully because the obvious metric points the wrong way: it is 9% wider on average, which reads as a regression, while cutting the spread across deciles from 0.26 to 0.08. No distribution-free method can achieve exact conditional coverage — that is a theorem, not a gap in the implementation — so measuring how far the marginal guarantee is from the conditional one is part of the job rather than an optional extra.

How the four diagnostics relate

These are not four topics so much as one method applied four times: establish what the measure reports when there is nothing to report, then read the real result against that.

The four share a failure mode as well as a method. In each case the tempting number is the flattering one, and it is flattering because it was computed on the wrong thing: an in-sample criterion, an importance recorded during fitting, an error metric compared against zero instead of its own floor, a coverage figure averaged over a population nobody is pricing. The corrective is the same each time — construct the null case deliberately, then read the result against it — which is why a simulated noise floor, a column of nothing, a set of fresh draws and a decile breakdown all belong to one subsection.

Model Selection — Cross-Validation, AIC/BIC and Bayesian LOO

Four routes to “which model?” on data generated from a known cubic, where they can be graded against the truth. All four find degree 3 — on this sample. Repeated on 200 fresh draws the agreement turns out to be partial and the pattern is the content: CV lands on the truth 68% of the time, AIC 72% and BIC 97%, with all three picking 3 in the same draw only 60% of the time. PSIS-LOO, run separately on ten draws because each costs a full posterior, lands at 7 of 10 — too few to compare with the others precisely, but falling on the AIC side of the divide, as theory says it must. That split is not three criteria failing. CV, AIC and LOO all estimate predictive accuracy, and a degree-5 polynomial predicts a cubic almost exactly as well as a cubic does; BIC asks which model generated the data. Raising n to 2,500 leaves AIC at 0.69 and CV at 0.64 while BIC stays at ceiling — consistency versus efficiency, measured rather than asserted. The Bayesian panel is read with the paired standard error rather than the standard error of each ELPD, which shows LOO decisively rejecting degrees 1 and 2 while unable to separate 3 from anything up to 8.

View example →

Interpretability — Impurity vs Permutation vs SHAP

Three feature-importance measures given a column of pure noise and asked to notice. Impurity awards it 10% of the strongest real feature’s importance, SHAP 5%, and permutation returns a negative value — shuffling it slightly improves held-out AUC, which is right for a column containing nothing. Read as ranks the three look alike and the story inverts; read as magnitudes it is unambiguous, and 18 of 25 features turn out to sit within two standard errors of zero, where ordering is arbitrary. The mechanism is then isolated rather than cited: five noise columns identical except in cardinality collect steadily more impurity as that number rises, while permutation stays flat at zero. SHAP’s residual mass is not a defect — SHAP is faithful to the model, and the forest really did split on the noise. Closes with the correlated-block caveat measured at 1.5×, SHAP beeswarm and local attributions, and PDP/ICE.

View example →

Calibration — Are the Probabilities Right, and How to Fix Them

A model can rank well and still emit probabilities that are wrong, and the metric for it needs its own diagnostics first. Because ECE averages absolute gaps it is strictly positive even for a perfect model, so a noise floor is simulated — about 0.012 here — and everything measured against that rather than against zero. Naive Bayes then lands 29× the floor at the same AUC as logistic regression, the classic result. Two quieter ones follow: logistic regression is the second worst of the four, and the SVM only looks good because SVC(probability=True) had already Platt-calibrated it — never a like-for-like comparison. The sharpest finding is a failure: Platt scaling cannot repair logistic regression at all (0.0551 to 0.0560), because a sigmoid on a sigmoid is only a two-parameter rescaling, while isotonic fixes the same model eight-fold. Match the corrector to the shape of the distortion.

View example →

Conformal Prediction — Guaranteed Coverage, and What It Does Not Guarantee

The strongest uncertainty statement available: intervals whose coverage is guaranteed in finite samples, distribution-free, wrapped around any model, with no assumption that the model is good. Built from scratch on California housing, the guarantee holds at every level tested. Then the caveat that is usually noted in passing gets measured: the guarantee is marginal, and a constant-width 90% interval turns out to cover 98% of the cheapest decile of houses and 72% of the eighth — completely valid on average, and really a 72% interval for expensive properties. CQR is then justified on the trade it actually makes rather than the one it appears to: it is 9% wider on average, which looks like a regression, while cutting the coverage spread from 0.26 to 0.08. The classification half turns on matching the set construction to the score that calibrated it — including one label too many costs 34% in set size and pushes coverage to 0.955 against a 0.90 target, because over-covering is not free.

View example →