Statistical simulation

Bootstrap method simulator

Generate a sample from the chosen distribution, apply bootstrap resampling with replacement, and build confidence intervals for any statistic without parametric assumptions.

Simulation parameters

Original sample

Histogram of the sample with theoretical density curve

Click "New sample" to start

Bootstrap distribution of the statistic

Histogram of the B replicates with the CI shaded

Click "New sample" to start

What is the bootstrap?

The bootstrap is a resampling technique proposed by Bradley Efron in 1979. Its name comes from the English expression «pull yourself up by your own bootstraps», which refers to the idea of learning about the variability of an estimator using only the data from the sample itself, without needing to know the population distribution.

The core intuition is simple: if we don't know the population distribution \(F\), we can use the empirical distribution \(\hat{F}_n\) — which assigns probability \(1/n\) to each observation in the sample — as a substitute. If we draw many bootstrap samples from \(\hat{F}_n\) (that is, with replacement from the original sample) and compute the statistic of interest on each one, the variability of those replicates approximates the variability of the actual estimator.

Formally, let \(\hat{\theta} = T(X_1,\ldots,X_n)\) be an estimator of \(\theta\). Each bootstrap replicate \(\hat{\theta}^* = T(X_1^*,\ldots,X_n^*)\) is computed on a sample \((X_1^*,\ldots,X_n^*)\) drawn with replacement from the original sample. The distribution of \(\hat{\theta}^* - \hat{\theta}\) approximates the distribution of \(\hat{\theta} - \theta\).

\[\widehat{SE}_{\text{boot}} = \sqrt{\frac{1}{B-1}\sum_{b=1}^{B}\left(\hat{\theta}^{*(b)} - \bar{\hat{\theta}}^*\right)^2}\]

where \(\bar{\hat{\theta}}^* = \frac{1}{B}\sum_{b=1}^{B}\hat{\theta}^{*(b)}\) is the mean of the bootstrap replicates.

What the simulator does, step by step

The simulator has an advantage that does not exist with real data: it knows the population the sample comes from. That makes it possible to compare what the bootstrap says, seeing only the sample, with the truth.

  1. "New sample" draws n observations from the chosen population distribution. This is the only time the population is used. The original sample chart shows that sample (bars) next to the theoretical density (red line).
  2. It computes the statistic on the sample (mean, median or standard deviation): this is the estimate \(\hat{\theta}\), shown in the "Bootstrap / Sample" column.
  3. It resamples. It builds a bootstrap sample by choosing one of the n observations n times, at random and with replacement. Some appear more than once and others do not appear.
  4. It computes the statistic on the bootstrap sample and repeats steps 3 and 4 until it has B replicates. Their histogram is the bootstrap distribution chart.
  5. Bootstrap standard error: the standard deviation of the B replicates.
  6. Percentile interval: it sorts the B replicates from smallest to largest and takes the values that leave α/2 outside on each side. With B = 1000 and a 95% level, these are the 26th and the 975th sorted replicates. On the chart, the green bars lie inside the interval and the grey ones outside.

The two buttons separate two sources of variation. "Regenerate bootstrap" repeats steps 3 to 6 with the same sample: the results change very little, and that small difference is the Monte Carlo error, which shrinks as B grows. "New sample" changes the sample: now the interval really moves, and no value of B reduces that variation, only a larger n. It is the uncertainty the bootstrap is trying to measure.

The "Theoretical" column holds what can only be known by knowing the population: the true value of the parameter and, when a simple formula exists, the theoretical standard error of the statistic (detailed below). If there is none, "—" is shown: these are precisely the cases where the bootstrap is most useful. The "Normal CI" row only appears for the mean and is the classical interval \(\bar{x} \pm z \cdot s/\sqrt{n}\).

What to check: whether the bootstrap standard error is close to the theoretical one and whether the interval contains the true parameter. Clicking "New sample" several times, the 95% interval should contain it about 95 times out of 100 (somewhat fewer with small samples, as explained below).

How the resampling works

The non-parametric bootstrap algorithm follows these steps:

  1. Take the original sample \(\mathbf{x} = (x_1, \ldots, x_n)\) of size \(n\).
  2. Generate \(B\) bootstrap samples \(\mathbf{x}^{*(1)}, \ldots, \mathbf{x}^{*(B)}\), each of size \(n\), by drawing with replacement from \(\mathbf{x}\). In each replicate some original values will appear several times and others won't appear at all (on average, about \(1 - e^{-1} \approx 63.2\,\%\) of the original values are represented in each replicate).
  3. Compute the statistic of interest \(\hat{\theta}^{*(b)} = T(\mathbf{x}^{*(b)})\) for each replicate \(b = 1, \ldots, B\).
  4. Estimate the standard error as the standard deviation of the \(B\) replicates (formula above).
  5. Build the percentile CI at \((1-\alpha) \times 100\,\%\) as the interval between the \(\alpha/2\) and \(1-\alpha/2\) percentiles of the bootstrap distribution:
\[\text{IC}_{\text{percentil}} = \left[\hat{\theta}^*_{(\alpha/2)},\; \hat{\theta}^*_{(1-\alpha/2)}\right]\]

The percentile method is the simplest one, and the one implemented in this simulator. For small samples or biased estimators, more sophisticated methods such as BCa (bias-corrected and accelerated) offer better coverage. In practice, \(B = 1000\) replicates is usually enough to estimate the standard error and build 95% percentile intervals, although their endpoints still vary somewhat from one run to the next (Monte Carlo error); \(B = 5000\) or more provides greater stability in the extreme percentiles. No number is universally sufficient: it depends on the confidence level and on how many decimal places of the interval you want to interpret.

Why it works and where the figures come from

The plug-in principle

What you want to know is how much the statistic varies from one sample to another. To see it directly you would have to draw many samples from the population, and there is only one. The bootstrap replaces the unknown population with the best picture available of it, the sample itself, and repeats the experiment there:

Real worldBootstrap world
Where you draw fromThe population F, unknownThe sample (empirical distribution \(\hat{F}_n\)), known
What you getA sample and its statistic \(\hat{\theta}\)A resample and its statistic \(\hat{\theta}^*\)
How many times you can repeat itOnceAs many as you like
What you want to measureHow \(\hat{\theta}\) varies around \(\theta\)How \(\hat{\theta}^*\) varies around \(\hat{\theta}\)

The bet is that the second variation resembles the first. It works when the sample represents the population well in whatever affects the statistic, and fails when it does not (very small samples, or statistics that depend on the extreme values).

For the mean, the bootstrap reproduces the classical formula

In this case you can work out what will come out without simulating anything. Each value in a resample, \(X^*\), is one of the n observations with probability \(1/n\). Its mean and variance are therefore those of the sample:

\[ \begin{gathered} E^*(X^*) = \bar{x}, \\ \operatorname{Var}^*(X^*) = \frac{1}{n}\sum_{i=1}^{n}(x_i - \bar{x})^2 = \hat{\sigma}^2. \end{gathered} \]

The mean of a resample is the average of n independent draws, so its variance is the one above divided by n:

\[ \begin{gathered} \operatorname{Var}^*(\bar{x}^*) = \frac{\hat{\sigma}^2}{n}, \\ SE_{\text{boot}} \approx \frac{\hat{\sigma}}{\sqrt{n}} = \sqrt{\frac{n-1}{n}}\;\frac{s}{\sqrt{n}}. \end{gathered} \]

This is the usual standard error, \(s/\sqrt{n}\), times a factor slightly below 1 (0.95 with n = 10; 0.98 with n = 30). With B replicates, the simulator gets close to that value with a small random error. The bootstrap is not magic: it replaces a formula with computation. Its value lies in the statistics for which there is no formula.

Why a third of the data is missing from each resample

The probability that a particular observation is not picked in one draw is \(1 - 1/n\). Since the n draws are independent, the probability that it is not picked in any of them is

\[ \left(1 - \frac{1}{n}\right)^{n} \;\longrightarrow\; e^{-1} \approx 0.368. \]

Each resample contains, on average, 63.2% of the distinct observations (65.1% with n = 10; 63.8% with n = 30), and the rest are repeats. The number of distinct possible resamples is \(\binom{2n-1}{n}\): 92,378 with n = 10 and about 69 billion with n = 20. The B replicates are a random sample of all of them, hence the Monte Carlo error.

The theoretical standard error of the median

For the median there is an approximate formula, valid for large n, which depends on the height of the density at the median, \(f(m)\):

\[ SE(\text{median}) \approx \frac{1}{2\,f(m)\,\sqrt{n}}. \]

This is what the simulator uses in the "Theoretical" column. For the normal, \(f(m) = 1/(\sigma\sqrt{2\pi})\) and you get \(\sqrt{\pi/2}\;\sigma/\sqrt{n} \approx 1.25\,\sigma/\sqrt{n}\): with normal data, the median is 25% less precise than the mean. For the uniform between 0 and 10 it gives \(5/\sqrt{n}\), and for the exponential with λ = 0.5, \(2/\sqrt{n}\). For the standard deviation with normal data, \(\sigma/\sqrt{2(n-1)}\) is used.

The median formula has a practical drawback: it requires knowing the population density, which is exactly what is not known. The bootstrap reaches a similar value using the sample alone.

How much the percentile interval really covers

The percentile interval takes the central stretch of the bootstrap distribution. If that distribution is skewed, so is the interval, which is an advantage over \(\bar{x} \pm z \cdot s/\sqrt{n}\). But with small samples it falls short, because the sample underestimates the spread of the population and there is nothing equivalent to the Student's t correction. Actual coverage of the 95% percentile interval for the mean (obtained by simulation):

Sample sizeNormal populationExponential population
n = 1089%86%
n = 3093%92%
n = 10095%94%

That is why, with small samples, variants such as BCa or the bootstrap-t are preferred. From n of about 30, the percentile method is a reasonable approximation.

When to use the bootstrap

The bootstrap is especially useful when classical parametric methods aren't directly applicable or their assumptions don't hold:

  • Small samples with unknown distribution: classical methods require normality or large sample sizes for the CLT to guarantee the approximation. The bootstrap works reasonably well from \(n \approx 15\text{–}20\) onward, although the inherent uncertainty of a small sample still persists.
  • Complex statistics: the median, Pearson correlation, the coefficient of variation, regression coefficients in non-linear models, or the Kolmogorov-Smirnov statistic don't have simple analytical expressions for their standard error. The bootstrap sidesteps that problem.
  • Non-normal data: if the data distribution has heavy tails or marked skewness, the classical confidence interval \(\bar{x} \pm z \cdot s/\sqrt{n}\) may have coverage below the nominal level. The percentile bootstrap captures the skewness of the estimator's distribution.
  • Comparison with the parametric CI: for the mean with normal data and large \(n\), the percentile bootstrap CI and the classical \(t\) CI are practically identical. Differences appear with skewed distributions or for statistics other than the mean.

Limitations: the bootstrap doesn't work well with statistics that depend on extreme values (maximum, minimum) in distributions with bounded support, nor with data that have strong temporal dependence (that's what the block bootstrap is for). It also doesn't «create» information: if the original sample is small and unrepresentative, the bootstrap can only redistribute the existing uncertainty.

Frequently asked questions

Does the bootstrap always work?

Not always. The non-parametric bootstrap fails when the statistic is sensitive to the extreme values of the empirical distribution. A classic example is the maximum of a uniform sample: the bootstrap distribution of the maximum systematically underestimates the parameter. It also fails with very small sample sizes (\(n \leq 5\)), where the empirical distribution is too discrete to approximate the real distribution well. In those cases it's preferable to use Bayesian methods or exact intervals.

Does the bootstrap work with discrete distributions (binomial, Poisson)?

Yes. The bootstrap is a non-parametric method that assumes no particular form for the data-generating distribution, so it works equally well with continuous and discrete data. By resampling with replacement from the original sample —made up, for instance, of integer counts from a binomial or a Poisson— and recomputing the statistic on each replicate, you obtain its approximate sampling distribution without needing analytical formulas. The only practical difference is visual: for discrete data the population is described by a probability mass function (PMF) at the integer values, rather than a continuous density curve. Note that the bootstrap distribution of highly discrete statistics (such as the median of a small count sample) can concentrate on just a few values, which makes the percentile CI "jump".

What is the BCa method?

The BCa method (Bias-Corrected and Accelerated), also proposed by Efron, corrects two shortcomings of the simple percentile method: the bias of the estimator (the difference between \(\bar{\hat{\theta}}^*\) and \(\hat{\theta}\)) and the acceleration (how the standard error changes with the parameter value). The CI percentiles are shifted according to these corrections, which improves the actual coverage for biased estimators or when the scale of the estimator's distribution isn't constant. The acceleration is usually computed via the jackknife method. For most practical applications with \(n \geq 30\) BCa and the percentile method give similar results.

How many replicates B do I need?

To estimate the bootstrap standard error, \(B = 200\text{–}500\) replicates is usually enough. To build 95% percentile confidence intervals, \(B \geq 1000\) is recommended. For extreme percentiles (99% or 99.9%) or for the BCa method, \(B = 5000\text{–}10\,000\) is advisable. Beyond \(B = 10\,000\), the precision gain is marginal and the computational cost can be high with large datasets. The variability of the bootstrap CI decreases roughly as \(1/\sqrt{B}\), similar to Monte Carlo error.

What is the difference between the bootstrap and the jackknife?

The jackknife (Quenouille, 1949; popularized by Tukey) is another resampling technique that estimates the bias and standard error of an estimator by systematically leaving out one observation at a time: it generates exactly \(n\) «leave-one-out» replicates. It's faster for large datasets but less general than the bootstrap. It doesn't capture the full distribution of the estimator well, nor can it be used to build percentile CIs. The bootstrap, because of its randomness, needs \(B \gg n\) replicates to be stable, but it can estimate the entire shape of the estimator's distribution and handle more complex statistics. The first-order jackknife is a linearization of the bootstrap and converges to it as \(B \to \infty\).

References and further reading