McNemar's Test for Comparing Classifiers
A statistical test for whether one classifier is genuinely better than another, built only from the cases where the two models disagreed — the right tool when both models were evaluated on the exact same test set.
Prerequisites: The Confusion Matrix and Classification Metrics
A new model gets 91% accuracy on the test set; the old model it's replacing gets 89%. Is that two-point gap a real improvement, or could it just as easily have come from noise in which particular test cases happened to land in the test set? Comparing two accuracy percentages directly, without a test, invites exactly this false confidence. When both models were scored on the same test set, McNemar's test gives a clean answer, because it uses the fact that the two models saw identical cases and can be compared case by case rather than as two independent summary statistics.
Why comparing the two disagreement cells is the right move
Build a 2x2 table not of predicted-versus-actual, but of model-A-correct versus model-B-correct across the same test cases:
| Model B correct | Model B wrong | |
|---|---|---|
| Model A correct | ||
| Model A wrong |
The cases where both models agree ( and ) carry no information about which model is better — they'd cancel out of any comparison. All the evidence lives in the disagreement cells, (A right, B wrong) and (A wrong, B right). McNemar's test asks whether that split of disagreements is far from the 50/50 split expected if the two models were equally good:
In plain English: if the two models disagree about equally often in each direction, there's no evidence one is better; the test statistic grows only when the disagreements are lopsided toward one model consistently beating the other on the same cases.
Worked example: is the 2-point gap real?
On 1,000 shared test cases, both models agree 850 times (correct or wrong together, split however between and , irrelevant to the test). Among the 150 disagreements, model A is right and B is wrong 95 times (), and B is right and A is wrong 55 times (). Applying the formula:
A chi-squared statistic of 10.1 with 1 degree of freedom corresponds to a p-value well under 0.01 — strong evidence model A's edge isn't a coincidence. Contrast this with , (the same 150 disagreements, nearly evenly split): , nowhere near significant — here the same two-point accuracy gap really could be noise.
What this means in practice
Use McNemar's test whenever two models are compared on one shared, fixed test set — champion-versus-challenger decisions, A/B comparisons against production, or a paper's claim of beating a baseline. It focuses statistical power on the cases that actually distinguish the two models, avoiding the common trap of eyeballing a small accuracy gap and calling it meaningful.
McNemar's test compares two classifiers evaluated on the same test set by looking only at the cases where they disagree, testing whether the direction of those disagreements is lopsided enough to rule out chance — the right tool for "is model A really better than model B," as opposed to comparing two independent accuracy percentages.
McNemar's test evaluates whether one model beats another on this specific test set's disagreements — it says nothing about whether that edge will replicate on new data, and it assumes the test cases are independent of each other, which (as with cross-validation) can be violated by autocorrelated financial time series.
Related concepts
Practice in interviews
Further reading
- McNemar, Note on the Sampling Error of the Difference Between Correlated Proportions
- Dietterich, Approximate Statistical Tests for Comparing Supervised Classification Learning Algorithms