Generating a permutation sample
In the video, you learned that permutation sampling is a great way to simulate the hypothesis that two variables have identical probability distributions. This is often a hypothesis you want to test, so in this exercise, we will write a function to generate a permutation sample from two data sets.
Remember, a permutation sample of two arrays having respectively n1 and n2 entries is constructed by concatenating the arrays together, scrambling the contents of the concatenated array, and then taking the first n1 entries as the permutation sample of the first array and the last n2 entries as the permutation sample of the second array.
This exercise is part of the course
Statistical Thinking in Python (Part 2)
Exercise instructions
- Define a function with this signature:
permutation_sample(data_1, data_2).- Concatenate the two input arrays into one using
np.concatenate(). Be sure to pass indata_1anddata_2as one argument `(data1, data2). - Use
np.random.permutation()to permute the concatenated array. - Store the first
len(data_1)entries asperm_sample_1and the lastlen(data_2)entries asperm_sample_2. - Return
perm_sample_1andperm_sample_2.
- Concatenate the two input arrays into one using
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
def permutation_sample(data1, data2):
"""Generate a permutation sample from two data sets."""
# Concatenate the data sets: data
data = ____
# Permute the concatenated array: permuted_data
permuted_data = ____
# Split the permuted array into two: perm_sample_1, perm_sample_2
perm_sample_1 = permuted_data[____]
perm_sample_2 = permuted_data[____]
return perm_sample_1, perm_sample_2