A two-sample bootstrap hypothesis test for difference of means
We now want to test the hypothesis that Frog A and Frog B have the same mean impact force, but not necessarily the same distribution, which is also impossible with a permutation test.
To do the two-sample bootstrap test, we shift both arrays to have the same mean, since we are simulating the hypothesis that their means are, in fact, equal. We then draw bootstrap samples out of the shifted arrays and compute the difference in means. This constitutes a bootstrap replicate, and we generate many of them. The p-value is the fraction of replicates with a difference in means greater than or equal to what was observed.
The objects forces_concat
and empirical_diff_means
are already in your namespace.
This is a part of the course
“Statistical Thinking in Python (Part 2)”
Exercise instructions
- Compute the mean of all forces (from
forces_concat
) usingnp.mean()
. - Generate shifted data sets for both
force_a
andforce_b
such that the mean of each is the mean of the concatenated array of impact forces. - Generate 10,000 bootstrap replicates of the mean each for the two shifted arrays.
- Compute the bootstrap replicates of the difference of means by subtracting the replicates of the shifted impact force of Frog B from those of Frog A.
- Compute and print the p-value from your bootstrap replicates.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Compute mean of all forces: mean_force
mean_force = ____
# Generate shifted arrays
force_a_shifted = force_a - np.mean(force_a) + mean_force
force_b_shifted = ____
# Compute 10,000 bootstrap replicates from shifted arrays
bs_replicates_a = draw_bs_reps(____, ____, ____)
bs_replicates_b = draw_bs_reps(____, ____, ____)
# Get replicates of difference of means: bs_replicates
bs_replicates = ____
# Compute and print p-value: p
p = ____ / ____
print('p-value =', p)
This exercise is part of the course
Statistical Thinking in Python (Part 2)
Learn to perform the two key tasks in statistical inference: parameter estimation and hypothesis testing.
You now know how to define and estimate parameters given a model. But the question remains: how reasonable is it to observe your data if a model is true? This question is addressed by hypothesis tests. They are the icing on the inference cake. After completing this chapter, you will be able to carefully construct and test hypotheses using hacker statistics.
Exercise 1: Formulating and simulating a hypothesisExercise 2: Generating a permutation sampleExercise 3: Visualizing permutation samplingExercise 4: Test statistics and p-valuesExercise 5: Test statisticsExercise 6: What is a p-value?Exercise 7: Generating permutation replicatesExercise 8: Look before you leap: EDA before hypothesis testingExercise 9: Permutation test on frog dataExercise 10: Bootstrap hypothesis testsExercise 11: A one-sample bootstrap hypothesis testExercise 12: A two-sample bootstrap hypothesis test for difference of meansWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.