Lesson 28: Practice with hacker stats

(c) 2017 Justin Bois. This work is licensed under a Creative Commons Attribution License CC-BY 4.0. All code contained herein is licensed under an MIT license.

This tutorial was generated from a Jupyter notebook. You can download the notebook here.

Practice 1: Writing a function to draw bootstrap replicates

As you can imagine, it is quite useful to be able to generate, or "draw," bootstrap replicates. Now, you will write a function, draw_bs_reps() to do this automatically. You will include this function in your ~/git/bootcamp/bootcamp_utils.py module.

  1. Define a function with call signature draw_bs_reps(data, func=np.mean, size=1), where func is a function that takes in an array and returns a statistic. Examples that could be passed in as func are np.mean, np.std, np.median, or a user-defined function. size is the number of replicates to generate.
  2. Write a good doc string.
  3. Define n to be the length of the input data array.
  4. Use a list comprehension to compute a list of bootstrap replicates.
  5. Return the replicates as a Numpy array.

If you like, you can use the bs_replicate() function from the previous lesson.

Now that you have the function, feel free to play around with it and compute bootstrap replicates with the finch beak or other data you want to play with.

Practice 2: Plot ECDFs of bootstrap samples

To help you visualize how bootstrap samples can show what you might expect by repeating experiments, we will make a plot of ECDFs of lots of bootstrap samples.

  1. Load in the beak depth data from 1975.
  2. Generate the x and y values for the ECDF of the data and plot them as before. You should use the ecdf() function you already wrote. When you make the plot, use the color=#1f77b4' kwarg of plt.plot().
  3. Write a for loop to do the following 100 times:
    • Generate a bootstrap sample from the data set using np.random.choice().
    • Compute the x and y values for the ECDF of the bootstrap sample.
    • Use ax.plot() with kwargs color='#d62728' and alpha=0.01 to display the ECDF of the bootstrap sample.