Statistical Comparison of Models: McNemar and the 5×2 CV t-Test
Model A scores 86.2% and model B 85.4%: is A really better, or is the gap noise? Answering needs a statistical test that respects how the scores were obtained. McNemar's test compares two classifiers on one shared test set using only the cases where they disagree; cross-validation comparisons need care because fold scores are correlated, so the naive paired t-test over many folds is badly overconfident. Dietterich's 5×2 CV t-test and the Nadeau–Bengio corrected resampled t-test fix this, and Wilcoxon or Friedman tests compare models across several datasets. This page explains the tests, their assumptions and the effect sizes that matter. In the exercises you implement the tests, including the t distribution from scratch; then you measure each test's false-alarm rate when two models are equally good and its power when one is better.
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.
Luck or skill?
Two archers each shoot 20 arrows. One hits 15, the other 13. Is the first better, or just luckier today? If they shot at the same targets, the only targets that tell us anything are the ones where one hit and the other missed. If those few cases split 4 to 2, that could easily be luck. Statistical tests turn this intuition into a number, the p-value: how surprising the result would be if both were equally good.
Is model A really better than model B?
B right
B wrong
A right
(ignored)
14
A wrong
6
(ignored)
exact p-value 0.1153 → not significant at 5%
test cases needed for 80% power: 1,369
Cases both models get right or both get wrong tell you nothing about which is better. A 3-point accuracy gap when the models disagree on 15% of cases needs about 1 300 test cases to be detected reliably: small test sets cannot separate close models.
Why engineers care
Switching a deployed perception or fault-detection model has costs: re-certification, re-testing, operator retraining. A “better” model that is only better by chance on one test set is not worth that cost. Proper tests say how confident you can be.
📝 Quick check (Level 0)
0/3 answered
1. When comparing two models on the same test cases, which cases carry information?
2. A small p-value means the difference would be…
3. To detect a small accuracy gap reliably you need…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.
Which test when
Situation
Test
two classifiers, one shared test set
McNemar (exact binomial on disagreements)
two models, cross-validation on one dataset
5×2 CV t-test (Dietterich) or corrected resampled t-test (Nadeau–Bengio)
two models across many datasets
Wilcoxon signed-rank test
several models across many datasets
Friedman test, then Nemenyi post-hoc
any metric, one test set
paired bootstrap confidence interval of the difference
Avoid the naive paired t-test over many repeated CV folds. Its “independent samples” are not independent, so it declares differences that are not there.
3. The naive t-test on 10×10 CV is problematic because…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 35 minUndergraduate students and practising engineers.
The tests in detail
McNemar: under H₀ (“A and B are equally accurate”), each disagreement is equally likely to favour either model, so b ~ Binomial(b + c, ½). The exact p-value is 2·P(X ≤ min(b, c)). The large-sample version uses χ² = (|b − c| − 1)²/(b + c).
Corrected resampled t-test (Nadeau–Bengio): with k train/test splits (train size n₁, test n₂), use t = d̄ / √(s²(1/k + n₂/n₁)) with k − 1 degrees of freedom. The extra term accounts for the correlation from overlapping training sets.
5×2 CV t-test: five replications of 2-fold CV. The statistic is t = p₁⁽¹⁾ / √(mean of the five per-replication variances) with 5 degrees of freedom.
Effect size and uncertainty: always report the difference with a confidence interval (for example a paired bootstrap), not only a p-value.
🐍 Exercise 1: model-comparison tests from scratch
Implement the regularised incomplete beta function and the two-sided t-test p-value; McNemar's exact and χ² tests; the naive paired t-test, the Nadeau–Bengio corrected t-test and Dietterich's 5×2cv t-test; and a paired bootstrap confidence interval for the accuracy difference.
📝 Quick check (Level 2)
0/3 answered
1. McNemar with b = 10, c = 0 gives exact p =…
2. The Nadeau–Bengio correction makes the test…
3. Degrees of freedom of the 5×2cv t-test?
Level 3: Advanced
★★★★☆ Advanced⏱ 30 minGraduate level, with full derivations and proofs.
Comparing models honestly
Decide the comparison in advance: metric, test and significance level. Testing many metrics or many model pairs inflates false discoveries; correct for multiplicity (A5.1.10).
Statistical vs practical significance: with 10⁶ test cases, a 0.1-point gain is “significant” but may not matter. With 100 cases, a 5-point gain may not be detectable. Report confidence intervals.
Use the same splits for all models (paired comparisons are far more powerful).
Variance from training randomness (seeds, initialisation) also matters for neural networks: repeat runs with different seeds.
Across datasets, use rank-based tests and critical-difference diagrams (Demšar, 2006).
Interview questions.
Why is McNemar preferred over comparing two accuracy confidence intervals? The predictions are paired, and the test uses that pairing, which cancels shared difficulty.
What does a non-significant result mean? Not that the models are equal, only that the data could not show a difference. Report the interval.
📝 Quick check (Level 3)
0/3 answered
1. Testing 20 model pairs at α = 0.05 with no correction gives about … false alarms if all are equal.
2. A significant 0.1-point gain on a huge test set is…
3. Not rejecting H₀ means…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.
Which tests can you trust?
Exercise 1.
The t-test p-values match tables (t = 2.228 with 10 degrees of freedom gives p = 0.05).
McNemar with 12 against 5 disagreements gives p = 0.143, not significant despite a 7-case difference.
For the same five fold differences, the corrected t-test gives p = 0.035 where the naive one gives 0.009.
Exercise 2: false alarms and power. Setup:
two nearest-centroid classifiers using different, equally informative features (H₀ true), or one slightly weaker feature (H₁: about a 4-point accuracy gap);
300 examples per dataset, 200 datasets, α = 0.05.
Test
False-positive rate under H₀ (should be 0.05)
Power under H₁
McNemar, one 2/3–1/3 split
0.035
0.12
paired t, 10-fold CV
0.065
0.21
paired t, 10 × 10 repeated CV (naive)
0.535
0.82
corrected t (Nadeau–Bengio), 10 × 10 CV
0.025
0.22
5×2cv t
0.060
0.14
The naive t-test on repeated CV declares a difference in more than half of the cases where none exists. Its apparent “power” is meaningless. Repeating CV adds correlated samples that the naive test treats as independent evidence.
McNemar, the corrected t-test and the 5×2cv test keep false alarms near or below 5%.
Using the data more fully (corrected CV) roughly doubles power compared with one hold-out.
All honest tests detect a 4-point difference on 300 examples less than a quarter of the time. Small benchmarks cannot separate close models.
🐍 Exercise 2 (research): false alarms and power of comparison tests
Simulate pairs of classifiers that are equally good or differ by about 4 accuracy points on datasets of 300 examples; apply McNemar on one split, paired t-tests on 10-fold and naive 10×10 CV, the corrected resampled t-test and the 5×2cv t-test; estimate how often each rejects at α = 0.05.
Model comparison in AI, robotics and aerospace
Model upgrades in safety-relevant systems: a pre-registered comparison with McNemar or bootstrap intervals before replacing a fielded model.
Benchmark claims: across many scenes or datasets, Wilcoxon or Friedman tests with critical-difference diagrams.
Robot learning: compare policies over many seeds and evaluation episodes, with confidence intervals (rliable).
A/B tests of deployed models (A5.3.5).
References
Q. McNemar (1947). “Note on the sampling error of the difference between correlated proportions or percentages.” Psychometrika 12(2).
T. G. Dietterich (1998). “Approximate statistical tests for comparing supervised classification learning algorithms.” Neural Computation 10(7).
C. Nadeau, Y. Bengio (2003). “Inference for the generalization error.” Machine Learning 52(3).
J. Demšar (2006). “Statistical comparisons of classifiers over multiple data sets.” JMLR 7.
R. Agarwal et al. (2021). “Deep reinforcement learning at the edge of the statistical precipice.” NeurIPS.
The type I error rate is the probability of rejecting H₀ when it is true; power is the probability of rejecting it when a specified alternative is true.
If two classifiers have equal accuracy on the population and test cases are independent, then conditional on the number of disagreements n = b + c, b is Binomial(n, ½).
If k fold differences have variance σ² and pairwise correlation ρ, the variance of their mean is σ²(1/k + (k−1)ρ/k), not σ²/k; ignoring ρ underestimates the standard error and inflates the type I error, increasingly so as k grows through repetition.
Pair the comparison, account for overlapping training data, and report effect sizes with intervals.
Practice
Show:
Level 0
Problem 1Level 0
Test for two classifiers on one shared test set?
McNemar’s test.
Problem 2Level 0
The usual significance level α?
0.05.
Problem 3Level 0
Which cases does McNemar use?
The disagreements.
Level 1
Problem 4Level 1
Exact McNemar p-value for b = 10, c = 0?
2/1024 ≈ 0.00195.
Problem 5Level 1
Continuity-corrected McNemar χ² for b = 12, c = 5?
36/17 ≈ 2.118.
Problem 6Level 1
Exact McNemar p-value for b = c = 5?
1.
Problem 7Level 1
Degrees of freedom of the 5×2cv t-test?
5.
Level 2
Problem 8Level 2
Nadeau–Bengio variance factor for k = 100, n_test/n_train = 1/9?
0.01 + 0.111 ≈ 0.121.
Problem 9Level 2
Paired t statistic for differences 1, 2, 3?
2/(1/√3) ≈ 3.46.
Problem 10Level 2
Expected false alarms among 20 independent tests of true nulls at α = 0.05?
1.
Problem 11Level 2
Exact McNemar p-value for 12 vs 5 disagreements?
≈ 0.143.
Problem 12Level 2
Under H₁, fraction of disagreements favouring A if A is 3 points better and they disagree on 15%?
(15 + 3)/30 = 0.6.
Problem 13Level 2
Test for two models across many datasets?
Wilcoxon signed-rank.
Level 3
Problem 14Level 3
Explain why the naive t-test over repeated CV is too liberal.
Fold results share most of their training data and, across repetitions, the same examples, so they are positively correlated; the test treats them as independent, underestimating the standard error of the mean difference, so t is too large and false positives far exceed α. ∎
Problem 15Level 3
Explain why cases where both models are right carry no information about which is better.
Such cases contribute equally to both accuracies; the difference in accuracy equals (b − c)/n, depending only on disagreements, so agreements only add to n without affecting the contrast. ∎
Problem 16Level 3
Describe how to report a model comparison.
State the metric, data and splits; report both scores, the paired difference with a confidence interval (bootstrap or test-based), the test used and its p-value, the number of seeds or folds, and whether the difference is practically meaningful. ∎
Problem 17Level 3
Estimate the test size needed to detect a 3-point gap when models disagree on 15% of cases.
Disagreements favour A with probability 0.6; detecting 0.6 vs 0.5 with 80% power at α = 0.05 needs about 195 discordant cases, i.e. 195/0.15 ≈ 1 300 test cases. ∎
Level 4
Problem 18Level 4
False-positive rate of the naive 10×10 CV t-test in exercise 2?
≈ 0.535.
Problem 19Level 4
False-positive rate of the corrected resampled t-test?
≈ 0.025.
Problem 20Level 4
Power of the corrected test for a 4-point gap on 300 examples?
≈ 0.22.
Coding practice
🐍 Code 1 (Level 1): exact McNemar
Binomial on disagreements.
🐍 Code 2 (Level 1): McNemar χ²
With continuity correction.
🐍 Code 3 (Level 1): paired t statistic
Mean over standard error.
🐍 Code 4 (Level 2): Nadeau–Bengio factor
Corrected variance multiplier.
🐍 Code 5 (Level 2): 5×2cv statistic
Dietterich's t.
Topic test
A timed test drawn at random from this topic's question bank. Each attempt gets new questions. For a longer test across the whole chapter, see the chapter test, or mix topics in the question bag.
Statistical Comparison of Models: McNemar and the 5×2 CV t-Test: topic test
10 questions, drawn at random from a bank of 16. Every attempt is different.
⏱ Time limit: 15 minutes. The test submits itself when time runs out.
Levels covered: 0, 1, 2, 3, 4. No hints or solutions until you submit.