Contents/ Part XI · Estimation & Confidence Intervals/ Chapter 75

Resampling & Simulation

When no formula exists, let the data resample itself. The bootstrap builds a confidence interval for almost any statistic by drawing samples-of-the-sample. We validate it against the t-interval, then put a real interval on a MEDIAN salary, where no neat formula exists.

⏱️ ~16 min read
🐍 Notebook included
📊 Chapter 75

Every interval so far came from a formula, and every formula came with conditions. But many useful statistics, the median, a percentile, a correlation, a ratio, have no tidy standard-error formula at all. The bootstrap sidesteps the algebra entirely by simulating.

🔁
The bootstrap resamples your data with replacement, many times, recomputing the statistic on each resample. The spread of those values is the standard error, and their 2.5th/97.5th percentiles form a 95% confidence interval, for any statistic you can compute.
🔁
One loop, any statistic

The same three lines, resample, recompute, read off the percentiles, give a confidence interval for the mean, the median, a correlation, or anything else. No new theory required.

1

The Bootstrap Idea

We have only one sample, but a good sample resembles its population. So instead of drawing many samples from the population (which we cannot do), we resample from our sample with replacement, each resample the same size, and recompute the statistic each time.

Resample with replacement, recompute, repeat your sample n values resample 1 → stat resample 2 → stat ... × 10,000 bootstrap distribution spread = SE, percentiles = CI

In the notebook, the bootstrap distribution of the mean has a spread of 1.27, matching the formula standard error s/√n of 1.28, recovered with no algebra. The bootstrap turns "imagine resampling the population" into "actually resample the sample", which is something a computer can do trivially.

2

The Percentile Interval

The simplest bootstrap confidence interval is the percentile method: take the 2.5th and 97.5th percentiles of the bootstrap statistics. For the mean, this should reproduce the textbook t-interval, a reassuring sanity check.

The percentile method is the easy default, but it can be slightly off when the statistic's distribution is skewed. The library-first, more accurate choice is BCa (bias-corrected and accelerated), one call away in scipy.stats.bootstrap(data, statistic, method="BCa"). The companion notebook reports the BCa interval next to the percentile one for the median salary, on this 300-row sample they match almost exactly, but BCa is the safer default on small or heavily skewed samples.

95% CI = middle 95% of the bootstrap distribution 2.5th 97.5th central 95%

In the notebook the bootstrap percentile interval for the mean is [47.18, 52.17] against the t-interval's [47.10, 52.24], essentially identical. A method that reproduces the formula where a formula exists is exactly the kind you can trust where one does not.

3

Where the Bootstrap Wins

The payoff is statistics with no standard-error formula. The median is the classic case: there is no simple algebraic SE, but the bootstrap does not care, resample, take the median, read the percentiles.

StatisticFormula CI?Bootstrap?
Meanyes (t-interval)yes (and agrees)
Medianno simple oneyes, easily
Percentile (e.g. 90th)noyes
Correlationawkward / approximateyes
Ratio, trimmed mean, …rarelyyes, same loop

The same resample-and-recompute loop delivers a CI for any of these. This generality, introduced by Bradley Efron in 1979, is why the bootstrap is one of the most used tools in modern statistics and data science. It also has cousins: the permutation test (shuffle group labels to test a difference) and the jackknife (leave-one-out resampling).

4

Real-World Example: Median Salary

An HR team exports 300 employee salaries. Pay is right-skewed, a few high earners pull the mean up, so the median is the fairer "typical pay" summary. But the median has no neat CI formula, exactly the case the bootstrap was built for.

📂 Dataset · resampling-and-simulation--salaries.xlsx

One row per employee with annual_salary, department, and years_experience.

SummaryPoint estimate95% confidence intervalMethod
Median salary$107,650$101,050 to $111,100bootstrap (no formula)
Mean salary$111,251$107,335 to $115,167t-interval

The median is about $107,650, with a bootstrap 95% interval of roughly $101,000 to $111,000, a defensible "typical pay" range that no textbook formula could provide. Because the data is right-skewed (skewness 0.85), the mean ($111,251) sits above the median; reporting the median with a bootstrap interval is the honest, robust summary. The bootstrap turned an awkward statistic into a routine one.

5

Resampling in Machine Learning & AI

Resampling is everywhere in machine learning, from honest error bars on metrics to the ensembles that power random forests.

Idea (this chapter)In ML / AI it becomesExample
Bootstrap CIError bars on any metric95% CI for accuracy / F1 / AUC
Resampling the dataBagging & random forestseach tree trains on a bootstrap sample
Permutation testFeature importance & significanceshuffle a feature to test its effect
SimulationMonte Carlo everythinguncertainty when no formula exists
🤖
Why this matters for AI research

The bootstrap is the standard way to put a confidence interval on a model metric: resample the test set, recompute accuracy or AUC, and read the percentiles, no distributional assumptions needed. It is also the engine of bagging: every tree in a random forest is trained on a bootstrap resample of the data, and permutation importance resamples by shuffling a feature to measure its contribution. When a quantity has no clean formula, which in modern ML is most of the time, resampling and simulation are how you quantify uncertainty honestly.

🐍

Bootstrap any statistic in Python

The companion notebook writes a three-line bootstrap, recovers the standard error and percentile interval for the mean (matching the t-interval), bootstraps the median and a percentile with no formula, and loads resampling-and-simulation--salaries.xlsx to put a 95% interval on the median salary.

📓 View Notebook (code & outputs) ▶ Open in Colab ⬇ View / Download on GitHub

View opens the rendered notebook instantly (no setup). Open in Colab runs & edits it live in your browser. To run locally, install numpy, pandas, scipy, matplotlib, and openpyxl and launch jupyter notebook.

🎓 Key Takeaways

  • The bootstrap resamples the data with replacement and recomputes the statistic; the spread is the SE.
  • Percentile CI: the 2.5th and 97.5th percentiles of the bootstrap statistics form a 95% interval.
  • It reproduces the t-interval for the mean and works for statistics with no formula (median, percentile, correlation).
  • Real data: median salary $107,650 with a bootstrap 95% interval of about $101k–$111k, where no formula exists.
  • In ML/AI: bootstrap CIs on metrics, bagging/random forests, and permutation importance are all resampling.
6

Practice Challenges

Five short challenges, beginner to intermediate. Try them with NumPy before checking the solutions.

1

Bootstrap the standard error

From a sample of 80 from Normal(100, 15), bootstrap the SE of the mean and compare to s/√n.

Hint: the bootstrap SE is the std of the bootstrap means.
2

Percentile CI for the mean

Build the bootstrap 95% percentile CI for the mean and compare to the t-interval.

Hint: np.percentile(boot, [2.5, 97.5]).
3

CI for the median

On right-skewed lognormal data (n = 150), build a bootstrap 95% CI for the median.

Hint: same loop, swap np.mean for np.median.
4

CI for a correlation

Bootstrap a 95% CI for the correlation between two related variables.

Hint: resample paired rows, recompute the correlation each time.
5

Real data: median salary

Load resampling-and-simulation--salaries.xlsx and build a bootstrap 95% CI for the median salary.

Hint: 20,000 resamples, then read the percentiles.
Check your work

A fully-worked solutions notebook walks through all five challenges, each verified in code. Try them yourself first, then compare.

📓 View Solutions ▶ Open Solutions in Colab ⬇ View / Download on GitHub
7

Quiz: Test Yourself

Eight quick questions on resampling and the bootstrap. Answer them, hit Check Answers, and keep refining until you score 100%. Your progress is saved, so you can hop back to the chapter and return anytime.

🏁
That completes Estimation & Confidence Intervals

You can now turn a sample into a best guess and an honest interval, for means, proportions, differences, and even formula-free statistics via the bootstrap, and report the margin of error correctly. Hypothesis Testing & Inference takes the mirror image: instead of estimating a value, we test a claim, starting with the logic of hypothesis testing.