Exercise 5.1: Writing functions for bootstrap replicates
It will be useful to have some functions in your arsenal to do statistical inference with bootstrapping. In this exercise, you will write some handy functions. You should test these functions out on real data.
a) In the lessons, we wrote a function, draw_bs_rep()
to draw a single bootstrap replicate out of a single set of repeated measurements. Update this function to have a size
keyword argument so that you can draw many bootstrap replicates and return a Numpy array of the replicates. Here are step-by-step instructions.
Define a function with call signature
draw_bs_reps(data, func, rg, size=1, args=())
, wherefunc
is a function that takes in an array and returns a statistic; it has call signaturefunc(data, *args)
. Examples that could be passed in asfunc
arenp.mean
,np.std
,np.median
, or a user-defined function.rg
is an instance of a Numpy random number generator.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.
b) Write a function analogous to the one in part (a) except for pairs bootstrap. The call signature should be draw_bs_pairs(data1, data2, func, rg, size=1, args=())
, where func
has call signature func(data1, data2, *args)
.
You will want to include these in a module so you can use it over and over again. I will not be providing this functionality in the bootcamp_utils
module; I want you to write this yourself. (Or, you can install the dc_stat_think module that I wrote using pip
, which has this and many other useful functions for bootstrapping.)