Bootstrap Confidence Interval Estimate
Estimate confidence intervals using bootstrap resampling for robust statistical inference.
Code
import numpy as np
def bootstrap_ci(data, statistic=np.mean, n_boot=10000, ci=0.95):
boots = [statistic(np.random.choice(data, size=len(data), replace=True)) for _ in range(n_boot)]
lower = np.percentile(boots, (1 - ci) / 2 * 100)
upper = np.percentile(boots, (1 + ci) / 2 * 100)
return lower, upper
def bootstrap_mean(data, n_boot=10000):
return bootstrap_ci(data, np.mean, n_boot)Inside the snippet
Bootstrap Confidence Interval Estimate reduces to the functions bootstrap_ci and bootstrap_mean.
Points of care
- Plan for an install step:
numpyis a real dependency, resolved by the package manager.
Putting it to use
The primary signature is def bootstrap_ci(data, statistic=np.mean, n_boot=10000, ci=0.95)
Treat bootstrap_ci as the API: import it, and ignore the supporting code.
Core lines
def bootstrap_ci(data, statistic=np.mean, n_boot=10000, ci=0.95):
boots = [statistic(np.random.choice(data, size=len(data), replace=True)) for _ in range(n_boot)]
lower = np.percentile(boots, (1 - ci) / 2 * 100)
upper = np.percentile(boots, (1 + ci) / 2 * 100)
return lower, upper
def bootstrap_mean(data, n_boot=10000):
return bootstrap_ci(data, np.mean, n_boot)
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.Statistics CalculatorCompute count, sum, mean, median, minimum, maximum, range, variance, and standard deviation for a number list.Text StatisticsCount characters, words, lines, sentences, paragraphs, and estimated reading time entirely in the browser with no text sent to a server.
FAQ
- Which packages must be installed first?
- Yes. The imports resolve to `numpy`, which the package manager must provide.
Report a problem with this exampleOpens a pre-filled issue on GitHub.