Search

Search papers and lessons

Bootstrap Sampling

Level 2 · 12 min read

Bootstrap sampling estimates how uncertain a statistic is by resampling your own data with replacement, instead of collecting new data.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Sampling
  • Sign in to see your progress

TL;DR

You have one dataset, and you want to know how much a number computed from it could vary. The bootstrap answers this by treating the dataset as a stand-in for the wider population: you draw new samples of the same size from it, with replacement, recompute the number each time, and look at how much those recomputed values spread out.

The method needs no formula for the uncertainty and works for almost any statistic, including medians, correlations and model coefficients that have no tidy textbook error formula.

Where you'll see it: in error bars on A/B test results, in the confidence intervals that statistics libraries report for medians and regression coefficients, and in bagging, where each tree in a random forest is trained on its own bootstrap sample.

Intuition

Sampling from the sample

Suppose you measure the response time of a server on five requests and get 12, 15, 9, 30 and 14 milliseconds. The average is 16 ms. How much would that average change if you had measured five different requests? Ideally you would just repeat the experiment many times and watch the spread. Usually you cannot.

The bootstrap's move is to pretend your five measurements are a good picture of all possible requests. If that is roughly true, then drawing from your five numbers is a cheap way to simulate new experiments. You draw five values from the list, one at a time, and after each draw you put the value back. That "put it back" is the replacement. Some values come up twice, some not at all, and the resulting list is a plausible-looking dataset of the same size. Compute its average, repeat a thousand times, and the thousand averages show you the likely range of the true average.

The idea that a sample can stand in for the population is the same reasoning that powers the Monte Carlo method. The difference is where the samples come from. Monte Carlo draws from a model you wrote down. The bootstrap draws from the data you already have.

Why replacement matters

Without replacement, every resample would be a shuffle of the original list, and every resample would have exactly the same mean. No spread, no uncertainty. Replacement lets each resample differ, and the differences between resamples are what we measure.

10203040500.50.60.70.80.91n, the sample sizeshare of distinct values
  • share of distinct originals in one resample
  • limit as n grows
A resample of n values drawn with replacement contains about 63% of the distinct original values for large n. For n = 3 the share is 19/27, about 70%.

Each resample of size nn leaves out a given original value with probability (1−1/n)n(1 - 1/n)^n, which approaches e−1≈0.368e^{-1} \approx 0.368 for large nn. So a typical resample contains about 63% of the distinct originals, and the rest are duplicates. That is the source of variation.

The shape of the procedure

flowchart LR
  A["Original sample of n values"] --> B["Draw n values with replacement"]
  B --> C["Compute the statistic (mean, median, ...)"]
  C --> D["Repeat B times, often 1000 or more"]
  D --> E["Use the spread of the B values as uncertainty"]

The math

Setup

Let x1,…,xnx_1, \dots, x_n be the observed sample and let θ^=s(x1,…,xn)\hat{\theta} = s(x_1, \dots, x_n) be the statistic we care about, such as the mean. The true value we would like to know is θ\theta, the same statistic computed on the whole population. We want the sampling distribution of θ^\hat{\theta}, meaning how it would vary across repeated samples of size nn.

The bootstrap sample

A bootstrap sample x1∗,…,xn∗x^*_1, \dots, x^*_n is drawn from the observed values, each draw independently and with equal probability 1/n1/n. Write the bootstrap replicate of the statistic as θ^b∗=s(x1∗,…,xn∗)\hat{\theta}^*_b = s(x^*_1, \dots, x^*_n) for replicate bb.

The empirical distribution function F^n\hat{F}_n puts probability 1/n1/n on each observed value. Drawing from it is exactly what a bootstrap sample does, so the bootstrap replaces the unknown population distribution FF with F^n\hat{F}_n. The cumulative distribution function (CDF) of F^n\hat{F}_n is known to you; the one of FF is not.

Estimating the standard error

After BB replicates, the bootstrap standard error is the sample standard deviation of the replicates:

SE^boot=1B−1∑b=1B(θ^b∗−θˉ∗)2,θˉ∗=1B∑b=1Bθ^b∗.\widehat{\mathrm{SE}}_{\text{boot}} = \sqrt{\frac{1}{B-1} \sum_{b=1}^{B} \left(\hat{\theta}^*_b - \bar{\theta}^*\right)^2}, \qquad \bar{\theta}^* = \frac{1}{B} \sum_{b=1}^{B} \hat{\theta}^*_b.

As BB grows, this estimate settles down. The remaining error comes from using finitely many resamples, and it shrinks like 1/B1/\sqrt{B}. The bigger source of error is whether F^n\hat{F}_n is a good stand-in for FF, which depends on nn and on the data.

Percentile confidence intervals

Sort the BB replicates. A 95% percentile interval runs from the 2.5th to the 97.5th percentile of the replicates:

[θ^(0.025)∗, θ^(0.975)∗].\left[\hat{\theta}^*_{(0.025)},\ \hat{\theta}^*_{(0.975)}\right].

This needs no assumption that the statistic is Gaussian, which is its main appeal. It is also fragile for small nn, which we return to in the tradeoffs section.

Why it works for the mean

For the sample mean, the bootstrap standard error is close to the textbook answer s/ns/\sqrt{n}, where ss is the sample standard deviation. You can see why from the variance rules. Each bootstrap draw has mean xˉ\bar{x} and variance 1n∑i(xi−xˉ)2\frac{1}{n}\sum_i (x_i - \bar{x})^2, so the mean of nn draws has variance 1n∑i(xi−xˉ)2/n\frac{1}{n}\sum_i (x_i - \bar{x})^2 / n. This is the same formula that the variance of a sample mean gives, with the population variance replaced by its plug-in estimate. The bootstrap is worth using precisely when no such closed form exists.

Worked examples

Example
A tiny bootstrap by hand

Take three measurements: x=(2,4,9)x = (2, 4, 9). The sample mean is xˉ=5\bar{x} = 5. Draw five resamples of size 3, with replacement, and write down the mean of each.

  1. Resample 1: (2,4,4)(2, 4, 4), mean 10/3≈3.33310/3 \approx 3.333.
  2. Resample 2: (9,2,9)(9, 2, 9), mean 20/3≈6.66720/3 \approx 6.667.
  3. Resample 3: (4,9,2)(4, 9, 2), mean 5.0005.000.
  4. Resample 4: (9,9,9)(9, 9, 9), mean 9.0009.000.
  5. Resample 5: (2,4,9)(2, 4, 9), mean 5.0005.000.

The average of the five replicate means is 29/5=5.829/5 = 5.8. Their deviations from 5.85.8 are −2.467-2.467, 0.8670.867, −0.800-0.800, 3.2003.200 and −0.800-0.800. The squared deviations sum to 18.35618.356, and dividing by B−1=4B - 1 = 4 gives 4.5894.589. The square root is SE^≈2.14\widehat{\mathrm{SE}} \approx 2.14.

Interpretation: the mean of these three numbers is about 5, and the typical error is about 2. Five resamples is far too few for a real analysis. The point is that the procedure ran on nothing but the three numbers you started with.

Example
A 95% interval for a median of server latency

You measured 11 latencies in milliseconds: 9, 11, 12, 12, 13, 14, 15, 15, 18, 22, 30. The median is 14 ms. You want a 95% interval for the true median.

  1. Draw B=1000B = 1000 resamples of size 11 with replacement and compute the median of each.
  2. Sort the 1000 medians. The 2.5th percentile is the 25th value, and the 97.5th percentile is the 975th value, counting from the bottom.
  3. Suppose the 25th value is 12 and the 975th is 18. The interval is [12,18][12, 18] ms.
  4. Interpretation: under the bootstrap approximation, values of the true median between 12 and 18 ms are consistent with these data. An interval like this need not be symmetric around 14: resampled medians can only take values from the data, and the data have a long right tail.

A caution that matters here: with only 11 points, the percentile interval is rough. Treat the endpoints as approximate, not exact to the millisecond.

Tradeoffs and failure modes

The bootstrap is a good default when you have a single dataset and an unusual statistic. It goes wrong in predictable ways.

SituationProblemBetter choice
Very small nn (under about 10 to 20)The sample is too thin to stand in for the population; intervals are too narrowExact methods if the model is known; a parametric model with honest assumptions
Heavy tails, such as income or latencyExtreme values dominate; resamples may miss or repeat them unpredictablyReport a robust statistic, or use a larger sample
Dependent data, such as time seriesResampling single points destroys the ordering and correlationBlock bootstrap, which resamples contiguous chunks
Statistic is not smooth, such as the maxThe bootstrap distribution can be badly wrongExtreme value theory, or a different target
Data where observations are not independent of one another (clustered users, repeated measures)Resampling rows treats correlated rows as independent, so intervals are too narrowResample at the cluster level
Needing a single exact answer for a textbook statisticSimulation noise adds run-to-run variationUse the closed form, and use the bootstrap only to check it

The most common mistake is trusting a percentile interval from a few dozen points without a second look. The second most common is reporting a bootstrap interval from B=50B = 50 and forgetting that the endpoints move every time you rerun the code. Fix the random seed and use enough resamples.

History

Resampling ideas predate the bootstrap. The "jackknife", which recomputes a statistic with one observation left out at a time, was developed in the 1940s and 1950s by Quenouille and Tukey. The bootstrap itself was named and analysed by Bradley Efron in a 1979 paper in the Annals of Statistics, which showed that resampling could estimate standard errors and sampling distributions for statistics where theory was hard or absent.

It caught on for two reasons. Computers had become fast enough to run thousands of resamples in reasonable time, and the method asked nothing of the analyst except the ability to compute the statistic. Efron and Robert Tibshirani's 1993 book made the approach accessible to applied researchers, and it is now standard in statistical software. Leo Breiman's 1996 bagging method used bootstrap samples to train many models and average them, which connected the idea to machine learning.

Check yourself

QuestionWhat does drawing with replacement change compared with shuffling the original list?Show answer

Shuffling gives a permutation of the same values, so every resample has the same mean and there is no spread to measure. Sampling with replacement lets values repeat and go missing, so the resampled statistics vary, and that variation is the quantity the bootstrap uses to estimate uncertainty.

QuestionA dataset has n=4n = 4 values. What is the probability that one specific value is left out of a single bootstrap resample?Show answer

Each draw misses that value with probability 3/43/4, and the four draws are independent, so the probability is (3/4)4=81/256≈0.316(3/4)^4 = 81/256 \approx 0.316.

QuestionWhy is the bootstrap useful for the median, when a textbook formula for the median's standard error is awkward?Show answer

The procedure only needs the statistic to be computable on any dataset. You compute the median on each resample, and the spread of those medians estimates the standard error. No derivation of the median's sampling distribution is needed, and the same code works for any statistic.

QuestionYou run the bootstrap on 5 data points and report a 95% interval. What is the main concern?Show answer

Five points are too few for the empirical distribution to resemble the population well. The resamples can only contain those five values, so the interval will likely be too narrow. The estimate is only as good as the sample it resamples from, so the honest move is to report the interval with that caveat or to collect more data.

QuestionA web analytics table contains many page views per user. You bootstrap individual rows to get a confidence interval for average session length. Why might the interval be too narrow?Show answer

Page views from the same user are correlated. Resampling individual rows treats them as independent, which overstates the effective sample size. The fix is to resample users (the clusters) and carry all of each user's rows along with them.

Sources