Bootstrap: How Much Could the Mean Wobble?

Probability and StatisticsMedium+15 XP+15

Bootstrap: How Much Could the Mean Wobble?

You have one small sample and you want to know how much its mean could have wobbled. The bootstrap answers that without any formula: pretend your sample is the population, draw new samples from it with replacement, and look at how much the mean moves around.

Here the draws are handed to you so the answer is reproducible. resample_indices is a list of draws, and each draw is a list of positions into values — the same position may appear more than once, which is what "with replacement" means.

Task: write bootstrap_ci(values, resample_indices, confidence) returning [centre, lower, upper], each rounded to 4 decimal places:

  1. For each draw, pull out those values and take their mean. That gives you one bootstrap mean per draw.
  2. centre is the mean of all those bootstrap means.
  3. lower and upper are the percentile edges that leave confidence percent in the middle. With confidence = 90, you cut 5% off each end, so you want the 5th and 95th percentiles of the bootstrap means.

For those two percentiles, use the same sliding rule as the percentile problem, on the sorted bootstrap means:

position=(m−1)×p100\text{position} = (m - 1) \times \frac{p}{100}position=(m−1)×100p​

where m is how many bootstrap means you have, interpolating between neighbours when the position isn't a whole number.

  • Each tail is (100 - confidence) / 2 percent, so the two percentiles are at tail and 100 - tail.
  • Draws can be any length, and don't have to match the length of values.

The width of that interval is the real output here: a narrow one means the mean is pinned down, a wide one means your sample is too small to say much.

Examples

values = [1, 2, 3, 4]
resample_indices = [[0,1,2,3],[0,0,1,1],[2,3,3,3],[1,2,2,0]]
confidence = 50
returns [2.5, 2.0625, 2.8125]
values = [5, 7]
resample_indices = [[0,1],[0,0],[1,1]]
confidence = 90
returns [6.0, 5.1, 6.9]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run