Randomization test (permutation test) for two groups

Updated

A randomization test, also called a permutation test, asks whether the difference between two groups is larger than the random assignment of units to groups would produce on its own. If the treatment had no effect, the group labels would be arbitrary: any other split of the same values into two groups would have been just as likely. The test computes the difference for every possible split, or for many random ones, and the permutation p-value is the share of splits that give a difference at least as large as the one observed. It needs no normal distribution and no formula for a standard error.

When to use it

  • Units were assigned to the two treatments at random, as in a designed comparison of two coatings, settings or materials.
  • The samples are small and you doubt the normality behind the 2-sample t test, for example because of one long value.
  • You want a test of a statistic the standard tests do not cover, such as a difference in medians or in trimmed means.

The Mann-Whitney test is a randomization test on ranks. To estimate how large the difference is, with a confidence interval, use the 2-sample bootstrap.

How it works

With n_A values in group A and n_B in group B:

  1. Compute the observed statistic, for example D = x̄_B − x̄_A.
  2. Pool all n_A + n_B values. Split them into a group of n_B and a group of n_A in every possible way, and compute D for each split. There are C(n_A + n_B, n_B) splits.
  3. The two-sided p-value is the share of splits with |D| at least as large as the observed |D|:
p = (number of splits with |D*| ≥ |D_obs|) / C(n_A + n_B, n_B)

This is the exact p-value. When there are too many splits to list, draw a large number R of random shuffles instead, with a recorded seed. The sampled p-value then has a Monte Carlo standard error of about √(p(1 − p)/R).

The logic rests on the random assignment: under the null hypothesis, the observed split is just one of the equally likely ones. With observational data, such as two machines that were not assigned at random, the same calculation is still a useful check, but the conclusion is about the two groups, not about a cause.

Example

A plant tests a new floor coating (B) against the current one (A). Sixteen steel coupons are coated, eight with each, assigned by drawing lots, then run on an abrasion tester for 1,000 cycles. Weight loss (mg), lower is better:

CoatingWeight loss (mg)
A (current)14.0, 15.3, 12.9, 10.7, 12.2, 10.4, 14.3, 20.9
B (new)9.5, 9.1, 12.7, 12.2, 11.4, 8.3, 10.9, 13.6

The means are 13.84 mg (A) and 10.96 mg (B), so D = −2.875 mg. Coating A has one high value, 20.9 mg, and a standard deviation of 3.33 mg against 1.87 for B.

There are C(16, 8) = 12,870 ways to split the 16 values into two groups of eight, few enough to list them all:

Difference in means over all 12,870 splits, B − A0100020003000Number of splits< −4−4 to −3−3 to −2−2 to −1−1 to 00 to 11 to 22 to 33 to 4≥ 4
Difference in means over all 12,870 splits, B − A
MethodSplits countedP (two-sided)
Exact, all 12,870 splits506 at least 2.875 apart0.039
Sampled, 10,000 random shuffles, seed 20264030.040
Welch 2-sample t, for comparison0.056

The sampled version used numpy.random.default_rng(2026); its Monte Carlo standard error is about 0.002, so it agrees with the exact value. The one-sided p-value, for B lower than A, is exactly half: 253 splits, p = 0.020.

How to read the results

  • The new coating wears less. Only 3.9% of the possible random assignments give a difference as large as 2.9 mg in either direction, so chance alone is an unlikely explanation at the 5% level.
  • The t test is less sure here. Its p-value, 0.056, is pulled up by the 20.9 mg coupon, which inflates the standard deviation of group A. The randomization test uses the same mean difference but takes its reference distribution from the data themselves.
  • The result is borderline either way. p = 0.039 is not overwhelming. Before switching coatings, check the 20.9 mg coupon for a preparation problem and consider a second, larger trial.
  • The size of the effect needs an interval. The randomization test gives a p-value only. A 2-sample bootstrap or a t interval would show how large the reduction might be.
  • See the p-value explained for what 0.039 does and does not mean.

Common mistakes

  • Shuffling when units were not exchangeable. If the coupons for A were all coated on Monday and those for B on Friday, the labels are not arbitrary under the null, and the test checks the wrong thing.
  • Ignoring pairing. For paired data, shuffle the sign of each difference within its pair, not the group labels.
  • Not reporting the seed and the number of shuffles for a sampled test, or not saying the test was exact.
  • Reporting p = 0 from a sampled test. If no shuffle beats the observed value, report p < 1/R, or use (k + 1)/(R + 1).
  • Testing a different statistic after seeing the data. Choose the statistic, mean or median difference, before the test.

Frequently asked questions

What is the difference between a randomization test and a permutation test?

The calculation is the same. "Randomization test" stresses that the justification comes from the random assignment in the experiment; "permutation test" is the more general name for the same computation.

Should I use the exact or the sampled p-value?

Exact when the number of splits is manageable, up to a few million. Otherwise sample 10,000 or more shuffles with a recorded seed; the answer will be within a Monte Carlo error you can state.

Both compare the observed difference with what chance would give. The t test uses a theoretical distribution that assumes normal data; the randomization test builds the distribution from the data. With well-behaved data they give similar p-values, as R. A. Fisher noted when he introduced the idea in The Design of Experiments.

Can I use a randomization test for more than two groups?

Yes. Shuffle the labels across all groups and use a statistic such as the ANOVA F; the p-value is the share of shuffles with F at least as large as observed.

Try it with your own data

StatLab SPC is free, open-source statistics for engineers. It runs in your browser, works offline, and never uploads your data.

Open StatLab