All modules

Module 12 · Resampling & simulation

Let repeated samples reveal uncertainty.

Resample observations to estimate precision, or shuffle group labels to test a null model. Make the simulation match the statistical question.

30–40 min · Learn, explore, practiceBuilds on Sampling distributions · Confidence intervals with t · Comparing two groups
Monte Carlo p = (b + 1) / (B + 1)

By the end of this lesson

Two questions, two resampling rules

Suppose you have a sample of delivery times and want to estimate how precisely its mean describes the population. A bootstrap treats the observed sample as an empirical stand-in for that population. Repeatedly draw the same number of observations with replacement and calculate a mean from each resample.

Now suppose you have two independent groups and want to evaluate whether their outcomes could come from the same distribution. A permutation test pools the observed values and reassigns their group labels while preserving the original group sizes. Values are not duplicated within one shuffle.

These operations answer different questions. The bootstrap approximates sampling variation around an estimate. Permutation generates a reference distribution under an exchangeability null model. Neither procedure creates additional independent information about the population.

Resample, summarize, repeat

Choose a confidence-interval question or a group-comparison question. The illustrative data and seed make each run reproducible.

Bars count simulated statistics. Bootstrap guide lines mark percentile endpoints; permutation guide lines mark ±|observed difference|. Tail counts use the original simulated statistics, not histogram bins.

Make a change

3–200 comma or space separated values.

95
2000

Same inputs and seed reproduce the same simulation.

Observed statistic15.400
Interval or test result[14.55, 16.25]
Uncertainty estimateSE 0.439
Replicates · B2000

The displayed interval is a 95% percentile bootstrap interval for the population mean. The reported SE describes variation among resampled means. More repetitions improve numerical stability, not the amount of original data.

Read the chart summary

Observed mean 15.400 from 20 observations. 95% percentile bootstrap interval 14.550 to 16.250; estimated standard error 0.439 from 2000 resamples, seed 42.

Try this

Build a percentile interval

For each of B bootstrap samples, compute the mean. Their standard deviation estimates the standard error of the mean. To form a 95% percentile interval, take the 2.5th and 97.5th percentiles of the bootstrap means. The endpoints may change slightly when you change the random seed.

As a mechanical illustration, the original sample 4, 6, 8, 10 has mean 7. One possible resample is 4, 4, 8, 10, with mean 6.5. Another is 6, 8, 10, 10, with mean 8.5. Thousands of such means form the bootstrap distribution; two resamples alone are far too few to estimate its tails.

If the 2.5th and 97.5th percentiles from a run are 5 and 9, the percentile interval is [5, 9]. Its intended interpretation concerns approximate repeated-sampling coverage of the procedure. It is not a 95% probability distribution for a fixed unknown population mean after the data have been observed.

Count unusual shuffled differences

For two groups, choose the statistic D = mean(A) − mean(B). Under the null model used here, group labels are exchangeable: independent observations come from the same population distribution. For an appropriately randomized experiment, the assignment mechanism can justify a related randomization analysis under a sharp no-effect null.

Compute D for each random reassignment. A two-sided result is at least as extreme when its absolute difference is at least |Dobserved|. This lab uses that absolute-distance convention and a small numerical tolerance for ties. Other software can use twice the smaller one-sided tail; these conventions need not agree for asymmetric discrete null distributions.

Let b of B random shuffles be at least as extreme. Report (b + 1)/(B + 1), including the observed allocation in the adjustment. For b = 39 and B = 999, the p-value is 40/1000 = 0.040. Even if b = 0, the simulation does not justify p = 0; its smallest reported p-value would be 0.001.

Separate sampling uncertainty from simulation noise

Increasing B gives a steadier computational approximation. It does not increase the original sample size, fix selection bias, or necessarily shrink the population confidence interval. Changing the seed reruns a comparable simulation; keeping it fixed makes classroom demonstrations reproducible.

For a permutation p-value near p, a rough Monte Carlo standard error is √[p(1 − p)/(B + 1)]. If p ≈ 0.05 and B = 1,999, that error is about 0.0049. A simulated answer close to a decision threshold may need substantially more repetitions than this teaching explorer supports.

The bootstrap standard error shown in bootstrap mode describes variation among resampled means. The Monte Carlo standard error shown in permutation mode describes numerical uncertainty in a tail-probability estimate. These are different quantities and should not be substituted for each other.

What resampling cannot repair

A bootstrap cannot recover population features absent from the observed sample. Tiny samples, extreme skewness, influential observations, and a degenerate sample can make percentile intervals inaccurate. More advanced intervals such as BCa address some limitations; the transparent percentile method here is a teaching tool, not a universally best interval.

Dependence also changes the correct resampling unit. Paired data should retain their pairs; repeated observations within a cluster may require cluster resampling; time series may need blocks. This lab implements independent observations only.

For the unstudentized permutation difference used here, equal means alone are not enough to guarantee validity when group distributions differ, especially with unequal variances and sample sizes. Use a method that matches the design and the null being tested. Describe conclusions in terms of that model, rather than calling resampling assumption-free.

Make the connection.

Try each question before revealing the solution.

Why does a bootstrap sample of size n draw with replacement rather than simply rearrange the original observations?

Show the worked solution

Rearranging the same values leaves their mean unchanged. Replacement changes which observations appear and how often, creating variation in the resampled mean while retaining sample size n. Each bootstrap sample may repeat some original observations and omit others.

Among 999 random label shuffles, 9 differences are at least as extreme as the observed one. What p-value does this lesson report?

Show the worked solution

The adjusted Monte Carlo value is (9 + 1)/(999 + 1) = 0.010. It is an estimate from simulated label allocations, not the result of enumerating every possible allocation.

A student increases bootstrap repetitions from 1,000 to 5,000 but keeps the same eight original observations. Has the effective sample size become 5,000?

Show the worked solution

No. The observed sample size remains eight. More repetitions reduce simulation error in estimating the bootstrap distribution. They do not supply new population observations or remove small-sample limitations.

Assumptions, methods & references
  • Bootstrap resampling assumes independent observations representative of the target population. The percentile interval is approximate and may have poor coverage for small or strongly skewed samples.
  • Permutation mode tests exchangeability of independent group labels using an unstudentized mean difference. Equality of population means by itself is insufficient under arbitrary unequal distributions.
  • All B permutations are randomly generated; this implementation does not switch to exhaustive enumeration. The two-sided convention is absolute distance from zero and uses the (b + 1)/(B + 1) adjustment.
  • The browser explorer permits 200–5,000 replicates and 3–200 observations per group. A fixed pseudorandom seed is for reproducibility, not additional statistical information.

Further reading

Keep building your intuition.Explore the complete course