Bootstrap Confidence Interval Estimate

Estimate confidence intervals using bootstrap resampling for robust statistical inference.

PythonData Integrity

Code

pythonsnippet
10 lines
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: numpy is 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

FAQ

Which packages must be installed first?
Yes. The imports resolve to `numpy`, which the package manager must provide.