Drawing K-S replicates
Now, you need a function to draw Kolmogorov-Smirnov replicates out of a target distribution, f
. Construct a function with signature draw_ks_reps(n, f, args=(), size=10000, n_reps=10000)
to do so. Here, n
is the number of data points, and f
is the function you will use to generate samples from the target CDF. For example, to test against an Exponential distribution, you would pass np.random.exponential
as f
. This function usually takes arguments, which must be passed as a tuple. So, if you wanted to take samples from an Exponential distribution with mean x_mean
, you would use the args=(x_mean,)
keyword. The keyword arguments size
and n_reps
respectively represent the number of samples to take from the target distribution and the number of replicates to draw.
This exercise is part of the course
Case Studies in Statistical Thinking
Exercise instructions
- Write a function with signature
draw_ks_reps(n, f, args=(), size=10000, n_reps=10000)
that does the following.- Generate
size
samples from the target distributionf
. Remember, to pass theargs
into the sampling function, you should use thef(*args, size=size)
construction. Store the result asx_f
. - Initialize the replicates array,
reps
, as an empty array withn_reps
entries. - Write a
for
loop to do the followingn_reps
times.- Draw
n
samples fromf
. Again, use*args
in your function call. Store the result in the variablex_samp
. - Compute the K-S statistic using
dcst.ks_stat()
, which is the function you wrote in the previous exercise, conveniently stored in thedcst
module. Store the result in thereps
array.
- Draw
- Return the array
reps
.
- Generate
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
____ ____:
# Generate samples from target distribution
x_f = ____
# Initialize K-S replicates
reps = ____
# Draw replicates
for i in range(n_reps):
# Draw samples for comparison
x_samp = ____
# Compute K-S statistic
____[i] = ____
return reps