Put a bootstrap confidence interval on an evaluation score

An agent that passes 62 percent of 50 test tasks might really be a 50 percent agent or a 75 percent agent. A bootstrap interval shows that uncertainty without any distribution assumptions.

Write bootstrap_ci(x, n_boot=1000, alpha=0.05, seed=0) for a 1D array x of length n. Follow these steps exactly so results are reproducible:
1. rng = np.random.default_rng(seed).
2. idx = rng.integers(0, n, size=(n_boot, n)), sampling with replacement.
3. means = mean of x[idx] along axis 1.
4. Return (lo, hi) as a tuple of floats: np.percentile(means, 100 * alpha / 2) and np.percentile(means, 100 * (1 - alpha / 2)), default linear interpolation.

Signaturedef bootstrap_ci(x, n_boot=1000, alpha=0.05, seed=0)

Example

for x of all 1s every resample has mean 1, so the result is (1.0, 1.0).

Understand the idea

The bootstrap estimates how uncertain a statistic is by resampling your own data. Draw n values with replacement many times, compute the mean of each resample, and read off percentiles of those means as a confidence interval. No normal-distribution assumption is needed. Like re-running a small survey many times by reshuffling the answers you already have. Worked example: x = [0, 1]. A resample of size 2 can be [0, 0], [0, 1], [1, 0] or [1, 1], with means 0, 0.5, 0.5, 1. So about a quarter of bootstrap means are 0, half are 0.5 and a quarter are 1, and a 95% interval comes out near (0, 1), wide because two points tell you little. For all-ones data every mean is 1, giving (1.0, 1.0). Note rng.integers(0, n) excludes n, so it covers every index; using n - 1 would never pick the last element. Rule: idx = rng.integers(0, n, (n_boot, n)); means = x[idx].mean(1); lo, hi = percentiles at 100 alpha / 2 and 100 (1 - alpha / 2). Agent eval reports use bootstrap intervals.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: constant databootstrap_ci(np.ones(10))
  • Sample 2: pass/fail scoresbootstrap_ci(_pass)
  • Test 3: 90 percent interval with another seedbootstrap_ci(_lat, n_boot=500, alpha=0.1, seed=3)
  • Test 4: last element mattersbootstrap_ci([0.0, 0.0, 0.0, 10.0], n_boot=200, seed=1)