Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Permutation tests for comparing two groups

Click here to run the notebook interactively, so you can play with the code examples.

Notebook setup


[notice] A new release of pip is available: 25.2 -> 25.3
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
<Figure size 640x480 with 0 Axes>

Introduction

The permutation tests for comparing two groups is another example of the computational approach to statistics. The permutation test uses a randomization strategy on the data to generate samples from a hypothetical distribution that represents the “no difference between groups” scenario. The procedure of generating repeated samples from the “no difference” distribution is called a permutation test.

The permutation test is an example of the hypothesis testing procedure. Classical statistics approaches the comparison of two groups by relying on pre-packaged procedures like the Welch’s two-sample t-test, which makes assumptions that the populations are normally distributed and uses complicated math formulas (analytical approximations). In contrast, the permutation test allows us to compare two groups without making assumptions about the populations’ distribution.

Statistical inference

TODO: improt short version from simulation_hypothesis_tests.ipynb

Permutation test

TODO: import from blog post and slides

Suppose we have a obtained samples from group of students who took the smart drug treated, and a similar group who didn’t take the smart drug controls.

We’ll compare the IQ scores in the two groups in terms of the difference between the average scores computed from each group.

7.8870000000000005

Statistical question?

Are the two groups the same? This is equivalent to saying the smart drug had no effect.

Disproving the skeptical colleague

We’ll now use the 10000 permutations of the original data to obtain sampling distribution of the difference between means under the null hypothesis.

Compute the p-value of the observed difference between means dscore under the null hypothesis.

0.0101
<Figure size 700x200 with 1 Axes>

Alternative using formula

0.010163611652137501

Alternative using predefined function

(7.886999999999972, 0.0108)

Conclusion

Hands-on computational methods are much better compared to formulas and theory: no need to memorize formulas, which makes it easier for beginners, because the computations are directly connected with definition. Learners need less math theory, and get more practical experience. Furthermore, the resampling tests are more general, since they don’t require assumptions about specific population distribution.