Permutation Tests Explained: A Hands-On Guide
A permutation test asks how often a difference as large as the one you observed would turn up if the group labels meant nothing. You pool the data, reassign the labels in every possible way (or in thousands of random ways), recompute the difference each time, and report the share of relabellings at least as extreme as yours as the p-value. It needs no normality assumption, only that the observations are exchangeable when the null hypothesis is true.
Last updated · by the simstat.com editorial team · How we write
The idea: shuffle the labels
You have two groups, say treated and untreated, and the treated group’s average is higher. Could a gap that size appear by chance? A permutation test answers this directly. Suppose the treatment does nothing. Then the labels “treated” and “control” are arbitrary: each value would have been the same whichever group it landed in. Under that null hypothesis the labels are exchangeable, meaning any reshuffling of them is as plausible as the one you happened to observe.
So reshuffle them. Pool all the values, deal them out again into groups of the original sizes, and compute the difference between the groups. Do that for every possible split, or for thousands of random ones, and you get the permutation distribution: the differences that random labelling alone produces. If your observed difference sits far out in its tails, chance labelling rarely does that well, and you have evidence against the null hypothesis.
Hesterberg (2014), the longer preprint version of his 2015 article, explains the name: choosing which observations form the first group, without replacement, is equivalent to randomly permuting all the labels. Phipson and Smyth (2010) trace the idea to Fisher (1935), who assumed that every permutation would be enumerated.
The test statistic and the p-value
The test statistic is the number you compare across relabellings. For two groups A and B it is usually a difference:
where s is the mean or the median and A and B are the values currently labelled as each group. Write t for the value of T in your real data and T* for its value after a relabelling. With values in A and in B there are
distinct ways to choose which values form group A. The exact two-sided p-value is the share of those M splits whose statistic is at least as far from zero as yours:
Here # counts splits, and the real labelling is one of the M splits, so it is always counted. The two one-sided p-values count T* ≥ t (evidence that A is larger) or T* ≤ t (evidence that A is smaller) instead.
The statistic does not have to be a difference in means. Hesterberg (2014) points out that the method works the same way for a difference in medians or trimmed means, with no new formulas, and that, given the pooled data, the difference in means, either group mean alone and the pooled-variance t statistic order the splits identically, so they give the same one-sided p-values.
One-sided or two-sided?
Use a one-sided p-value only if you fixed the direction before seeing the data. For a two-sided test there are two conventions, and software differs:
- Count absolute values, P(|T| ≥ |t|), as in the formula above. This is the main two-sided p-value our permutation test tool reports. It treats |T| as the test statistic, like the two-sided statistic in Phipson and Smyth’s (2010) example, the absolute value of a t statistic, so it is a permutation p-value in its own right and can never exceed 1.
- Double the smaller one-sided p-value. Hesterberg (2014) recommends this, because permutation distributions need not be symmetric or centred at zero, so the evidence on each side should be measured separately. The tool shows this value too, on its own line labelled “doubled”, capped at 1.
When the permutation distribution is symmetric about zero the two agree. With unequal group sizes or a skewed statistic they can differ, as the worked example below shows. Say which one you used.
Exact enumeration or random permutations
For small groups the computer can list every split, and the p-value above is exact: no simulation error at all. M grows quickly with the sample sizes, though, so for larger data you draw B random relabellings instead, a Monte Carlo permutation test. The p-value is then computed as
where is the statistic for the b-th random relabelling, b = 1, …, B. The +1 in numerator and denominator counts your real data as one of the arrangements. Hesterberg (2014) gives the same +1 rule for one-sided p-values and notes that it prevents reporting a p-value of zero, which is impossible, since the observed split is always one of the possible ones.
Phipson and Smyth (2010) make the stronger case. Dropping the +1 and reporting count / B treats the p-value as a quantity to be estimated. They point out that this understates the p-value by about 1/B, show that it can push the actual type I error rate above the nominal level, and they argue that a p-value of exactly zero makes no inferential sense. With many tests and a multiple-comparison correction, such zeros can produce findings that look highly significant even when every null hypothesis is true. They describe (count + 1)/(B + 1) as valid but conservative when the random permutations are drawn independently, with replacement, which they say is by far the most common practice.
A Monte Carlo p-value also carries simulation error. Like any proportion estimated from B draws, it has a standard error of about √(p(1 − p)/B); Hesterberg (2015) uses this for a one-sided p-value. With B = 10,000, a p-value near 0.05 has a standard error of about 0.0022. Hesterberg (2015) recommends 10,000 resamples for routine use, and more if the result is borderline. An exact test, where the groups are small enough to run one, has no simulation error at all.
A worked example: mouse survival times
The data are survival times, in days, of mice in a small experiment used by Efron and Tibshirani (1993): a treatment group of 7 mice (94, 197, 16, 38, 99, 141, 23) and a control group of 9 mice (52, 104, 146, 10, 50, 31, 40, 27, 46), as listed in the book’s companion R package, bootstrap (Tibshirani and Leisch, 2019). These are the default values in our permutation test tool.
Worked example: an exact permutation test for the difference in means
Step 1: the observed statistic. The treatment mean is 86.86 days and the control mean 56.22 days, so t = 86.86 − 56.22 = 30.63 days.
Step 2: every relabelling. Pool the 16 values. There are C(16, 7) = 11,440 ways to choose which 7 of them to call “treatment”. For each split, compute the treatment mean minus the control mean.
Step 3: count. Of the 11,440 splits, 1,608 give a difference of at least 30.63 days, and 3,182 give a difference at least 30.63 days from zero in either direction. So
- one-sided p (treatment longer): 1,608 / 11,440 = 0.1406;
- one-sided p (treatment shorter): 0.8611;
- two-sided p, P(|T| ≥ |t|): 3,182 / 11,440 = 0.2781.
Doubling the smaller one-sided value instead gives 0.2811, slightly different, because with groups of 7 and 9 the permutation distribution is not exactly symmetric.
The same test for medians. The medians are 94.00 and 46.00 days, a difference of 48.00. Exact enumeration gives a one-sided p of 0.1818 and a two-sided p, P(|T| ≥ |t|), of 0.3024. Here the two conventions part company more clearly: twice the smaller one-sided p is 0.3636.
Random permutations for comparison. With the tool’s random method, 10,000 permutations and seed 1993, the first three relabellings put 50, 99, 27, 16, 31, 23, 141, then 23, 99, 27, 10, 40, 50, 38, then 40, 94, 46, 27, 31, 52, 146 in the treatment group, giving differences of −25.49, −50.89 and −13.05 days. Over all 10,000, 2,701 are at least 30.63 days from zero, so the two-sided p is (2,701 + 1) / 10,001 = 0.2702, with a simulation standard error of 0.0044 (the tool reports twice this, ±0.0089, as its simulation error). The exact value, 0.2781, lies inside the run’s ±2 standard error range of 0.2613 to 0.2791.
A difference of 30.63 days looks large, but random labelling produces one at least that large, in one direction or the other, in about 28% of all splits. With 7 and 9 mice, these data do not rule out chance. That is not the same as showing the treatment has no effect: the sample is small and the survival times vary a great deal.
To reproduce the numbers, open the tool (the mouse data are preloaded), choose means or medians and keep “Exact if possible”. For the random run, choose “Random permutations”, 10,000 permutations and seed 1993.
What the test assumes and what it tells you
A permutation test makes no assumption about the shape of the distribution. Phipson and Smyth (2010) put its requirement this way: under the null hypothesis the observations are independent and identically distributed. That is what makes the labels exchangeable, and it has consequences:
- The null hypothesis is “same distribution”, not “same mean”. Because the data are pooled, a difference in spread can also affect the p-value. Hesterberg (2014) notes that you cannot use a plain permutation test to compare means when the population variances may differ under the null hypothesis, because then you cannot pool the data.
- Observations must be independent. Paired, clustered or repeated measurements need a scheme that shuffles only what is exchangeable, such as swapping labels within pairs.
- It gives a p-value, not an effect size. It does not tell you how large the difference is or how precisely you have measured it, and Hesterberg (2014) notes that permutation testing cannot give confidence intervals.
Permutation test or bootstrap?
Both methods resample your data, but they answer different questions. A permutation test asks whether a difference could be chance: it resamples in a way that is consistent with the null hypothesis, so for a difference in means its distribution is centred at zero. The bootstrap asks how precisely you have estimated something: it resamples each group with replacement, makes no such null assumption, and its distribution is centred near your estimate (Hesterberg, 2015). Hesterberg (2014) describes using the two together: the permutation test to see whether there is a difference, the bootstrap to quantify the variability of the estimated difference. Hesterberg (2015) also notes that pooling the two samples and drawing bootstrap samples is a less accurate way to test than a permutation test.
In practice, report the permutation p-value alongside a confidence interval. Our bootstrap confidence interval calculator works on one sample at a time, so it gives an interval for each group’s mean or median. An interval for the difference itself needs a two-sample bootstrap, which resamples each group separately and recomputes the difference (Hesterberg, 2015).
How to report a permutation test
Mean survival was 86.86 days in the treatment group (n = 7) and 56.22 days in the control group (n = 9), a difference of 30.63 days. An exact two-sided permutation test over all 11,440 possible group assignments gave p = 0.2781.
Include:
- the test statistic (difference in means, medians or something else) and its observed value;
- exact enumeration or random permutations, and for random permutations the number B, the seed and the p-value formula, (count + 1)/(B + 1);
- the definition of the two-sided p-value: P(|T| ≥ |t|) or twice the smaller one-sided p-value;
- a measure of effect size with its uncertainty, such as a bootstrap confidence interval for the difference.
Common mistakes
Reporting p = 0. A Monte Carlo permutation test can never give exactly zero if it is computed as (count + 1)/(B + 1): the smallest possible value is 1/10,001 with 10,000 permutations. If no random relabelling is at least as extreme as yours, report that smallest value, 1/(B + 1), and say it is a floor set by B, or run an exact test. Phipson and Smyth (2010) warn that many public software packages can return p-values of exactly zero, and explain why these mislead.
Choosing the one-sided p-value after seeing the data. Picking the direction that matches the observed difference roughly halves the p-value for free. Decide the alternative hypothesis before you look.
Not saying which two-sided p-value you used. P(|T| ≥ |t|) and twice the smaller one-sided p differ when the permutation distribution is asymmetric, as the medians in the example show (0.3024 versus 0.3636).
Using too few random permutations. With B = 500, a p-value near 0.05 has a standard error of about 0.0097, enough to move it across a significance threshold. Check the simulation error, √(p(1 − p)/B), or rerun with another seed; if the conclusion moves, increase B.
Shuffling observations that are not exchangeable. Permuting labels across pairs, clusters or time points breaks the structure of the data and gives a wrong null distribution.
Reading a large p-value as “no difference”. In the example, a 30.63-day difference was not statistically significant with 7 and 9 mice. Absence of evidence in a small sample is not evidence of absence; a confidence interval shows what differences are still compatible with the data.
Where to go next
Run the test on your own data with our permutation test, and put an interval around each group’s mean or median with the bootstrap confidence interval calculator. For the estimation side of resampling, read what bootstrapping is and how it works. The resampling hub collects our other tools and guides on simulation-based methods.
References
- Phipson, B. & Smyth, G. K. (2010). Permutation P-values Should Never Be Zero: Calculating Exact P-values When Permutations Are Randomly Drawn. Statistical Applications in Genetics and Molecular Biology, 9(1), Article 39.
- Hesterberg, T. C. (2015). What Teachers Should Know About the Bootstrap: Resampling in the Undergraduate Statistics Curriculum. The American Statistician, 69(4), 371–386.
- Hesterberg, T. C. (2014). What Teachers Should Know about the Bootstrap: Resampling in the Undergraduate Statistics Curriculum. arXiv preprint arXiv:1411.5279 (longer version of the 2015 article).
- Tibshirani, R. & Leisch, F. (2019). bootstrap: Functions for the Book "An Introduction to the Bootstrap" (R package version 2019.6), data set mouse.c. CRAN.
- Efron, B. & Tibshirani, R. J. (1993). An Introduction to the Bootstrap. Chapman & Hall/CRC.
Try it
- Permutation test: Test a difference in means or medians between two groups by shuffling group labels, exactly or by simulation.
- Bootstrap confidence interval: Resample your data to get percentile and BCa confidence intervals for a mean, median or standard deviation.