A point estimate reduces a sample to one number, but it does not show how that estimate could vary across repeated samples. scipy.stats.bootstrap() resamples observed values with replacement and returns confidence bounds for a statistic without requiring a normal-distribution formula.
For a one-sample mean, bootstrap() still expects the data as a one-item tuple such as (latency_ms,). Giving the statistic an axis parameter lets SciPy process batches of resamples, while a fixed numpy.random.Generator makes the simulated distribution reproducible.
The bias-corrected and accelerated method, abbreviated BCa, adjusts the interval limits for bias and skewness. A degenerate statistic can make BCa bounds nan, so the program checks for finite ordered bounds, a finite standard error, and the requested resample count before reporting success.
import numpy as np from scipy.stats import bootstrap latency_ms = np.array([ 117, 121, 113, 125, 119, 128, 115, 122, 120, 118, 124, 116, ]) rng = np.random.default_rng(20260625)
def mean_latency(sample, axis): return np.mean(sample, axis=axis)
result = bootstrap( (latency_ms,), mean_latency, confidence_level=0.95, n_resamples=9999, method="BCa", rng=rng, )
import numpy as np from scipy.stats import bootstrap latency_ms = np.array([ 117, 121, 113, 125, 119, 128, 115, 122, 120, 118, 124, 116, ]) rng = np.random.default_rng(20260625) def mean_latency(sample, axis): return np.mean(sample, axis=axis) result = bootstrap( (latency_ms,), mean_latency, confidence_level=0.95, n_resamples=9999, method="BCa", rng=rng, ) interval = result.confidence_interval sample_mean = np.mean(latency_ms) checks_passed = ( np.isfinite([interval.low, interval.high]).all() and np.isfinite(result.standard_error) and interval.low < interval.high and result.bootstrap_distribution.shape[-1] == 9999 ) if not checks_passed: raise RuntimeError("bootstrap result failed validation") print(f"sample mean: {sample_mean:.2f} ms") print(f"95% BCa interval: {interval.low:.2f} to {interval.high:.2f} ms") print(f"bootstrap standard error: {result.standard_error:.2f} ms") print(f"resamples: {result.bootstrap_distribution.shape[-1]}") print("validation checks: passed")
BCa can return nan bounds when identical observations or a statistic that does not vary produce a degenerate bootstrap distribution.
$ python3 bootstrap_ci.py sample mean: 119.83 ms 95% BCa interval: 117.58 to 122.25 ms bootstrap standard error: 1.20 ms resamples: 9999 validation checks: passed