Bootstrap Confidence Interval in Python
Estimates a confidence interval for a statistic (like the mean) using bootstrap resampling in pure Python.
Python code
23 linesimport random
def bootstrap_ci(data, statistic, n_bootstraps=1000, ci_level=0.95, seed=42):
random.seed(seed)
n = len(data)
boot_stats = []
for _ in range(n_bootstraps):
sample = [random.choice(data) for _ in range(n)]
boot_stats.append(statistic(sample))
boot_stats.sort()
lower_idx = int((1 - ci_level) / 2 * n_bootstraps)
upper_idx = int((1 + ci_level) / 2 * n_bootstraps) - 1
return boot_stats[lower_idx], boot_stats[upper_idx]
if __name__ == "__main__":
data = [12, 15, 14, 10, 13, 11, 16, 14, 12, 13]
ci_low, ci_high = bootstrap_ci(data, statistic=lambda x: sum(x) / len(x))
print(f"95% Bootstrap CI for mean: [{ci_low:.2f}, {ci_high:.2f}]")
Output
95% Bootstrap CI for mean: [11.90, 14.20]
How it works
The bootstrap_ci function repeatedly resamples the original data with replacement, computes the statistic for each resample, and then uses the sorted list of bootstrap statistics to find the percentile-based confidence interval. Setting seed ensures reproducibility, which is important for debugging and sharing results. The function accepts any callable statistic, making it generic for means, medians, or custom metrics.
Common mistakes
- Not setting a seed, which makes results non-reproducible
- Using `random.sample` without replacement instead of `random.choice` with replacement
- Forgetting that the statistic function must handle the resampled data correctly
Variations
- Use `numpy.random.choice` for faster resampling on larger datasets
- Employ the `bootstrap` function from `scipy.stats` for built-in CI computation
Real-world use cases
- Calculating the confidence interval for an A/B test’s conversion rate difference without parametric assumptions.
- Estimating the uncertainty of a median or other robust statistic in data analysis reports.
- Validating the stability of a machine learning model’s performance metric on limited data.
Sponsored
More from A/B testing & experimentation
Keep learning
Related tutorials and quizzes for this topic.