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
sizesamples from the target distributionf. Remember, to pass theargsinto 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_repsentries. - Write a
forloop to do the followingn_repstimes.- Draw
nsamples fromf. Again, use*argsin 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 thedcstmodule. Store the result in therepsarray.
- 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