#82Bootstrap Confidence Interval (Percentile)Medium

Bootstrap Confidence Interval (Percentile)

Background

The percentile bootstrap is the universal confidence-interval tool: resample your data with replacement, recompute the statistic each time, take the (α/2,1α/2)(\alpha/2, 1 - \alpha/2) quantiles of the bootstrap distribution. No parametric assumptions, no closed form needed.

Problem statement

Implement bootstrap_ci(data, stat_fn, n_bootstrap=2000, alpha=0.05, seed=0). For each bootstrap iteration, sample nn values from data with replacement, apply stat_fn, collect BB statistics, and return (lo,hi)(\text{lo}, \text{hi}) at the α/2\alpha/2 and 1α/21 - \alpha/2 percentiles.

Input

  • data - sequence of values.
  • stat_fn - function taking a sequence -> scalar.
  • n_bootstrap - int >= 100.
  • alpha - float in (0,1)(0, 1) (default 0.050.05 for a 95% CI).
  • seed - RNG seed.

Output

  • (lo, hi) - tuple of plain Python floats.

Examples

Input: data=[1,2,3,4,5,6,7,8,9,10], stat_fn=mean
Output: CI roughly around mean ~ 5.5 with bounds about (4, 7)

Constraints

  • Reproducible via seed.
  • Return Python floats.

Notes

  • Why percentile, not normal. No assumption that the statistic is normally distributed. Works for medians, quantiles, ratios, anything.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Mean CI on 1..10 (example)
  • Median CI on 1..20 (reference)
  • Mean CI at the 90% level (sample)