2-sample bootstrap: confidence interval for a difference
The 2-sample bootstrap estimates the uncertainty in a difference between two independent groups, such as the difference in median repair time between two crews or in mean strength between two suppliers, by resampling each group separately. It repeats the comparison on thousands of resampled data sets and uses the middle 95% of the resulting differences as a 95% percentile interval. It does not need normal data or a formula for the standard error, so it works for medians, percentiles and ratios as well as means.
When to use it
- You want to know how much two groups differ, not just whether they do, and the statistic is a median, percentile or ratio.
- The data are skewed or contain a few long values, such as changeover times, lead times or defect counts, and the 2-sample t interval is in doubt.
- The groups have different spreads or sizes; the bootstrap resamples each group on its own and handles both.
For a test of whether the groups differ at all, a randomization test or the Mann-Whitney test is the usual choice. For one group, see the 1-sample bootstrap.
How it works
With group A (n_A values) and group B (n_B values) and a statistic such as the median:
- Draw n_A values from group A with replacement, and separately n_B values from group B with replacement.
- Compute the difference of the statistic, θ̂*_B − θ̂*_A.
- Repeat B times (10,000 is a common choice) and sort the differences.
95% CI for θ_B − θ_A = [ 2.5th percentile, 97.5th percentile ] of the B resampled differencesEach group is resampled only from itself, which keeps the group sizes and lets each group keep its own shape and spread. If the interval excludes zero, the difference is significant at about the 5% level.
As with any random method, record the seed of the random number generator and the number of resamples, so others can reproduce the result, and rerun with another seed to see how much the interval ends move.
Example
A stamping plant introduces a quick-changeover method for die changes on one press. To size the gain, the team compares the last 15 die changes with the old method and the first 12 with the new one (minutes):
| Method | Changeover time (min) |
|---|---|
| Old | 32, 26, 39, 49, 63, 44, 35, 32, 55, 74, 46, 27, 30, 74, 45 |
| New | 17, 31, 21, 26, 27, 25, 39, 31, 26, 37, 43, 18 |
The old method has median 44.0 and mean 44.7 minutes (standard deviation 15.8); the new one has median 26.5 and mean 28.4 minutes (standard deviation 8.2). The old times are skewed by a few long changes. The team bootstraps the difference new − old with B = 10,000, using NumPy's default generator with seed 2026 (numpy.random.default_rng(2026)).
| Difference, new − old | Estimate | Bootstrap SE | 95% percentile interval |
|---|---|---|---|
| Median | −17.5 min | 6.2 | −26.0 to −3.0 min |
| Mean | −16.3 min | 4.5 | −25.4 to −7.7 min |
Only 0.7% of the resampled median differences are zero or positive. Seeds 7 and 99 give median intervals of −25.5 to −1.0 and −25.0 to −2.0 minutes; the mean intervals change by less than 0.2 minutes.
How to read the results
- The new method is faster. The typical changeover is about 17.5 minutes shorter, and the 95% interval, roughly 3 to 26 minutes shorter, excludes zero.
- The gain is uncertain in size. With 12 and 15 changeovers, the data support anything from a small gain to a large one. Planning capacity on the full 17.5 minutes is optimistic; the lower end of the interval is the conservative figure.
- The median interval moves more between seeds. Medians of small samples take only a few values, so the bootstrap distribution is lumpy and the 2.5th percentile can fall on either side of a gap. The upper end ranges from −3.0 to −1.0 minutes across seeds; the conclusion does not change.
- The mean interval is close to the Welch interval. The Welch 95% interval for the mean difference is −26.1 to −6.5 minutes, similar to the bootstrap's −25.4 to −7.7; with these sample sizes the t method holds up well for means.
- Check that the groups are comparable. The new-method changeovers are the first 12 after training. If the dies changed were easier, or the team was being watched, part of the gain may not last; keep tracking with a run chart.
Common mistakes
- Pooling the two groups before resampling. That builds the distribution under "no difference", which is a randomization test, not a confidence interval for the difference.
- Not reporting the seed and the number of resamples. The interval cannot be reproduced without them.
- Ignoring pairing. If the same units are measured under both conditions, resample the pairs (or the differences), not the two groups separately.
- Expecting precision from tiny groups. With five values per group, the bootstrap interval is unstable and too narrow.
- Reading a lumpy median distribution as a problem. It is normal for small samples; look at how much the interval moves with the seed instead.
Frequently asked questions
What is the difference between the 2-sample bootstrap and a randomization test?
The bootstrap resamples each group from itself to estimate how big the difference is, with a confidence interval. The randomization test shuffles the group labels to ask whether a difference this large could arise by chance alone, with a p-value.
Can I bootstrap a ratio, such as new time / old time?
Yes. Compute the ratio of the medians or means for each resample and take the percentiles of the ratios. The interval does not need to be symmetric.
How many resamples should I use?
10,000 is a good default for 95% intervals. Check stability by rerunning with another seed; if the ends move more than you can accept, use more resamples.
Does the bootstrap work with unequal group sizes and spreads?
Yes. Each group is resampled from itself at its own size, so unequal sizes and spreads are handled without extra assumptions.