Why a Single Number Isn't Enough
The mean delivery time in the FoodHub dataset is 24.2 minutes. But this is calculated from a sample of 1,898 orders — not every order ever placed. If you collected a different sample of 1,898 orders, you'd get a slightly different mean. And another sample would give yet another value.
Reporting just "24.2 minutes" implies a precision that doesn't exist. A more honest statement is: "We're 95% confident the true mean delivery time is somewhere between 23.6 and 24.8 minutes." That range is a confidence interval.
What is Bootstrapping?
Bootstrapping is a resampling technique invented by statistician Bradley Efron in 1979. The name comes from the phrase "pulling yourself up by your bootstraps" — you use your existing sample to simulate what drawing thousands of new samples would look like.
The core idea: treat your sample as if it were the population. Then simulate drawing new samples from it by sampling with replacement — meaning each draw picks a random value from your dataset, puts it back, and can pick it again.
The Bootstrap Algorithm — Step by Step
- 1Start with your original sample.
You have n observations (e.g., 1,898 delivery times). Compute your statistic of interest — say, the mean: 24.2 minutes. - 2Draw a bootstrap sample.
Randomly select n values from your dataset, with replacement. Some values will appear twice; some won't appear at all. This simulates collecting a new random sample. - 3Compute the statistic on the bootstrap sample.
Calculate the mean (or whatever you're measuring) on this new sample. You get a slightly different value — maybe 24.0 or 24.5. - 4Repeat 1,000 to 10,000 times.
Each repetition gives you one bootstrap estimate. After 10,000 iterations, you have 10,000 slightly different means. - 5Build the confidence interval from the distribution.
Sort your 10,000 bootstrap means. The 2.5th percentile and the 97.5th percentile define the 95% confidence interval. These are the values that bracket the middle 95% of your bootstrap distribution.
What Does "95% Confident" Actually Mean?
This is the most commonly misunderstood part of confidence intervals. It does NOT mean: "There is a 95% probability that the true mean lies between 23.85 and 24.47 minutes." The true mean is a fixed (unknown) number — it's not a random variable with probabilities.
In practice, analysts say "95% confident the mean is between X and Y" as a shorthand. The important takeaway is that the interval reflects sampling uncertainty — a wider interval means more uncertainty (smaller sample or more variable data), a narrower interval means more precision.
Why Bootstrap Instead of the Traditional Formula?
The classical approach to confidence intervals assumes your data follows a specific distribution (usually Normal) and uses formulas like:
This works well for large samples of means (Central Limit Theorem guarantees approximate normality). But it fails for:
- Medians, percentiles, or custom statistics — no formula exists
- Small samples — CLT doesn't kick in, normality can't be assumed
- Complex derived statistics — ratios, differences of percentiles, etc.
Bootstrap works for all of these because it makes no distribution assumptions — it learns the sampling distribution empirically from your data.
Practical Notes
How many iterations?
1,000 gives a rough estimate. 10,000 gives stable results for most purposes. Beyond 10,000, improvements are marginal. The FoodHub analysis used 10,000 iterations — a standard choice.
Setting a random seed
Bootstrap involves randomness. To make results reproducible (so you get the same CI every time you run the code), set a random seed before bootstrapping: np.random.seed(42). Without this, the interval will vary slightly between runs — still valid, but not reproducible.
When bootstrap fails
Bootstrap assumes your sample is representative of the population. If your sample is biased (e.g., only weekend orders), the CI will be narrow and precise — but centred on a biased estimate. Bootstrap quantifies sampling variability, not systematic bias.
Bootstrap CIs were computed for mean delivery time and mean preparation time, separately for weekday and weekend orders. The confidence intervals for the two groups didn't overlap — a strong visual signal that the difference was real, not just sampling noise. This foreshadowed the hypothesis test that formally confirmed weekends have significantly longer delivery times. See Chapter 8 — Bootstrap Analysis →