Is the new model really better? McNemar's test on a 200-example eval set

2026-11-28来源 dev.toEnglish

I was reading a thread this week about how you tell if your model is improving, and it was all about how to build the eval. I wanted to add the bit that usually gets left out, which is how big the eval set has to be before a change in the score means anything, and what test to run on it.

Say the old model gets 80% on 200 examples and the new one gets 85%. With 200 examples at 80% the standard error is about 2.8 points, so the 95% interval is roughly plus or minus 5.5, and if you compare the two accuracies as if they came from separate samples the gap is only about 1.3 standard errors. On that view you can't tell them apart.

But they didn't come from separate samples. Both models answered the same 200 questions, and that changes the test you should use.

Only the disagreements count

If both models got a question right, or both got it wrong, that question can't tell you which model is better. What's left are the questions where they disagree. Call nab​ the number the old model got right and the new one got wrong, and nba​ the other way round. If the models were equally good each disagreement would be a coin flip, so nab​ would be Binomial(n,21​) with n=nab​+nba​ . McNemar's test asks how likely a split at least that lopsided would be.

from math import comb def mcnemar_p_value(y, a, b): n_ab = n_ba = 0 for t, p, q in zip(y, a, b): if p == t and q != t: n_ab += 1 elif q == t and p != t: n_ba += 1 n = n_ab + n_ba if n == 0: return 1.0 k = min(n_ab, n_ba) tail = sum(comb(n, j) for j in range(k + 1)) return min(1.0, 2 * tail / 2 ** n) 

It's exact, it's a few lines, and Python's integers mean there's no rounding until the last division.

The thing I find most useful about it is that the same headline gain can come out quite differently. If the new model wins 14 disagreements and loses 4, that's a net 10 out of 200, so 5 points, and the p-value is about 0.03. If it wins 18 and loses 8 it's the same net 10 and the same 5 points, but the p-value is about 0.08. That's why I'd always log the won and lost counts next to the accuracy.

How often each test catches a real gain

To see how much the pairing buys you I simulated it. On each example the old model is right and the new one wrong with probability 3%, the new one is right and the old one wrong with probability 8%, and otherwise they agree, so the true accuracies are 80% and 85%. I drew 200 examples 10,000 times and ran both tests at the 5% level.

McNemar (paired) two-proportion z test (unpaired)
New model really 5 points better caught 49.5% of the time caught 14.8% of the time
Models equally good (5.5% each way) false alarm 3.1% false alarm 0.08%

So the paired test finds the real gain more than three times as often. It still only finds it about half the time, which tells you that 200 examples is too few for a 5 point change. With 400 examples McNemar caught it 84% of the time against 43% for the unpaired test, and with 600 it was 96% against 70% (3,000 runs each). The unpaired test is far too cautious when the models agree on most examples, because it treats all that agreement as if it were noise.

A few things I'd keep in mind

Keep the eval set fixed and versioned. If someone adds or swaps examples between runs you can't pair the results any more and you're back to the weak test.

With more than two classes both models can be wrong in different ways. That still counts as both wrong, because you compare each prediction with the label and not with each other.

The chi-squared version of McNemar is fine with lots of disagreements, but with under about 25 the exact sum is better and it costs nothing.

And if you're comparing five models pairwise that's ten tests, so one of them coming in under 0.05 doesn't mean much on its own.

I've put this up as a problem you can solve in the browser, with tests checked against an independent exact calculation, at https://academicquant.co.uk/ml-mcnemar-test?utm_source=devto&utm_medium=post&utm_campaign=devto-mcnemar-eval

原文:https://www.a8q9.cn/en/article/is-the-new-model-really-better-mcnemar-s-test-on-a-200-example-eval-set-a66fac.html

相关文章