Comparing Two Models on Shared Test Data
Statistics & ExperimentsModel answerLast updated
Apply statistical tests to determine whether two classifiers differ significantly when evaluated on the same labeled dataset, covering hypothesis testing, exact methods, confidence intervals, and multiple comparisons.
Comparing Two Models on Shared Test Data
You built two classification models, A and B, and evaluated both on the identical set of 10,000 labeled examples. The results break down as follows:
| B Correct | B Incorrect | |
|---|---|---|
| A Correct | 8,740 | 300 |
| A Incorrect | 220 | 740 |
Your task is to determine whether the two models have meaningfully different error rates.
Part 1: Statistical Test with Continuity Correction
Apply a standard paired-sample test for model accuracy. Compute the test statistic and two-sided p-value using continuity correction. Show all intermediate calculations: identify which cells matter, compute their difference and sum, and evaluate the test statistic.
Part 2: Exact Calculation
Recompute the p-value without the normal approximation, using an exact method based on the binomial distribution. Describe when you would choose the exact approach over the continuity-corrected version.
Part 3: Confidence Interval
Construct a 95% confidence interval for the difference in accuracy between A and B. Name your method and explain why you selected it over alternatives.
Part 4: Real-World Considerations
- State the key assumptions your test relies on. When might they be violated?
- Describe scenarios where the test would be inappropriate or misleading.
- You now want to compare model A against 10 different models on the same test set. How would you adjust your testing strategy to control for multiple comparisons?
Hints
Hint 1
Only the discordant pairs (where the models disagree) carry information about which is better; the concordant pairs contribute equally to both models.
Hint 2
The test statistic under the null hypothesis follows a known distribution; identify which one and why.
Hint 3
The paired setting means the two accuracies are not independent—this affects the standard error of their difference.
Hint 4
With many comparisons, the probability of a false positive across all tests grows; choose a correction method that controls this rate.
Model answer
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Model Answer: Comparing Two Classifiers on Paired Data
Understanding the Setup
Both models were scored on the same 10,000 examples, making this a paired comparison. The key insight is that examples where A and B both succeed or both fail tell us nothing about which model is better. All discriminative signal comes from the discordant pairs—where one model is correct and the other is wrong.
From the table:
- b = 300 (A correct, B incorrect)
- c = 220 (A incorrect, B correct)
- n11 = 8,740 (both correct)
- n00 = 740 (both incorrect)
- Total n = 10,000
Accuracy for each model:
- Acc(A) = (8,740 + 300) / 10,000 = 0.9040
- Acc(B) = (8,740 + 220) / 10,000 = 0.8960
- Raw difference = 0.0080 (0.8 percentage points)
Part 1: Test with Continuity Correction
Under the null hypothesis H₀ (both models have equal error rates), each discordant pair is equally likely to favor either model. Conditionally on observing b + c total discordant pairs, b follows a Binomial(b + c, 0.5) distribution.
Intermediate values:
- b = 300, c = 220
- |b − c| = 80
- b + c = 520
Test statistic (continuity-corrected):
$$\chi^2_{\text{cc}} = \frac{(|b-c| - 1)^2}{b+c} = \frac{(80-1)^2}{520} = \frac{6241}{520} = 12.00$$
Under H₀, this statistic follows a chi-squared distribution with 1 degree of freedom. We can also express this as a standardized normal: z = √12.00 ≈ 3.464.
P-value:
$$p = P(\chi^2_1 > 12.00) \approx 0.00053$$
or equivalently, p ≈ 5.3 × 10⁻⁴. This is highly significant; we reject H₀ at any standard significance level (α = 0.05, 0.01, etc.). Model A's error rate is significantly lower than B's.
For reference, the uncorrected statistic is (80)²/520 ≈ 12.31, yielding p ≈ 4.5 × 10⁻⁴. Continuity correction has minimal impact here because the sample size of discordant pairs (520) is large.
Part 2: Exact Binomial Test
The exact test treats b as a binomial random variable with n = b + c = 520 and null probability p = 0.5. The two-sided p-value is:
$$p_{\text{exact}} = 2 \sum_{k=0}^{220} \binom{520}{k} \left(0.5\right)^{520}$$
Under H₀, the mean is 260 and the standard deviation is √(520 · 0.25) ≈ 11.40. Observing b = 220 is about 3.46 standard deviations below the mean.
Using a normal approximation to the binomial:
$$z = \frac{220.5 - 260}{11.40} \approx -3.464$$
This gives p ≈ 5.3 × 10⁻⁴, nearly identical to the continuity-corrected test. This is expected: the continuity-corrected McNemar test is specifically designed as a normal approximation to the exact binomial test.
When to prefer the exact test:
- Few discordant pairs. Most guidelines recommend the exact test when b + c < 25 or even b + c < 10.
- Extreme tail behavior. When p-values are very small (e.g., after multiple-testing correction), approximation error matters.
- Conservative inference. The exact test is slightly conservative; if you require a guaranteed upper bound on false positives, use it.
- Computational cost is trivial. With modern software (e.g.,
scipy.stats.binomtest), there is no reason not to use it for small samples. A mid-p variant is also available to reduce conservatism.
With 520 discordant pairs, all approaches agree to high precision.
Part 3: 95% Confidence Interval for Accuracy Difference
Method: Paired Wald Interval
For paired proportions, the standard approach is the Wald interval, which is well-calibrated when the discordant count is moderate to large (here, 520 pairs).
The point estimate of the difference is:
$$\hat{\Delta} = \frac{b - c}{n} = \frac{80}{10000} = 0.008$$
The paired standard error accounts for the correlation between the two models' correctness:
$$SE = \frac{1}{n}\sqrt{(b+c) - \frac{(b-c)^2}{n}}$$
Computing the terms:
- (b + c) = 520
- (b − c)² / n = 6400 / 10000 = 0.64
- (b + c) − (b − c)² / n = 520 − 0.64 = 519.36
- √519.36 ≈ 22.79
- SE = 22.79 / 10000 ≈ 0.00228
95% CI:
$$CI = 0.008 \pm 1.96 \times 0.00228 = 0.008 \pm 0.00447 = [0.00353, 0.01247]$$
Interpretation: Model A is more accurate than B by between 0.35 and 1.25 percentage points, with 95% confidence. The interval excludes zero, consistent with the significant hypothesis test.
Why paired over unpaired:
An unpaired two-proportion interval would use:
$$SE_{\text{unpaired}} = \sqrt{\frac{0.904 \times 0.096}{10000} + \frac{0.896 \times 0.104}{10000}} \approx 0.00424$$
This is nearly twice the paired standard error. The reason: the two models share the same test set, so their correctness is highly positively correlated. Pairing exploits this correlation and yields much tighter intervals and more powerful tests.
Alternatives for small samples or coverage concerns:
- Agresti–Min: Add 0.5 to each of b, c, and recalculate.
- Newcombe's hybrid score interval: Better coverage in the tails; no closed form, but easily computed.
- Bootstrap over examples: Resample rows of the contingency table (keeping pairs intact). Works with any metric and clusters naturally.
Part 4: Assumptions, Violations, and Multiple Testing
Key Assumptions
- Independence: Examples are independent draws from the target distribution.
- Identical evaluation: Both models score every example; no missing predictions.
- No data leakage: The test set was not used to train or tune either model.
- Fixed models: Inference is about these two specific trained models, not about the training algorithms.
- Binary outcomes: Each prediction is scored as correct or incorrect.
When the Test Breaks Down
-
Clustered or dependent data: Examples from the same user, document, patient, or session violate independence. Effective sample size is smaller; p-values are too optimistic. Remedy: Use a cluster bootstrap, mixed-effects logistic model, or GEE.
-
Comparing algorithms, not models: If you want to compare the training procedures (different optimizers, architectures, datasets), McNemar ignores training variance and sampling variability. Remedy: Use repeated cross-validation with a corrected t-test (e.g., Dietterich's 5×2cv).
-
Different test sets: If A and B are evaluated on different examples, the data are not paired. Remedy: Use a two-sample proportion test with unpaired SE.
-
Non-accuracy metrics:
- For AUC: use DeLong's test.
- For F₁, precision, recall, or other non-decomposable metrics: use a paired bootstrap or permutation test.
- For cost-weighted errors: compare total cost, not binary counts.
-
Multi-class or ordinal predictions: McNemar assumes binary labels. For multi-class, use Stuart–Maxwell or a chi-squared test on the full contingency table.
-
Very small discordant counts: With b + c < 10, normal approximations fail. Use the exact binomial test.
-
Statistical vs. practical significance: With n = 10,000, tiny differences become statistically significant. Report the confidence interval and discuss whether 0.8 percentage points matters for the product.
Multiple Comparisons: Testing A Against 10 Models
Running 10 hypothesis tests at α = 0.05 without correction inflates the family-wise error rate to roughly 1 − (0.95)^10 ≈ 0.40. Here are the main strategies:
Bonferroni Correction
- Use α′ = α / m = 0.05 / 10 = 0.005 for each test.
- Valid under any dependence structure.
- Conservative: power loss grows with m.
- Our p ≈ 5 × 10⁻⁴ survives: 5 × 10⁻⁴ < 0.005. ✓
Holm Step-Down (Recommended)
- Sort the p-values: p₁ ≤ p₂ ≤ ... ≤ p₁₀.
- Compare pᵢ to α / (m − i + 1) in order.
- Uniformly more powerful than Bonferroni; still valid under arbitrary dependence.
- If p₁ ≈ 5 × 10⁻⁴ and α′₁ = 0.05 / 10 = 0.005, we reject. Then check p₂ against 0.05/9 ≈ 0.0056, and so on.
Benjamini–Hochberg (FDR Control)
- Appropriate if you are screening many candidates and tolerate some false discoveries.
- Less conservative than Bonferroni; higher power.
- Caveat: Tests here are dependent (all against A, same test set). BH is valid under positive regression dependence on a subset (PRDS), which holds. Benjamini–Yekutieli is valid under arbitrary dependence but stricter.
Simultaneous Confidence Intervals
- Report 99.5% CIs for all 10 differences (Bonferroni-adjusted).
- Easier to interpret than adjusted p-values; conveys uncertainty directly.
Selection Bias
- If you use this test set to choose the best of 10 models, the winner's accuracy is upward biased.
- Always validate on a fresh holdout set.
Summary of Strong Candidate Responses
Strong candidates will:
- Recognize paired data and focus on discordant pairs.
- Execute the calculations correctly: b = 300, c = 220, χ² ≈ 12.0, p ≈ 5 × 10⁻⁴.
- Explain why the continuity-corrected test approximates the exact binomial.
- Use paired SE for the confidence interval and justify why it is tighter.
- Discuss practical violations: clustering, training variance, metric choice, multiple comparisons, and business relevance.
- For 10 comparisons, recommend Holm step-down or report simultaneous CIs, and mention test-set reuse bias.