Sub topic 14
Quantifying uncertainty in AUC with a bootstrap
This chapter assumes you can compute a ROC curve and AUC with scikit-learn and that you know AUC is the probability of ranking a random positive above a random negative. It also assumes basic NumPy.
Why a single AUC is not enough
Suppose a model scores an AUC of 0.83 on a test set. That number is an estimate. A different test sample from the same population would give a different value. With a small test set, or very few positives, the difference can be large.
AUC depends on pairs of one positive and one negative example. If you have 30 positives, the result rests on only 30 positive scores. Comparing two models by their AUC values alone can therefore mislead you when the gap is smaller than the sampling noise.
The bootstrap idea
The bootstrap treats your test set as a stand-in for the population. You draw a new test set of the same size by sampling rows with replacement, compute the AUC on it, and repeat many times. The spread of those AUC values approximates how much your AUC would vary across real test samples.
A working example
The code below trains a model on an imbalanced synthetic dataset. It then bootstraps the test-set AUC 2000 times.
import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_auc_score
X, y = make_classification(
n_samples=1000, weights=[0.8, 0.2], random_state=0
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0, stratify=y
)
model = LogisticRegression(max_iter=1000).fit(X_train, y_train)
scores = model.predict_proba(X_test)[:, 1]
rng = np.random.default_rng(0)
n = len(y_test)
aucs = []
for _ in range(2000):
idx = rng.integers(0, n, n)
if len(np.unique(y_test[idx])) < 2:
continue # AUC is undefined with a single class
aucs.append(roc_auc_score(y_test[idx], scores[idx]))
aucs = np.array(aucs)
low, high = np.percentile(aucs, [2.5, 97.5])
print(f"AUC = {roc_auc_score(y_test, scores):.3f}")
print(f"95% interval: [{low:.3f}, {high:.3f}]")The 2.5th and 97.5th percentiles form a percentile confidence interval. A narrow interval means the estimate is stable. A wide one means the test set is too small to support fine distinctions.
Common errors
- Resampling the training data instead of the test set. Here you want the variability of the evaluation, so resample the held-out scores. Do not retrain the model on each resample.
- Skipping the single-class check. With few positives, a resample can contain none.
roc_auc_scorethen raises aValueError. - Too few repetitions. Use at least 1000 resamples so the interval endpoints are stable.
- Comparing two models with separate intervals. The models are scored on the same examples, so their AUCs are correlated. Bootstrap the difference instead. Use the same resampled indices for both models and take the percentiles of
auc_a - auc_b. If that interval contains 0, the data do not show a clear winner.
Reading the result
Report the AUC with its interval, for example "0.83 (95% CI 0.77 to 0.88)", and state the test-set size and the number of positives. If you are tuning thresholds or choosing between models, check whether the intervals overlap enough to make the difference meaningless.
Exercise
You compare model A and model B on the same test set. Their separate 95% bootstrap intervals overlap. Can you conclude there is no real difference between them? What should you compute instead?
Show answer
No. The AUCs come from the same examples and are positively correlated, so overlapping separate intervals do not rule out a real difference. Resample the test rows once per iteration, compute both AUCs on the same resample, and take the percentile interval of the paired difference. If that interval excludes 0, the difference is supported by the data.