Lesson 32: Practice with hacker stats

(c) 2018 Justin Bois. With the exception of pasted graphics, where the source is noted, 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 document was prepared at Caltech with financial support from the Donna and Benjamin M. Rosen Bioengineering Center.

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 want to include this in a module so you can use it over and over again. (I will not be providing it in the bootcamp_utils module; I want you to write this yourself.)

  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. Plot them with opacity = 0.3.
  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.
    • Add these samples to a DataFrame.
  4. Plot the bootstrap sample ECDFs in a different color with opacity = 0.01.

In order to do this, you will need to allow for a larger data frame for your plot using

alt.data_transformers.enable('default', max_rows=None)

or by using

alt.data_transformers.enable('json')

Be aware of the caveats of that, though.