Bootstrap and resampling

core 10 mininferencebootstrapresampling

Builds on:CLT and sampling distributions

The idea

Classical inference hands you standard-error formulas for a few blessed statistics — means, proportions, regression coefficients. The moment you care about a median, a ratio of two metrics, an AUC, or a p90, the formula book runs out. The bootstrap replaces the formula book with compute:

  1. From your sample of nn observations, draw nn observations with replacement (some rows repeat, others drop out).
  2. Compute your statistic on the resample.
  3. Repeat BB times (B≈1000B \approx 1000 to 10,00010{,}000). The distribution of the BB values approximates the sampling distribution of the statistic.

From there: the SD of the BB values is the bootstrap standard error; the 2.5th and 97.5th percentiles form the 95% percentile interval.

Why this is legitimate and not a free lunch: the plug-in principle. The true sampling distribution comes from repeatedly sampling the population; we cannot do that, so we substitute the best available estimate of the population — the empirical distribution of the sample itself — and sample from that instead. The approximation is good exactly to the extent that the sample resembles the population. The bootstrap quantifies uncertainty; it manufactures no new information, and it faithfully reproduces any bias your sample already has.

A detail interviewers probe: resamples must be size nn, with replacement. Sampling noise depends on nn, so simulating at a different size measures the wrong thing — and without replacement at size nn you would get the original sample back every time, showing zero variance. Each resample leaves out about (1−1/n)n≈e−1≈36.8%(1 - 1/n)^n \approx e^{-1} \approx 36.8\% of observations — the same "out-of-bag" fraction that bagging exploits for free validation.

Its cousin, the permutation test, shuffles group labels instead: it simulates the null world ("labels do not matter") to get a p-value, whereas the bootstrap simulates the sampling process to get SEs and CIs. Both are "resampling methods"; they answer different questions.

Worked example

Ten measured API latencies (ms), sorted:

12,  15,  21,  30,  45,  51,  62,  80,  110,  35012, \; 15, \; 21, \; 30, \; 45, \; 51, \; 62, \; 80, \; 110, \; 350

Sample median =(45+51)/2=48= (45 + 51)/2 = 48 ms. The 350 is real (a retry storm), so you want the median and honest uncertainty — but there is no s/ns/\sqrt{n} formula for medians.

Bootstrap by hand for two resamples (indices drawn with replacement):

Resample (values drawn)Median
15, 15, 21, 30, 45, 45, 62, 80, 110, 35045
12, 21, 30, 30, 51, 62, 62, 80, 110, 11056.5

Run B=10,000B = 10{,}000 of these and collect the medians. A typical run concentrates on the achievable values (30, 37.5, 45, 48, 51, 56.5, 62, ...) with a 95% percentile interval of roughly (21,80)(21, 80) ms. Notice what the bootstrap did automatically: the interval is asymmetric — wider above than below — because the data is right-skewed, something ±1.96×SE\pm 1.96 \times \text{SE} symmetry could never express. Also visible: with n=10n = 10 the bootstrap distribution is lumpy (medians of resamples can only hit averages of pairs of observed values) — a candid illustration that the bootstrap cannot conjure resolution the sample does not contain.

Cheat sheet

Use bootstrap forAvoid / adapt for
medians, quantiles (central)extreme order statistics: min, max, p99.9
ratios (revenue per session, CTR per user)time series → block bootstrap
correlation, AUC, F1, model metricsclustered data → resample clusters/users, not rows
skewed metrics at moderate nntiny nn — empirical distribution too coarse
SE when no formula existsbiased samples — bootstrap inherits the bias

Gotchas: