Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.

Bootstrap the Sampling Distribution

~15 mincode completion

The confidence interval you built earlier assumed a formula. The bootstrap assumes almost nothing and works for statistics that have no clean formula at all — a median, an F1 score, an AUC.

The idea is almost impudent: resample your own data, with replacement, and watch the statistic move.

  • Draw points from your sample with replacement — so some appear twice, some not at all.
  • Compute the statistic on that resample.
  • Repeat times.
  • The spread of those values estimates the spread of the statistic.
  • With replacement is the whole trick. Without it, every resample is the original sample in a different order, and the statistic never moves.

    x = [1, 2, 3, 4]
    a resample might be [2, 2, 4, 1] -> mean 2.25
    another might be    [3, 1, 1, 3] -> mean 2.00
    the spread of those means is the standard error

    Your task:

    Implement bootstrap_means(x, n_boot, seed) returning the resample means as an array of length n_boot.

    Seed first with np.random.seed(seed), then draw each resample with np.random.randint(0, n, size=n) as index positions. The exact call order matters, because the tests compare against a specific seeded run.

    Example Tests

    Four resamples of a four-point sample, seeded at 0

    Input: {"x":[1,2,3,4],"seed":0,"n_boot":4}

    Expected: [2,4,2.75,2.25]

    A constant sample cannot move, however you resample it

    Input: {"x":[5,5,5],"seed":1,"n_boot":3}

    Expected: [5,5,5]

    Python
    import numpy as np
    
    
    def bootstrap_means(x, n_boot, seed):
        """
        Means of n_boot bootstrap resamples.
    
        Args:
            x:      the observed sample, shape (n,)
            n_boot: how many resamples to draw
            seed:   passed to np.random.seed before drawing anything
    
        Returns:
            array of shape (n_boot,) of resample means
        """
        # YOUR CODE HERE
        pass
    
    Loading docs…