Bootstrap vs. parametric confidence intervals#

Compares a bootstrap confidence interval for the median (which has no simple closed form) against the parametric t-based interval for the mean, on the same skewed dataset.

import numpy as np

from mathematicskit.statistics import bootstrap_confidence_interval, mean_confidence_interval

A skewed dataset where the mean and median tell different stories#

rng = np.random.default_rng(0)
data = rng.exponential(scale=5.0, size=300)

mean_result = mean_confidence_interval(data, sigma=None)
median_result = bootstrap_confidence_interval(data, statistic=np.median, n_resamples=5000, seed=0)

print(f"mean:   {mean_result.estimate:.3f}, 95% CI ({mean_result.lower:.3f}, {mean_result.upper:.3f})")
print(f"median: {median_result.estimate:.3f}, 95% CI ({median_result.lower:.3f}, {median_result.upper:.3f})")
mean:   5.793, 95% CI (5.153, 6.433)
median: 4.006, 95% CI (3.505, 4.804)

Total running time of the script: (0 minutes 0.027 seconds)

Gallery generated by Sphinx-Gallery