(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.
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.
- Define a function with call signature
draw_bs_reps(data, func=np.mean, size=1)
, wherefunc
is a function that takes in an array and returns a statistic. Examples that could be passed in asfunc
arenp.mean
,np.std
,np.median
, or a user-defined function.size
is the number of replicates to generate.- Write a good doc string.
- Define
n
to be the length of the inputdata
array.- Use a list comprehension to compute a list of bootstrap replicates.
- 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.
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.
- Load in the beak depth data from 1975.
- Generate the
x
andy
values for the ECDF of the data and plot them as before. You should use theecdf()
function you already wrote. When you make the plot, use thecolor=#1f77b4'
kwarg ofplt.plot()
.- 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
andy
values for the ECDF of the bootstrap sample.- Use
ax.plot()
with kwargscolor='#d62728'
andalpha=0.01
to display the ECDF of the bootstrap sample.