Get startedGet started for free

Correlation of offspring and parental data

In an effort to quantify the correlation between offspring and parent beak depths, we would like to compute statistics, such as the Pearson correlation coefficient, between parents and offspring. To get confidence intervals on this, we need to do a pairs bootstrap.

You have already written a function to do pairs bootstrap to get estimates for parameters derived from linear regression. Your task in this exercise is to make a new function with call signature draw_bs_pairs(x, y, func, size=1) that performs pairs bootstrap and computes a single statistic on pairs samples defined. The statistic of interest is computed by calling func(bs_x, bs_y). In the next exercise, you will use pearson_r for func.

This exercise is part of the course

Statistical Thinking in Python (Part 2)

View Course

Exercise instructions

  • Set up an array of indices to sample from. (Remember, when doing pairs bootstrap, we randomly choose indices and use those to get the pairs.)
  • Initialize the array of bootstrap replicates. This should be a one-dimensional array of length size.
  • Write a for loop to draw the samples.
  • Randomly choose indices from the array of indices you previously set up.
  • Extract x values and y values from the input array using the indices you just chose to generate a bootstrap sample.
  • Use func to compute the statistic of interest from the bootstrap samples of x and y and store it in your array of bootstrap replicates.
  • Return the array of bootstrap replicates.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

def draw_bs_pairs(x, y, func, size=1):
    """Perform pairs bootstrap for a single statistic."""

    # Set up array of indices to sample from: inds
    inds = ____

    # Initialize replicates: bs_replicates
    bs_replicates = ____

    # Generate replicates
    for i in range(size):
        bs_inds = ____
        bs_x, bs_y = ____
        bs_replicates[i] = ____

    return bs_replicates
Edit and Run Code