Data Science Track · Lesson 7

Bootstrap Confidence Intervals

A single average is a guess. A confidence interval is an honest estimate — here's how resampling turns your data into a range of plausible truths.

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.

The fundamental problem We observe a sample. We want to know about the population. Any statistic we compute (mean, median, proportion) is an estimate with uncertainty attached. The confidence interval makes that uncertainty visible instead of hiding it.

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.

Sampling with replacement In a dataset of 5 values [10, 20, 30, 40, 50], a bootstrap sample might be [10, 10, 30, 50, 20] — the value 10 was picked twice, and 40 wasn't picked at all. Each bootstrap sample is the same size as the original but randomly shuffled with repetition. This mimics the natural variability that would occur if you collected a completely new sample.

The Bootstrap Algorithm — Step by Step

Bootstrap CI for FoodHub mean delivery time: Original mean: 24.16 minutes (from 1,898 orders) After 10,000 bootstrap iterations: Bootstrap mean distribution: approx. Normal(24.16, 0.16) 2.5th percentile → 23.85 min 97.5th percentile → 24.47 min 95% Confidence Interval: [23.85, 24.47] minutes Interpretation: If we collected many different samples of 1,898 orders, 95% of those samples' confidence intervals would contain the true population mean delivery time.

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.

The correct interpretation If you repeated your study many times — each time collecting a fresh sample and computing a 95% CI — then about 95% of those intervals would contain the true population mean. Your single interval either does or doesn't contain it, but you don't know which. You're 95% confident in the procedure, not in the specific interval.

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:

CI = x̄ ± z* · (σ / √n)

This works well for large samples of means (Central Limit Theorem guarantees approximate normality). But it fails for:

Bootstrap works for all of these because it makes no distribution assumptions — it learns the sampling distribution empirically from your data.

Bootstrap for the median The median delivery time in FoodHub is 25 minutes. Computing a confidence interval for a median using classical methods requires complex formulas. With bootstrap: resample 10,000 times, compute the median each time, take the 2.5th and 97.5th percentile. No assumptions needed.

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.

In the FoodHub Project

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 →