Simple Algorithm
Machine Learning · Convergence · 26 min

The Bootstrap

Resampling your data to measure uncertainty.

You have one sample and a statistic computed from it. How uncertain is that number? Efron's bootstrap answers by treating the sample as if it were the population and resampling it. This lesson makes the plug-in idea precise, builds standard errors and confidence intervals, proves when the bootstrap works and shows where it breaks, and connects it to estimating prediction error and to bagging.

What you’ll learn

  • State the plug-in principle, compute bootstrap standard errors by Monte Carlo resampling, and separate sampling error from Monte Carlo error.
  • Construct normal, percentile, basic, studentized and BCa intervals, and compare their coverage accuracy.
  • Prove that the bootstrap is consistent for the sample mean, and show why it fails for the sample maximum and for means of heavy-tailed data.
  • Derive the leave-one-out bootstrap, .632 and .632+ estimators of prediction error, and interpret bagging as bootstrap smoothing.

The plug-in principle

Data x1,…,xnx_1,\dots,x_n are i.i.d. from an unknown distribution FF, and a statistic θ^=t(F^n)\hat\theta=t(\hat F_n) estimates θ=t(F)\theta=t(F), where F^n\hat F_n puts mass 1/n1/n on each observation. How accurate θ^\hat\theta is depends on its sampling distribution, which depends on the unknown FF. The bootstrap (Efron, 1979) plugs in F^n\hat F_n for FF: draw a bootstrap sample x1∗,…,xn∗x_1^*,\dots,x_n^* from F^n\hat F_n, which means nn draws with replacement from the data, recompute θ^∗=t(F^n∗)\hat\theta^*=t(\hat F_n^*), and let the distribution of θ^∗−θ^\hat\theta^*-\hat\theta given the data stand in for that of θ^−θ\hat\theta-\theta.

DerivationThe sample mean, exactly
  1. E∗[xˉ∗]=xˉ,Var⁡∗(xˉ∗)=σ^2n\E^*\big[\bar x^*\big]=\bar x,\qquad\Var^*\big(\bar x^*\big)=\frac{\hat\sigma^2}{n}

    Under F^n\hat F_n each draw xj∗x_j^* has mean xˉ\bar x and variance σ^2=1n∑i(xi−xˉ)2\hat\sigma^2=\frac1n\sum_i(x_i-\bar x)^2, and the draws are independent, so

  2. seboot(xˉ)=σ^n\mathrm{se}_{\text{boot}}(\bar x)=\frac{\hat\sigma}{\sqrt n}

    Hence seboot(xˉ)=σ^/n\mathrm{se}_{\text{boot}}(\bar x)=\hat\sigma/\sqrt n: the textbook formula, with divisor nn instead of n−1n-1 inside σ^\hat\sigma. For the mean the bootstrap adds nothing; its value is that the same recipe works for a median, a correlation, a ratio, or the output of a whole learning pipeline, where no formula exists.

  3. #{distinct bootstrap samples}=(2n−1n),n=10: 92,378\#\{\text{distinct bootstrap samples}\}=\binom{2n-1}{n},\qquad n=10:\ 92{,}378

    A bootstrap sample is a multiset of size nn drawn from nn points, so there are only (2n−1n)\binom{2n-1}{n} distinct ones, and for tiny nn the ideal bootstrap could be computed exactly. Otherwise a couple of hundred replicates usually suffice for a standard error, and a thousand or more for interval endpoints, which live in the tails (Efron & Tibshirani, 1993).

python
import numpy as np
rng = np.random.default_rng(0)

def bootstrap(x, stat, B=2000):
    n = len(x)
    return np.array([stat(x[rng.integers(0, n, n)]) for _ in range(B)])

reps = bootstrap(x, np.median)               # x: a 1-D NumPy array
se = reps.std(ddof=1)                        # bootstrap standard error
lo, hi = np.percentile(reps, [2.5, 97.5])    # 95% percentile interval

A nonparametric bootstrap of the median. rng.integers(0, n, n) draws nn indices with replacement.

Confidence intervals

Write θ^(α)∗\hat\theta^*_{(\alpha)} for the α\alpha-quantile of the bootstrap replicates, zα=Φ−1(α)z_\alpha=\Phi^{-1}(\alpha), and se^\widehat{\mathrm{se}} for the bootstrap standard error. The common confidence intervals of nominal level 1−2α1-2\alpha:

IntervalEndpointsIdea
Normalθ^±z1−α se^\hat\theta\pm z_{1-\alpha}\,\widehat{\mathrm{se}}assumes θ^\hat\theta is approximately normal
Percentile[θ^(α)∗, θ^(1−α)∗]\big[\hat\theta^*_{(\alpha)},\ \hat\theta^*_{(1-\alpha)}\big]read the interval off the bootstrap distribution; respects monotone transformations
Basic[2θ^−θ^(1−α)∗, 2θ^−θ^(α)∗]\big[2\hat\theta-\hat\theta^*_{(1-\alpha)},\ 2\hat\theta-\hat\theta^*_{(\alpha)}\big]treat θ^∗−θ^\hat\theta^*-\hat\theta as a copy of θ^−θ\hat\theta-\theta, and invert
Studentized (bootstrap-tt)[θ^−t(1−α)∗se^, θ^−t(α)∗se^]\big[\hat\theta-t^*_{(1-\alpha)}\widehat{\mathrm{se}},\ \hat\theta-t^*_{(\alpha)}\widehat{\mathrm{se}}\big], with t∗=(θ^∗−θ^)/se^∗t^*=(\hat\theta^*-\hat\theta)/\widehat{\mathrm{se}}^*bootstrap an approximate pivot; needs a standard error inside every replicate
BCa[θ^(α1)∗, θ^(α2)∗]\big[\hat\theta^*_{(\alpha_1)},\ \hat\theta^*_{(\alpha_2)}\big]percentile interval at levels adjusted for bias and skewness (Efron, 1987)
α1,2=Φ(z^0+z^0+zα, 1−α1−a^ (z^0+zα, 1−α)),z^0=Φ−1(#{b:θ^b∗<θ^}B),a^=∑i(θ^(⋅)−θ^(i))36[∑i(θ^(⋅)−θ^(i))2]3/2\alpha_{1,2}=\Phi\Big(\hat z_0+\frac{\hat z_0+z_{\alpha,\,1-\alpha}}{1-\hat a\,(\hat z_0+z_{\alpha,\,1-\alpha})}\Big),\qquad\hat z_0=\Phi^{-1}\Big(\frac{\#\{b:\hat\theta^*_b<\hat\theta\}}{B}\Big),\qquad\hat a=\frac{\sum_i\big(\hat\theta_{(\cdot)}-\hat\theta_{(i)}\big)^3}{6\Big[\sum_i\big(\hat\theta_{(\cdot)}-\hat\theta_{(i)}\big)^2\Big]^{3/2}}
The BCa levels. The bias correction z^0\hat z_0 measures the median bias of the replicates. The acceleration a^\hat a, computed from the jackknife values θ^(i)\hat\theta_{(i)} (the statistic with observation ii deleted; θ^(⋅)\hat\theta_{(\cdot)} is their mean), measures how the standard error changes with θ\theta, which shows up as skewness. With z^0=a^=0\hat z_0=\hat a=0, BCa reduces to the percentile interval.
Check your understanding

The bootstrap distribution of your statistic is strongly skewed and its median sits well away from θ^\hat\theta. Which interval is designed for this situation?

  1. The normal interval θ^±1.96 se^\hat\theta\pm1.96\,\widehat{\mathrm{se}}
  2. The percentile interval, because it reads the skewness directly off the replicates
  3. Any of them: all bootstrap intervals agree in large samples
  4. The BCa interval, which adjusts the percentile levels for bias (z^0\hat z_0) and skewness (a^\hat a)
Show the answer

D. The BCa interval, which adjusts the percentile levels for bias (z^0\hat z_0) and skewness (a^\hat a)

A replicate median far from θ^\hat\theta signals median bias, which z^0\hat z_0 corrects, and skewness that changes with θ\theta is what the acceleration a^\hat a captures. BCa is second-order accurate and transformation-respecting; the studentized interval is the other second-order option.

Why it works, and when it fails

DerivationProof sketch for the first statement
  1. n(xˉ∗−xˉ)=1n∑j=1n(xj∗−xˉ),E∗=0,Var⁡∗=σ^2→σ2\sqrt n(\bar x^*-\bar x)=\frac1{\sqrt n}\sum_{j=1}^n(x_j^*-\bar x),\qquad\E^*=0,\qquad\Var^*=\hat\sigma^2\to\sigma^2

    Conditionally on the data, n(xˉ∗−xˉ)\sqrt n(\bar x^*-\bar x) is a normalized sum of nn i.i.d. draws with mean 0 and variance σ^2\hat\sigma^2, and σ^2→σ2\hat\sigma^2\to\sigma^2 almost surely by the strong law of large numbers:

  2. 1nσ^2∑i=1n(xi−xˉ)2 1{∣xi−xˉ∣>εn σ^} ⟶ 0a.s.\frac1{n\hat\sigma^2}\sum_{i=1}^n(x_i-\bar x)^2\,\mathbf 1\big\{\abs{x_i-\bar x}>\varepsilon\sqrt n\,\hat\sigma\big\}\ \longrightarrow\ 0\quad\text{a.s.}

    The summands form a triangular array, since their distribution F^n\hat F_n changes with nn, so check Lindeberg's condition. With Ex2<∞\E x^2<\infty, max⁡i≤n∣xi∣/n→0\max_{i\le n}\abs{x_i}/\sqrt n\to0 almost surely, so for any ε>0\varepsilon>0 the truncated second moment is eventually exactly zero:

  3. sup⁡t∣Pr⁡∗(n(xˉ∗−xˉ)≤t)−Φ(t/σ)∣ ⟶ 0a.s.\sup_t\Big|\Pr{}^*\big(\sqrt n(\bar x^*-\bar x)\le t\big)-\Phi(t/\sigma)\Big|\ \longrightarrow\ 0\quad\text{a.s.}

    Lindeberg's central limit theorem gives the conditional limit N(0,σ2)\mathcal N(0,\sigma^2), and since the limit is continuous, Pólya's theorem upgrades this to uniform convergence of distribution functions. Bickel & Freedman proved much more, for functionals that are smooth in a suitable sense, using the Mallows distance between distributions.

Where it fails. Take xi∼Uniform(0,θ)x_i\sim\mathrm{Uniform}(0,\theta) and the maximum x(n)x_{(n)}. Then n(θ−x(n))/θn(\theta-x_{(n)})/\theta converges to an exponential distribution, which is continuous. But a bootstrap sample contains the observed maximum with probability 1−(1−1/n)n→1−e−1≈0.6321-(1-1/n)^n\to1-e^{-1}\approx0.632, so the bootstrap distribution of n(x(n)−x(n)∗)n(x_{(n)}-x^*_{(n)}) has an atom of mass near 0.632 at zero, and it cannot converge to the continuous limit. (Bickel & Freedman, 1981, also discuss counterexamples, cases where the bootstrap is inconsistent.) The trouble is that F^n\hat F_n is a poor stand-in for FF exactly where the statistic looks: at the edge of the support.

Predict before you look

With n=50n=50 observations, what is the probability that a bootstrap sample contains the observed maximum, so that x(n)∗=x(n)x^*_{(n)}=x_{(n)}?

Show the answer

64 %

1−(1−1/50)50=1−0.9850≈1−0.364=0.6361-(1-1/50)^{50}=1-0.98^{50}\approx1-0.364=0.636, already close to the limit 1−e−1≈0.6321-e^{-1}\approx0.632. Almost two thirds of the replicates of the maximum are identical, while the true sampling distribution has no atom at all.

Other failures follow the same pattern. For the mean of heavy-tailed data with infinite variance (in the domain of attraction of a stable law with index below 2), the bootstrap distribution converges to a random limit rather than the true one (Athreya, 1987). Parameters on the boundary of the parameter space and non-smooth functionals cause similar trouble. For dependent data, resampling single observations destroys the dependence; block bootstraps resample contiguous blocks instead (Künsch, 1989). A widely applicable repair is to resample fewer points: subsampling mm of the nn observations without replacement, with m→∞m\to\infty and m/n→0m/n\to0, is consistent under very weak conditions (Politis & Romano, 1994), and the mm-out-of-nn bootstrap works similarly, at the price of choosing mm.

Bootstrap estimates of prediction error

Error estimation adds a twist: the statistic is the test error of a fitted model. Training on a bootstrap sample and testing on the original data is optimistic, because about 63% of the test points were also training points. The leave-one-out bootstrap tests each example only on fits whose bootstrap samples did not contain it. With C−iC^{-i} the set of bootstrap samples that exclude example ii:

Err^(1)=1n∑i=1n1∣C−i∣∑b∈C−iL(yi,f^∗b(xi))\widehat{\mathrm{Err}}^{(1)}=\frac1n\sum_{i=1}^n\frac1{\abs{C^{-i}}}\sum_{b\in C^{-i}}L\big(y_i,\hat f^{*b}(\mathbf x_i)\big)
For bagged trees, averaging the trees in C−iC^{-i} before taking the loss gives the out-of-bag error. Each fit sees only about 0.632n0.632n distinct examples, so Err^(1)\widehat{\mathrm{Err}}^{(1)} behaves like the error of a model trained on fewer points: it is pessimistic, roughly like twofold cross-validation.
Err^(.632)=0.368⋅err‾+0.632⋅Err^(1)\widehat{\mathrm{Err}}^{(.632)}=0.368\cdot\overline{\mathrm{err}}+0.632\cdot\widehat{\mathrm{Err}}^{(1)}
Efron's (1983) estimator pulls the pessimistic leave-one-out bootstrap toward the optimistic training error err‾\overline{\mathrm{err}}. The constant is the limiting probability 1−(1−1/n)n1-(1-1/n)^n that an example appears in a bootstrap sample.
DerivationWhere .632 fails, and the .632+ repair
  1. Err^(.632)=0.368⋅0+0.632⋅0.5=0.316\widehat{\mathrm{Err}}^{(.632)}=0.368\cdot0+0.632\cdot0.5=0.316

    Take labels that are pure noise, two equally likely classes, and a 1-nearest-neighbor classifier. The training error is 0 and the true error is 0.5; the leave-one-out bootstrap is about 0.5, since each example is predicted from other, unrelated examples. The .632 estimator is far too optimistic:

  2. R^=Err^(1)−err‾γ^−err‾∈[0,1](clipped to this range)\hat R=\frac{\widehat{\mathrm{Err}}^{(1)}-\overline{\mathrm{err}}}{\hat\gamma-\overline{\mathrm{err}}}\in[0,1]\qquad(\text{clipped to this range})

    Efron & Tibshirani (1997) measure how badly a model overfits. The no-information error γ^\hat\gamma is the error if inputs and labels were independent, γ^=∑kp^k(1−q^k)\hat\gamma=\sum_k\hat p_k(1-\hat q_k), where p^k\hat p_k is the fraction of labels in class kk and q^k\hat q_k the fraction of predictions; the relative overfitting rate is

  3. Err^(.632+)=(1−w^) err‾+w^ Err^(1),w^=0.6321−0.368 R^\widehat{\mathrm{Err}}^{(.632+)}=(1-\hat w)\,\overline{\mathrm{err}}+\hat w\,\widehat{\mathrm{Err}}^{(1)},\qquad\hat w=\frac{0.632}{1-0.368\,\hat R}

    The .632+ estimator gives the leave-one-out bootstrap more weight as overfitting grows:

  4. R^=1 ⟹ w^=0.6320.632=1 ⟹ Err^(.632+)=Err^(1)=0.5\hat R=1\ \Longrightarrow\ \hat w=\frac{0.632}{0.632}=1\ \Longrightarrow\ \widehat{\mathrm{Err}}^{(.632+)}=\widehat{\mathrm{Err}}^{(1)}=0.5

    With no overfitting (R^=0\hat R=0) the weight is 0.632, recovering the .632 estimator. In the 1-NN noise example γ^=0.5\hat\gamma=0.5 and R^=1\hat R=1, so w^=1\hat w=1 and the estimate is the honest 0.5.

Bagging as bootstrap smoothing

Bagging replaces a predictor by its bootstrap mean, f^bag(x)=E∗[f^(x;D∗)]\hat f_{\text{bag}}(\mathbf x)=\E^*\big[\hat f(\mathbf x;\data^*)\big], approximated with BB resamples; Breiman (1996) named it bootstrap aggregation. Efron (2014) studied the same operation for estimates computed after data-driven model selection, calling it bootstrap smoothing: averaging over resamples removes the jumps that selection introduces. He also gave a standard error for the smoothed estimate that needs no second level of resampling. Let Nbi∗N^*_{bi} be the number of times example ii appears in bootstrap sample bb, and tb∗t^*_b the corresponding replicate:

Var⁡^IJ=∑i=1nCov⁡^b(Nbi∗, tb∗)2\widehat{\Var}_{\mathrm{IJ}}=\sum_{i=1}^n\widehat{\Cov}_b\big(N^*_{bi},\,t^*_b\big)^2
The infinitesimal jackknife estimate of the variance of a bagged estimate: for each example, the covariance across resamples between how often it was drawn and the replicate it produced. Wager, Hastie & Efron (2014) adapted it, with a correction for the Monte Carlo noise of finite BB, to give confidence intervals for random-forest predictions (Random Forest Theory).
Think first Why can the same resamples serve both to compute the bagged predictor and to estimate its variance, when the bootstrap standard error of an ordinary statistic needs its own resampling?

The bagged predictor is itself a bootstrap average, a smooth function of the resampling weights Nbi∗N^*_{bi}. Its sensitivity to each observation's weight can be read off from how the replicates covary with the counts, which is exactly the directional derivative the infinitesimal jackknife needs. For an unsmoothed statistic there is no such average to differentiate, so a separate bootstrap is required.

Key takeaways

  • The bootstrap plugs in the empirical distribution for the unknown one: resample the data with replacement, recompute the statistic, and use the replicates' spread. For the mean it gives σ^/n\hat\sigma/\sqrt n; its value is that it works where no formula exists.
  • Percentile, basic and normal intervals are first-order accurate; studentized and BCa intervals are second-order accurate, with one-sided coverage error O(n−1)\order{n^{-1}} (Hall, 1988). BCa corrects for bias z^0\hat z_0 and acceleration a^\hat a.
  • The bootstrap is consistent for the mean under finite variance (Bickel & Freedman, 1981) and more accurate than the normal approximation for standardized means (Singh, 1981). It fails for the sample maximum (an atom of mass about 0.632), for heavy-tailed means, at boundaries and for dependent data; subsampling and block bootstraps repair many cases.
  • The leave-one-out bootstrap (the out-of-bag error, for bagging) is pessimistic; .632 blends in the training error, and .632+ adapts the weight to the relative overfitting rate.
  • Bagging is bootstrap smoothing, and its variance can be estimated from the same resamples by the infinitesimal jackknife, ∑iCov⁡^(Nbi∗,tb∗)2\sum_i\widehat{\Cov}(N^*_{bi},t^*_b)^2.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

What is the ideal bootstrap standard error of the sample mean xˉ\bar x of nn observations?

  1. σ^/n\hat\sigma/\sqrt n with σ^2=1n∑i(xi−xˉ)2\hat\sigma^2=\frac1n\sum_i(x_i-\bar x)^2
  2. s/n−1s/\sqrt{n-1} with s2=1n−1∑i(xi−xˉ)2s^2=\frac1{n-1}\sum_i(x_i-\bar x)^2
  3. σ^/n\hat\sigma/n
  4. Zero, because resampling cannot change the mean
Show the answer

A. σ^/n\hat\sigma/\sqrt n with σ^2=1n∑i(xi−xˉ)2\hat\sigma^2=\frac1n\sum_i(x_i-\bar x)^2

Each bootstrap draw has variance σ^2\hat\sigma^2 under the empirical distribution, which uses divisor nn, and the nn draws are independent, so Var⁡∗(xˉ∗)=σ^2/n\Var^*(\bar x^*)=\hat\sigma^2/n.

Question 2

Why does the nonparametric bootstrap fail for the maximum of a Uniform(0,θ)(0,\theta) sample?

  1. The maximum has infinite variance
  2. About 63% of bootstrap samples contain the observed maximum, giving the bootstrap distribution an atom at zero that the continuous true limit does not have
  3. Too few bootstrap replicates are used in practice
  4. The bootstrap requires normally distributed data
Show the answer

B. About 63% of bootstrap samples contain the observed maximum, giving the bootstrap distribution an atom at zero that the continuous true limit does not have

Pr⁡∗(x(n)∗=x(n))=1−(1−1/n)n→0.632\Pr^*(x^*_{(n)}=x_{(n)})=1-(1-1/n)^n\to0.632, while n(θ−x(n))/θn(\theta-x_{(n)})/\theta converges to a continuous exponential law. The empirical distribution is a poor proxy for FF at the edge of the support, which is where the maximum lives.

Question 3

A 1-nearest-neighbor classifier is trained on two balanced classes whose labels are pure noise. What do the .632 and .632+ estimators report?

  1. Both report about 0.5
  2. .632 reports about 0.5 and .632+ about 0.316
  3. .632 reports about 0.316, too optimistic, and .632+ about 0.5, because its relative overfitting rate is 1
  4. Both report 0, the training error
Show the answer

C. .632 reports about 0.316, too optimistic, and .632+ about 0.5, because its relative overfitting rate is 1

The training error is 0 and the leave-one-out bootstrap is about 0.5, so 0.368⋅0+0.632⋅0.5=0.3160.368\cdot0+0.632\cdot0.5=0.316. The .632+ estimator computes R^=(0.5−0)/(0.5−0)=1\hat R=(0.5-0)/(0.5-0)=1, so w^=1\hat w=1, and it reports the leave-one-out bootstrap, 0.5.

Question 4

Which bootstrap intervals are second-order accurate, with one-sided coverage error O(n−1)\order{n^{-1}} for smooth functions of means?

  1. The normal and percentile intervals
  2. The basic and percentile intervals
  3. All bootstrap intervals
  4. The studentized (bootstrap-tt) and BCa intervals
Show the answer

D. The studentized (bootstrap-tt) and BCa intervals

Hall (1988) showed that one-sided normal, percentile and basic intervals have coverage error of order n−1/2n^{-1/2}, while studentizing or correcting for bias and acceleration gains an order. Two-sided intervals of all kinds have O(n−1)\order{n^{-1}} coverage error, by cancellation between the tails.

Further reading

  • B. Efron, "Bootstrap methods: another look at the jackknife," Annals of Statistics 7(1), 1–26 (1979).
  • B. Efron & R. J. Tibshirani, An Introduction to the Bootstrap (Chapman & Hall, 1993).
  • P. J. Bickel & D. A. Freedman, "Some asymptotic theory for the bootstrap," Annals of Statistics 9(6), 1196–1217 (1981).
  • B. Efron, "Better bootstrap confidence intervals," Journal of the American Statistical Association 82(397), 171–185 (1987).
  • B. Efron & R. Tibshirani, "Improvements on cross-validation: the .632+ bootstrap method," Journal of the American Statistical Association 92(438), 548–560 (1997).