Skip to content

Is one model really better? Paired comparisons

A leaderboard's order isn't evidence of a difference. Compare fold by fold, correct for overlapping training rows, and ask what gap would matter in practice.

Advanced ~13 min9 steps

What runs in CodeSplash

You will be able to

  • Explain why comparisons should be paired on shared folds
  • Say why a plain paired t test on folds is too optimistic, and what corrects it
  • Read McNemar, DeLong, the region of practical equivalence and the leaderboard's chips

Read first

01

Same folds, same questions

Read

Every model in an experiment uses one split plan. So on each fold, two models answer the same questions, and their fold scores are paired.

Pairing removes the difference between easy and hard folds, which is often larger than the difference between the models. Two runs that look tied on average might be consistently ordered, fold by fold.

Paired lines (spaghetti plot)How did each subject change across conditions or time points?
View data
patientvisitscore
P01baseline34.700
P01week 431.400
P01week 1224.200
P02baseline34.900
P02week 434.300
P02week 1236.500
P03baseline34.500
P03week 436.200
P03week 1240.100
P04baseline30.400
P04week 428.900
P04week 1227.400
P05baseline37.200
P05week 433.100
P05week 1232.100
P06baseline26.500
P06week 422.500
P06week 1218.900
P07baseline26.600
P07week 421.800
P07week 1216.700
P08baseline26.600
P08week 428.700
P08week 1220.900
P09baseline16.500
P09week 414.900
P09week 1212.800
P10baseline24.200
P10week 424
P10week 1224.100
P11baseline34.300
P11week 435.700
P11week 1236.100
P12baseline30
P12week 426
P12week 1224.600
P13baseline28.400
P13week 423.100
P13week 1222
P14baseline12.900
P14week 411.300
P14week 126
P15baseline26.100
P15week 423.400
P15week 1225.900
P16baseline28.400
P16week 428.300
P16week 1228.200
P17baseline30.100
P17week 430.400
P17week 1232.300
P18baseline20.900
P18week 418.900
P18week 1215
P19baseline19.800
P19week 420.700
P19week 1216
P20baseline31.900
P20week 431.400
P20week 1230.200

First 60 of 72 rows.

Linear and boosted models on Pima: 0.857 and 0.843. Is that gap real?

02

A plain t test lies

Predict

Predict first

You run a paired t test on 5 fold differences. The folds share most of their training rows, so their differences are correlated. What does that do to the plain test's standard error?

Too small, so it misses real gapsAbout rightToo large, so it finds gaps that aren't there
Reveal

It's too small, which makes the p-value too small, so the test finds gaps that aren't there. Fold scores aren't independent draws: with 5 folds, any two training sets share three quarters of their rows.

03

The correction

Read

The Nadeau–Bengio corrected resampled t inflates the variance for that overlap:

Varcorrected=Var(d)(1J+ntestntrain)\text{Var}_\text{corrected} = \text{Var}(d)\left(\frac{1}{J} + \frac{n_\text{test}}{n_\text{train}}\right)

JJ is the number of folds. With 5 folds, ntest/ntrain=1/4n_\text{test}/n_\text{train} = 1/4, so 1/51/5 becomes 1/5+1/4=0.451/5 + 1/4 = 0.45. The variance more than doubles, so the standard error grows by 1.51.5 times.

The Holm-adjusted version of this test drives the leaderboard's chips: ≈ best (tied), worse, or unpaired (folds differ).

04

Tests on test rows

Read

On the locked test rows, two models have predictions for the same rows.

  • McNemar looks only at rows where one is right and the other wrong.
  • DeLong tests the difference in two AUCs on the same rows. For example, glucose against BMI as a marker: AUC 0.794 against 0.681, a difference of 0.113, p < .001.
  • A paired bootstrap resamples test rows and gives an interval for the difference in the primary metric.
05

Only the discordant rows count

Explore

McNemar's test ignores rows where both models agree. Raise the two agreeing cells and see that nothing changes. Then move the two discordant cells.

EExplorable · McNemar's discordant pairs

For paired yes/no data, which pairs carry the evidence?

Add 1,000 people who answered yes both times. What happens to the p-value?

How sure?
40
18
6
36
Yes afterNo after
Yes before
40
no change
18
changed
No before
6
changed
36
no change

Only the highlighted pairs count. p = .025 whatever the gray cells hold. The change in proportions is 12.0 points out of 100 people.

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Read each cell as model A's and model B's verdict on a row: right or wrong, where "before" and "after" become "A" and "B".

06

Would the gap matter?

Reflect

A significance test asks whether a gap is zero. You probably want to know something else: would this gap change what you do?

The Bayesian correlated t test answers with a region of practical equivalenceRegion of practical equivalence (ROPE)A band around zero of differences too small to matter; decisions compare the credible interval with it. (ROPE). You choose how small a gap doesn't matter. The default is 0.01 for bounded metrics. P(better) is the probability that the other model is better by more than that, and P(equivalent) that the gap lies inside it.

For a model you're working on, what size of gap would make you switch?

07

Check your understanding

Checkpoint

Check yourself

10-fold cross-validation, so J=10J = 10 and ntest/ntrain=1/9n_\text{test}/n_\text{train} = 1/9. What is 1/J+ntest/ntrain1/J + n_\text{test}/n_\text{train}? Give two decimals.

Show the answer

Answer: 0.21

1/10+1/9≈0.211/10 + 1/9 \approx 0.21, about twice the plain 1/101/10. More folds raise the fit count, but they don't make the correction go away.

Check yourself

On 1,000 test rows, A and B agree on 950. Of the other 50, A alone is right on 30, and B alone on 20. Which rows does McNemar use?

Show the answer

Answer: b

Rows where both are right or both are wrong say nothing about which is better.

Check yourself

A leaderboard row reads ≈ best. What should you do?

Show the answer

Answer: a

A tie means the folds couldn't separate them. That isn't proof of equality, and a ROPE lets you ask about equivalence directly.

Check yourself

The leaderboard shows unpaired beside a run. What does that mean?

Show the answer

Answer: b

Paired comparison needs identical folds. Re-run with the experiment's current plan to compare fairly.

08

Compare runs

In the app

On the Leaderboard, tick two rows and press Compare 2. The table lists p (corrected t), p (Holm), P(better), P(equivalent), McNemar p (test), DeLong p (test) and a test-set interval. Below, one line per fold shows the pairs. Do this before believing that a lead is real.

Python
import numpy as np
import pandas as pd
from scipy import stats
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

df = pd.read_csv("pima.csv")
X, y = df.drop(columns="diabetic"), df["diabetic"]
cv = StratifiedKFold(5, shuffle=True, random_state=1)
a = cross_val_score(make_pipeline(StandardScaler(), LogisticRegression()), X, y, cv=cv, scoring="roc_auc")
b = cross_val_score(RandomForestClassifier(300, random_state=1), X, y, cv=cv, scoring="roc_auc")
d = a - b
J, ratio = len(d), 1 / 4
t_plain = d.mean() / np.sqrt(d.var(ddof=1) / J)
t_corr = d.mean() / np.sqrt(d.var(ddof=1) * (1 / J + ratio))
for name, t in [("plain", t_plain), ("corrected", t_corr)]:
    print(name, round(t, 2), round(2 * stats.t.sf(abs(t), J - 1), 3))
R
library(ranger)
df <- rbind(MASS::Pima.tr, MASS::Pima.te)
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
d <- sapply(1:5, function(k) {
  tr <- df[fold != k, ]; te <- df[fold == k, ]
  a <- predict(glm(type ~ ., binomial, tr), te, type = "response")
  b <- predict(ranger(type ~ ., tr, num.trees = 300, probability = TRUE, seed = 1), te)$predictions[, "Yes"]
  as.numeric(pROC::auc(te$type, a, quiet = TRUE)) - as.numeric(pROC::auc(te$type, b, quiet = TRUE))
})
J <- length(d)
t_plain <- mean(d) / sqrt(var(d) / J)
t_corr <- mean(d) / sqrt(var(d) * (1 / J + 1 / 4))
round(c(plain = t_plain, p_plain = 2 * pt(-abs(t_plain), J - 1), corrected = t_corr, p_corrected = 2 * pt(-abs(t_corr), J - 1)), 3)
09

Recap

Recap
  • Compare on shared folds, and treat fold scores as pairs.
  • A plain t on folds is too optimistic. The corrected t and the leaderboard's chips account for the overlap.
  • Ask what gap would matter, with a ROPE, and not only whether it's zero.

Concepts in this lesson