Confidence intervals and the bootstrap
What the 95% of a confidence interval really refers to, normal-approximation intervals for a mean and a proportion, the t distribution for small samples, the bootstrap for statistics without formulas, a paired comparison of two models, and how a confidence interval differs from a credible one.
How much to trust 0.86
The topic Bayesian inference ended with a credible interval. A prior and the data give a posterior, and the posterior says that lies in with probability 0.95. That statement needs a prior. This topic gives the other answer, which needs none.
Take a running example. A classifier answers 430 of 500 test examples correctly, so its accuracy on the test set is
Its true accuracy is the probability of a correct answer on a new example from the same distribution. A different test set of 500 examples would give a different : 0.85, perhaps, or 0.87. How far from can 0.86 be?
The frequentist view treats as a fixed, unknown number. It does not put a distribution on . Randomness lives in the data: the test set is a random sample, so and everything else computed from it are random variables. Any statement about uncertainty must therefore be a statement about what a procedure does across repeated samples. A confidence interval is such a procedure: two statistics and , computed from the data.
What 95% means
A 95% confidence interval is a pair of statistics such that, for every value of ,
The probability is over the data. The interval is random; the parameter is fixed. The left-hand side is the coverage of the procedure.
Picture it with the classifier. Suppose its true accuracy is 0.86. Draw 100 independent test sets of 500 examples and build an interval from each with the same recipe. The intervals have different centres. Over many such batches, about 95 in 100 contain 0.86 and about 5 miss it. In any one batch of 100 the count of misses varies around 5.
Now look at the data we actually have. Suppose the recipe gave . This particular interval either contains or it does not; nothing random is left. The sentence " lies in with probability 0.95" is the Bayesian reading. A confidence interval does not license it. The 0.95 belongs to the recipe, not to this one interval.
An interval is exact when the guarantee holds for every and every sample size. Most intervals used in practice are only nominal: their coverage approaches 0.95 as grows, but at a given and it can be a little lower or higher. The interval of the next block has coverage 0.943 at and .
An interval from the normal approximation
Most of our intervals come from the central limit theorem (see the topic The central limit theorem). Many estimators are approximately normal around the true value:
where the standard error is the standard deviation of the estimator. A normal variable lies within standard deviations of its mean with probability 0.95; is the 97.5% quantile of . So with probability about 0.95, and this gives the interval
The hat on is there because depends on unknown quantities and must itself be estimated.
A mean. For the standard error is . We estimate by the sample standard deviation , with the from the topic Estimators: bias and variance, and use .
A proportion. Accuracy is a mean of Bernoulli variables: if example is answered correctly. Each has variance , so . Plugging in gives the textbook interval for a proportion, the Wald interval:
For the classifier, , and the interval is , that is . The width shrinks like : a test set four times larger gives an interval half as wide.
When data are few
The recipe of the previous block leans on large . It breaks in two common places.
A mean of a few values. Train the same model 10 times with different random seeds and average the test accuracies. With 10 values, is itself noisy, and the interval must be wider to account for that. If the values are normal, the statistic has Student's distribution with degrees of freedom. Its tails are heavier than those of . The interval
then has coverage exactly 0.95. For the multiplier is instead of ; with the coverage would drop to 0.92. The difference fades as grows: .
A proportion near 0 or 1. Take a model with error rate and a test set of 20 examples. The Wald interval for the error rate covers the true value only with probability 0.88, not 0.95. One reason: with probability the model makes no errors at all, , and the interval collapses to the single point 0. The Wilson interval fixes most of this. It solves for instead of plugging into the standard error, and has coverage 0.96 in the same setting. It is the better default for accuracies and click rates on small test sets.
The bootstrap
Many statistics have no convenient formula for their standard error: a median, the AUC or the F1 score of a classifier, the gain of one model over another. The bootstrap gets the standard error by simulation.
The idea is simple. To see how a statistic varies, we would draw many test sets from the population. We have only one. So we let the sample stand in for the population: the empirical distribution puts mass on each observation. Drawing from it means drawing from the data with replacement. The recipe:
- Draw values from the sample with replacement. This is a resample: some values repeat, others are missing.
- Compute the statistic on the resample.
- Repeat times, typically 1,000 to 10,000.
The standard deviation of the values estimates the standard error. Their 2.5% and 97.5% percentiles give the 95% percentile interval.
On the classifier the bootstrap agrees with the formula: it resamples the 500 per-example results, and gives a standard error of 0.0155 and the interval . Its value shows where there is no formula. Take 50 values from an exponential distribution, whose true median is . One such sample has median 0.549. From that sample alone the bootstrap estimates the standard error as 0.136; the true value, from 100,000 fresh samples, is 0.140. The percentile interval covers the true median. The bootstrap never learned that the data were exponential.
When the bootstrap fails
The bootstrap works when the statistic depends smoothly on the distribution of the data and the sample is reasonably large. It fails, or needs a change, in a few well-known cases.
- Extremes. The maximum of a resample can never exceed the sample maximum, and it equals it whenever the largest value is drawn at least once, which happens with probability . The bootstrap distribution of the maximum looks nothing like the true one.
- Small samples. With 10 observations the empirical distribution is a poor stand-in for the population, and percentile intervals undercover.
- Dependent data. Resampling single observations destroys dependence. A time series needs the block bootstrap, which resamples runs of consecutive points. Data in groups, such as several examples per user or per document, need resampling of whole groups.
- Heavy tails. For the mean of data with infinite variance, where the central limit theorem itself fails, the bootstrap is inconsistent.
One limit applies always. The bootstrap cannot see beyond the sample. If the test set is unrepresentative, say all examples come from one domain, resampling it reproduces the same error with great confidence.
Two models on one test set
Two classifiers are evaluated on the same 1,000 test examples. Model A scores 0.833, model B scores 0.859. Is B really better?
Separate Wald intervals, and , overlap. But they ignore that the two models face the same examples. Hard examples are hard for both, so their errors are strongly correlated. The models disagree on only 104 examples: B is right and A wrong on 65, A is right and B wrong on 39. The whole difference sits in those 104 examples.
The paired bootstrap uses this. It resamples examples, keeps both models' results on each resampled example together, and computes the difference in accuracy on each resample. Its 95% interval for the difference is , which excludes zero. Resampling the two models separately, as if their results were independent, gives , which includes zero.
Why the gap? For the difference of two results on the same example,
and the covariance is large and positive. The same principle, compare on the same units and resample the units, applies to A/B tests with repeated measurements and to evaluations across prompts or documents.
Two kinds of interval and what comes next
We now have two kinds of interval for the same accuracy.
- A credible interval treats as uncertain and the data as fixed. Given the observed data , it says , and it needs a prior.
- A confidence interval treats as fixed and the data as random. Its 0.95 is the coverage of the procedure, and it needs no prior.
With much data and a weak prior they nearly coincide. For the classifier with a uniform prior , the posterior is and its 95% credible interval is ; the Wald interval is . The numbers almost match, but they answer different questions. They part ways when data are scarce or the prior is strong: on 20 test examples, a prior built from many earlier models can move a credible interval far from the confidence interval.
Intervals also lead to decisions. In the previous block the 95% interval for the gain of model B excluded zero. That amounts to rejecting the hypothesis "no difference" at the 5% level. It does not show that the gain is large enough to matter, and it means less if it was one of many intervals we looked at: twenty independent 95% intervals all cover with probability only 0.36. The next topic, Hypothesis tests and A/B testing, makes this decision explicit: null hypotheses, p-values and what they are not, the power to detect an effect, and the A/B test.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.