Comparing Models Statistically
Paired tests, McNemar and honest benchmark comparisons.
Model B beats model A by 1.2 points of accuracy. A real improvement, a lucky test set, or a lucky random seed? This lesson separates the sources of variation in a measured score, puts an honest interval on a single accuracy, compares two models with paired tests on one test set and across retrainings, handles many datasets and many comparisons at once, and ends with Bayesian alternatives and what honest benchmarking still struggles with.
What you’ll learn
- Put a confidence interval on a test accuracy, and explain why the Wilson interval beats the textbook Wald interval.
- Derive McNemar's test from the discordant pairs, and quantify how much power pairing buys.
- Explain why resampled -tests are anticonservative, derive the Nadeau–Bengio correction, and apply Dietterich's 5×2cv test.
- Compare classifiers over many datasets with the Wilcoxon signed-rank, Friedman and Nemenyi tests, and prove that Holm's procedure controls the family-wise error rate.
- Read a Bayesian comparison with a region of practical equivalence, and design experiments that account for seeds and data splits.
Which variation, and an interval for one accuracy
A measured score moves for several reasons: which examples landed in the test set, which ones were used for training, the algorithm's internal randomness (initialization, data order, dropout), and the hyperparameter search. Which of these a comparison must include depends on the question (Dietterich, 1998):
- Is this trained model better than that one, on the population the test set was drawn from? Only the test sample varies.
- Is algorithm A better than algorithm B for training sets of this size from this population? Training samples and seeds vary too, so the models must be retrained.
Start with the first question and a single model. If the test examples are i.i.d. and the model is fixed, the number it classifies correctly is , where is its true accuracy.
The textbook (Wald) confidence interval plugs the estimate into the standard error. Its coverage is erratic, and can be poor even for large (Brown, Cai & DasGupta, 2001). Instead, keep every that a level- test, using the standard error under that , would not reject:
This is a quadratic inequality in . Its solution is an interval whose center is pulled from toward :
At the edge the difference is stark. A model that gets all 50 test examples right has a Wald interval of width zero, ; the Wilson interval at 95% is , since its lower end is . With 45 of 50 correct, Wald gives and Wilson , which never pokes above 1.
A model classifies 900 of 1,000 test examples correctly. How wide is the 95% confidence interval for its accuracy, in percentage points (upper end minus lower end)?
Show the answer
3.7 points
points; Wilson gives , the same width. So two models scored separately on test sets of 1,000 cannot be told apart at the 1-point level. Scoring them on the same examples does much better, as the next step shows.
Two models, one test set: pair the comparison
Two models evaluated on the same examples make correlated errors: hard examples trip both. Sort the test examples into four groups: both right, both wrong, where only A is right, and where only B is right. The difference in accuracy is , so the examples on which the models agree carry no information about which is better.
In simulations by Dietterich (1998), McNemar's test had acceptable Type I error and was the test to use when a learning algorithm can be run only once. Its limit is built in: it treats the two trained models as fixed, so it answers the first question of the previous step, not the second. A different training sample or seed might reverse the result, and McNemar's test cannot see that.
In McNemar's test for two classifiers on one test set, which test examples affect the statistic?
- All of them, weighted equally
- Only those both models get wrong
- Only those on which exactly one of the two models is correct
- Only those both models get right
Show the answer
C. Only those on which exactly one of the two models is correct
The statistic uses and alone. Examples both models get right or both get wrong shift the two accuracies equally and say nothing about which model is better; conditioning on them removes their noise, which is where pairing gets its power.
Retraining: correlated folds and corrected tests
To compare algorithms, retrain them on many training sets, for example over the folds of -fold cross-validation or over random train/test splits, and record the paired differences in test score. The naive paired -test uses with the sample standard deviation . But the training sets overlap heavily, and with random splits the test sets overlap too, so the are positively correlated, underestimates , and the test rejects a true null far too often (Dietterich, 1998). Worse, no estimator of the variance of -fold cross-validation is unbiased under all distributions (Bengio & Grandvalet, 2004).
Model the differences as equicorrelated, each with variance and pairwise correlation . The variance of their mean does not shrink to zero as grows:
The sample variance only sees the spread of the around their shared component, so it estimates , not :
The correlation cannot be estimated from the themselves. Nadeau & Bengio's heuristic gives , and the corrected resampled -test, compared with a distribution on degrees of freedom:
5×2cv. Dietterich proposed five replications of 2-fold cross-validation. In replication , let be the differences on the two folds, their mean and . Under the null, is approximately . With half the data in each training set the folds are less entangled, and Alpaydın (1999) improved the test into a 5×2cv test that uses all ten differences, with lower Type I error and higher power.
Many datasets, many comparisons
Papers usually compare several methods on a suite of datasets. Demšar (2006) reviewed the practice and recommended nonparametric tests. Averaging accuracies across datasets is questionable because errors on different problems are not commensurable; a paired -test across datasets adds assumptions of normality and sensitivity to outliers; counting wins (the sign test) is valid but weak.
Two classifiers: the Wilcoxon signed-rank test (Wilcoxon, 1945). Rank the absolute differences over the datasets, let and be the sums of the ranks of the positive and the negative differences, and use , with exact tables for small and for larger the normal approximation . It uses the size of each difference through its rank, which makes it more powerful than counting wins and robust to one dataset with a huge difference.
classifiers: the Friedman test (Friedman, 1937). Rank the classifiers within each dataset (1 = best) and average to get . Under the null that all are equivalent, below is approximately ; Iman & Davenport (1980) showed it is conservative and proposed , approximately -distributed with and degrees of freedom:
If it rejects, the Nemenyi post-hoc test declares two classifiers different when their average ranks differ by more than the critical difference , where is a Studentized-range quantile divided by ( for , for ).
Four classifiers are compared on datasets. What is the Nemenyi critical difference in average rank at ?
Show the answer
1.05
. With ranks running from 1 to 4, two classifiers must be about a full rank apart on average to be declared different: twenty datasets is not a lot.
Multiple comparisons. Running tests at level makes a false rejection likely: with 10 independent true nulls at 5%, the chance of at least one is . The family-wise error rate (FWER) is the probability of any false rejection. Bonferroni tests each hypothesis at , which keeps the FWER at most by the union bound, under any dependence. Holm (1979) sorts the -values and rejects for as long as , stopping at the first failure. It also holds the FWER at under any dependence, and rejects everything Bonferroni does and often more. When many comparisons are exploratory, controlling the false discovery rate instead (Benjamini & Hochberg, 1995) is a gentler criterion.
Let be the set of true null hypotheses, , with valid -values: for . Consider the event that every true null has . By the union bound, whatever the dependence between the -values:
Suppose a true null is rejected, and let step be the first time this happens. All hypotheses rejected before it are false nulls, so , i.e. . Rejection at step needs :
That contradicts . So a false rejection can only occur outside , and the FWER is at most . Bonferroni's thresholds are never larger than Holm's, which is why Holm is uniformly more powerful.
Beyond the p-value
A -value answers a narrow question: how surprising the data would be if the null hypothesis were exactly true. It is not the probability that the null is true; with enough data, differences too small to matter become "significant"; and a non-significant result is not evidence that two models are equivalent. Benavoli, Corani, Demšar & Zaffalon (2017) argued that machine learning should move to Bayesian analysis, and gave practical tests.
The Bayesian correlated -test models the cross-validation differences as correlated Gaussians with the Nadeau–Bengio . Under a reference ("matching") prior, the posterior of the mean difference is a Student with degrees of freedom, location and squared scale : numerically the same ingredients as the corrected -test, read differently. Declare a region of practical equivalence (rope), say point of accuracy, and report three posterior probabilities: A practically better, the two practically equivalent, B practically better.
Key takeaways
- Decide which variation the comparison must include: test-set sampling only (two trained models), or also training sets and seeds (two learning algorithms).
- Use the Wilson interval for an accuracy; at and 90% accuracy it is about ±1.9 points. Pairing on the same test examples is far more powerful than comparing two separate intervals.
- McNemar's test uses only the discordant examples, under the null, and speaks about fixed models.
- Resampled differences are correlated: correct the variance by (Nadeau–Bengio) or use 5×2cv; no estimator of the variance of -fold CV is unbiased in general.
- Across datasets use Wilcoxon or Friedman with post-hoc tests and Holm's correction; Bayesian tests with a region of practical equivalence answer whether a difference is large enough to matter.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Two trained classifiers are compared on one test set with McNemar's test, which rejects equality at . What has been established?
- That the first learning algorithm is better than the second for data of this kind
- That these two trained models differ in accuracy on the population the test set came from
- That retraining either model with a new seed would give the same ranking
- That the difference in accuracy is large enough to matter in practice
Show the answer
B. That these two trained models differ in accuracy on the population the test set came from
McNemar's test treats the trained models as fixed and the test examples as the only source of randomness, so it supports a conclusion about these two models. Whether the algorithms differ needs retraining on new samples and seeds, and whether the difference matters is a question of effect size, not of the -value.
Why does the ordinary paired -test on 30 random train/test splits reject a true null hypothesis too often?
- Because accuracy differences are never normally distributed
- Because 30 splits are too few for the distribution
- Because overlapping training and test sets make the differences positively correlated, so underestimates the variance of their mean
- Because the test should be one-sided
Show the answer
C. Because overlapping training and test sets make the differences positively correlated, so underestimates the variance of their mean
With correlation , , while estimates . The denominator is too small, the statistic too large, and the Type I error inflated; more splits make it worse, not better. The Nadeau–Bengio correction adds the missing term.
Four comparisons give -values 0.001, 0.015, 0.02 and 0.04. At family-wise level , which hypotheses does Holm's procedure reject?
- Only the first
- The first two
- The first three
- All four
Show the answer
D. All four
Holm compares the sorted -values with , , and , rejecting until the first failure. Here , , and , so all four are rejected. Bonferroni, which compares every -value with , would reject only the first.
A model makes no errors on 30 independent test examples. What can you say about its true error rate at 95% confidence?
- It is zero
- It is below 1%
- Nothing, since no errors were observed
- It is below roughly 10%
Show the answer
D. It is below roughly 10%
If the error rate were , the chance of 30 clean results would be , which falls below 5% once : the "rule of three" upper bound . The two-sided Wilson interval gives accuracy at least 0.886. Zero observed errors is compatible with a sizable error rate.
Further reading
- Q. McNemar, "Note on the sampling error of the difference between correlated proportions or percentages," Psychometrika 12(2), 153–157 (1947).
- T. G. Dietterich, "Approximate statistical tests for comparing supervised classification learning algorithms," Neural Computation 10(7), 1895–1923 (1998).
- C. Nadeau & Y. Bengio, "Inference for the generalization error," Machine Learning 52(3), 239–281 (2003).
- J. Demšar, "Statistical comparisons of classifiers over multiple data sets," Journal of Machine Learning Research 7, 1–30 (2006).
- A. Benavoli, G. Corani, J. Demšar & M. Zaffalon, "Time for a change: a tutorial for comparing multiple classifiers through Bayesian analysis," Journal of Machine Learning Research 18(77), 1–36 (2017).