Foundations to Frontier
Tests
D. Classical Machine Learning › D0 Core Concepts (concept pages)

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.

Level 0: Foundation

★☆☆☆☆ 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 rightB wrong
A right(ignored)14
A wrong6(ignored)
exact p-value 0.1153 → not significant at 5%
100100080% power
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. 1. When comparing two models on the same test cases, which cases carry information?
  2. 2. A small p-value means the difference would be…
  3. 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.

from statsmodels.stats.contingency_tables import mcnemar
print(mcnemar([[n11, b], [c, n00]], exact=True).pvalue)
from scipy.stats import wilcoxon, friedmanchisquare
print(wilcoxon(acc_A_per_dataset, acc_B_per_dataset).pvalue)
📝 Quick check (Level 1)
0/3 answered
  1. 1. Two classifiers, one test set: which test?
  2. 2. Comparing 5 models over 20 datasets:
  3. 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. 1. McNemar with b = 10, c = 0 gives exact p =…
  2. 2. The Nadeau–Bengio correction makes the test…
  3. 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. 1. Testing 20 model pairs at α = 0.05 with no correction gives about … false alarms if all are equal.
  2. 2. A significant 0.1-point gain on a huge test set is…
  3. 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

  1. Q. McNemar (1947). “Note on the sampling error of the difference between correlated proportions or percentages.” Psychometrika 12(2).
  2. T. G. Dietterich (1998). “Approximate statistical tests for comparing supervised classification learning algorithms.” Neural Computation 10(7).
  3. C. Nadeau, Y. Bengio (2003). “Inference for the generalization error.” Machine Learning 52(3).
  4. J. Demšar (2006). “Statistical comparisons of classifiers over multiple data sets.” JMLR 7.
  5. R. Agarwal et al. (2021). “Deep reinforcement learning at the edge of the statistical precipice.” NeurIPS.