LoslegenKostenlos loslegen

Generating many bootstrap replicates

The function bootstrap_replicate_1d() from the video is available in your namespace. Now you'll write another function, draw_bs_reps(data, func, size=1), which generates many bootstrap replicates from the data set. This function will come in handy for you again and again as you compute confidence intervals and later when you do hypothesis tests.

For your reference, the bootstrap_replicate_1d() function is provided below:

def bootstrap_replicate_1d(data, func):
    """Generate bootstrap replicate of 1D data."""
    bs_sample = np.random.choice(data, len(data))
    return func(bs_sample)

Diese Übung ist Teil des Kurses

Statistical Thinking in Python (Part 2)

Kurs anzeigen

Anleitung zur Übung

  • Define a function with call signature draw_bs_reps(data, func, size=1).
    • Using np.empty(), initialize an array called bs_replicates of size size to hold all of the bootstrap replicates.
    • Write a for loop that ranges over size and computes a replicate using bootstrap_replicate_1d(). Refer to the exercise description above to see the function signature of bootstrap_replicate_1d(). Store the replicate in the appropriate index of bs_replicates.
    • Return the array of replicates bs_replicates. This has already been done for you.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

def draw_bs_reps(data, func, size=1):
    """Draw bootstrap replicates."""

    # Initialize array of replicates: bs_replicates
    bs_replicates = ____

    # Generate replicates
    for i in ____:
        bs_replicates[i] = ____

    return bs_replicates
Code bearbeiten und ausführen