Q&A
What is the Bootstrap method in data science?
Overview
Classical statistics derives confidence intervals analytically, which often requires assumptions (normality, large samples). The bootstrap sidesteps these by simulating the sampling process: treat your observed sample as if it were the population, draw thousands of "bootstrap samples" of the same size with replacement, compute your statistic on each, and use the distribution of results as an empirical approximation of the true sampling distribution.
graph TD
A[Original sample n=100] --> B["Draw 1000 bootstrap samples<br/>(size n, with replacement)"]
B --> C["Compute statistic on each<br/>(mean, median, R², etc.)"]
C --> D["Distribution of 1000 estimates"]
D --> E["95% CI: 2.5th–97.5th percentile"]
style D fill:#1e1b4b,color:#fff
How It Works
- Start with a sample of size
n. - Draw
Bbootstrap samples (typically 1000–10000), each of sizen, with replacement - meaning each draw can include duplicates and some original observations are omitted. - Compute the statistic of interest (mean, median, correlation, model accuracy, custom metric) on each bootstrap sample.
- The collection of
Bestimates is the bootstrap distribution. - Derive a confidence interval - most commonly the percentile method: the 2.5th and 97.5th percentiles of the bootstrap distribution form the 95% CI.
Python Implementation
import numpy as np
rng = np.random.default_rng(42)
data = rng.normal(loc=5, scale=2, size=100) # our observed sample
B = 5000
bootstrap_means = np.array([
rng.choice(data, size=len(data), replace=True).mean()
for _ in range(B)
])
ci_lower = np.percentile(bootstrap_means, 2.5)
ci_upper = np.percentile(bootstrap_means, 97.5)
print(f"Bootstrap 95% CI for mean: ({ci_lower:.3f}, {ci_upper:.3f})")
print(f"Bootstrap SE: {bootstrap_means.std():.3f}")
The replace=True argument to np.random.choice is what makes it bootstrap - sampling with replacement.
Non-Parametric Confidence Interval for Any Statistic
The same code works for any statistic - just change .mean():
bootstrap_medians = np.array([
np.median(rng.choice(data, size=len(data), replace=True))
for _ in range(B)
])
# Median's CI, even though no closed-form formula exists
This is the bootstrap's superpower: it applies to statistics that don't have a standard analytical CI (trimmed means, custom model metrics, AUC, etc.).
Scikit-learn Integration: Bootstrap for Model Evaluation
from sklearn.utils import resample
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
scores = []
for _ in range(200):
X_boot, y_boot = resample(X_train, y_train) # bootstrap sample
model = RandomForestClassifier()
model.fit(X_boot, y_boot)
scores.append(accuracy_score(y_test, model.predict(X_test)))
print(f"Mean accuracy: {np.mean(scores):.3f}")
print(f"95% CI: ({np.percentile(scores,2.5):.3f}, {np.percentile(scores,97.5):.3f})")
Key Properties
- With replacement is essential - this is what distinguishes bootstrap from permutation testing (which samples without replacement).
- Works on small samples where CLT assumptions may not hold.
- Computationally simple but can be slow for very complex models (each iteration retrains the model).
- Gives a standard error (SD of bootstrap distribution) and confidence intervals without formulae.
Bootstrap vs Other Resampling
| Method | Sampling | Used for |
|---|---|---|
| Bootstrap | With replacement | CI, SE for any statistic |
| Cross-validation | Without replacement | Model generalization estimate |
| Permutation test | Without replacement (shuffled) | Hypothesis testing |
Why This Matters in Interviews
Data science and statistics interviews use bootstrap to test resampling fluency. Core points: draw samples with replacement; compute statistic on each; the distribution of results estimates the sampling distribution; use percentiles for CIs; it works for any statistic (no closed-form formula needed). Distinguishing bootstrap (with replacement, for uncertainty quantification) from cross-validation (without replacement, for model evaluation) is a frequent discriminator.
Common Pitfalls
- Sampling without replacement (that's cross-validation, not bootstrap).
- Using too few bootstrap samples (B < 1000 gives unstable CIs; use 1000–10000).
- Bootstrapping the whole pipeline without re-training the model on each sample (leaks information).
- Treating the bootstrap CI as exact - it's an approximation; very small samples give crude estimates.
- Confusing the bootstrap distribution (of estimates) with the data distribution.
References
- Efron & Tibshirani, An Introduction to the Bootstrap (1993).
- scipy bootstrap: https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.bootstrap.html