Bootstrap Confidence Interval
Estimate a confidence interval for any statistic by resampling with replacement, using a seeded random generator for repeatable runs.
Code
import random
import statistics
from collections.abc import Callable, Sequence
def bootstrap_confidence_interval(
sample: Sequence[float],
*,
statistic: Callable[[Sequence[float]], float] = statistics.mean,
iterations: int = 2000,
confidence: float = 0.95,
seed: int | None = None,
) -> tuple[float, float, float]:
if not sample:
raise ValueError("sample must not be empty")
if not 0 < confidence < 1:
raise ValueError("confidence must be between 0 and 1")
if iterations < 100:
raise ValueError("use at least 100 iterations")
rng = random.Random(seed)
size = len(sample)
estimates: list[float] = []
for _ in range(iterations):
resample = [sample[rng.randrange(size)] for _ in range(size)]
estimates.append(statistic(resample))
estimates.sort()
alpha = (1 - confidence) / 2
lower = estimates[int(alpha * iterations)]
upper = estimates[min(iterations - 1, int((1 - alpha) * iterations))]
return lower, upper, statistic(sample)
def main() -> None:
latencies = [12.0, 15.5, 11.2, 40.0, 13.8, 14.1, 16.0, 12.9, 55.0, 13.0]
lower, upper, observed = bootstrap_confidence_interval(
latencies, iterations=4000, seed=42
)
print(f"mean={observed:.2f} 95% CI=({lower:.2f}, {upper:.2f})")
median_lower, median_upper, _ = bootstrap_confidence_interval(
latencies,
statistic=statistics.median,
iterations=4000,
seed=7,
)
print(f"median CI=({median_lower:.2f}, {median_upper:.2f})")
if __name__ == "__main__":
main()Inside the snippet
Everything happens inside the functions bootstrap_confidence_interval and main.
Repetition is confined to for _ in range(iterations) — the rest of the file is straight-line. Reach for statistics and what you get is Callable, Sequence — that is the whole API contact area. ValueError is the guard’s signal — thrown at the branch, before any partial work happens.
Watch out for
- Requires
statisticsas a package; the snippet will not run without it. - The guard
not sampleshort-circuits the flow with an earlyraisewhen its condition holds.
Using it
The primary signature is def bootstrap_confidence_interval( sample: Sequence[float], *, statistic: Callable[[Sequence[float]], float…
Running the file prints labeled results such as mean=, median CI=( — each operation’s effect is visible in the output.
Start the program and main runs bootstrap_confidence_interval one after another.
Key statements
def bootstrap_confidence_interval(
sample: Sequence[float],
*,
statistic: Callable[[Sequence[float]], float] = statistics.mean,
iterations: int = 2000,
confidence: float = 0.95,
seed: int | None = None,
) -> tuple[float, float, float]:
Interactive Tools for This Topic
FAQ
- What does the snippet do with malformed data?
- The code refuses to continue: `ValueError` is thrown while the state is still recoverable.
- What does Bootstrap Confidence Interval print when run?
- Execute the whole file; each printed line is prefixed with a label like `mean=`, `median CI=(`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.