Chip Foundry Services Request Assessment

Permutation Test

A permutation test answers the question "could this difference have arisen just from how the labels were assigned?" by checking directly. If the group labels carry no information, then shuffling them among the observations should produce differences as large as the real one reasonably often. The test builds that shuffled distribution from your own data, with no assumption of normality, and reads the p-value from it.

The idea in three steps:

Null: the group labels do not matter
any measurement could equally have come from either group
Shuffle the labelsall arrangements, or a large random sample of them
Pool the observations
forget which group each came from
Reassign labels at random
same group sizes as the real data
Recompute the statistic
difference in means, medians, anything
p-value = share of shuffles as extreme as observed
no distributional formula needed

The p-value. Let $T$ be any test statistic, such as the difference in group means, and $T_{\text{obs}}$ its value for the real labeling. Over all $m$ possible relabelings, the two-sided p-value is the proportion that are at least as extreme:

$$p=\frac{\#\{\text{relabelings with }|T|\ge|T_{\text{obs}}|\}}{m},\qquad m=\binom{n_1+n_2}{n_1}$$

When $m$ is too large to enumerate, a random sample of $B$ relabelings estimates it, and the estimate should be (hits+1)/(B+1) so that it is never reported as exactly zero.

Worked example. Two cleaning tools leave particle counts (hundreds per wafer) of $8.1,\,9.4,\,7.2,\,10.3,\,8.8$ for tool A and $11.0,\,12.5,\,9.9,\,13.2,\,10.6$ for tool B. The means are $8.76$ and $11.44$, a difference of $2.68$. Under the null, the ten values could be split into two groups of five in $\binom{10}{5}=252$ ways, all equally likely. Computing the difference in means for every one of them gives the distribution below. Only $4$ relabelings reach a gap of $2.68$ in absolute size: the observed split, its mirror image, and two even more lopsided ones at $\pm2.84$. So $p=4/252=0.0159$, and the one-sided p-value is $2/252=0.0079$. The result matches scipy's exact permutation test.

All 252 ways to relabel the ten measurements difference in means if the tool labels were assigned at random; the observed split is near the extreme 2 2 5 6 7 11 17 13 21 22 18 22 22 21 13 17 11 7 6 5 2 2 -3 -2 -1 0 +1 +2 +3 mean of group B minus mean of group A, after shuffling labels observed +2.68 mirror -2.68 only 4 of 252 relabelings are this extreme in either direction: p = 4/252 = 0.0159 for comparison, the t-test gives 0.0108 using a normal-theory formula

A Monte Carlo version with $10{,}000$ random shuffles gives $0.0152$, within sampling error of the exact value. For comparison, the pooled t-test gives $p=0.0108$ and Welch's version $0.0111$. They agree closely here, but the permutation result does not depend on the normality that the t-test assumes.

Why it is so flexible. The statistic can be anything you can compute: a difference in medians, a ratio of variances, a correlation, a classification accuracy. Replacing the mean with the median changes one line, and the same shuffling machinery still gives a valid p-value. That is the main reason it is popular for unusual statistics and small samples.

Using it well.

  • Exchangeability: the test is valid only if, under the null hypothesis, the observations could be swapped freely. Randomized experiments satisfy this by design; observational data and clustered or time-ordered data may not.
  • Match the design: for paired data permute the signs of the within-pair differences, not the pooled values; for a correlation, shuffle one variable against the other.
  • The null is "no effect at all": if groups differ in spread but not in means, a difference-in-means permutation test can still reject, so pick a statistic matched to the question.
  • Resolution: with $m$ relabelings the smallest possible p-value is $1/m$ (or $2/m$ two-sided). With three per group, $m=20$, and nothing below $0.1$ is possible.
  • Estimate as well: a p-value gives no effect size. Report the observed difference and a bootstrap or similar interval for it.
  • Report the method: state whether the test was exact or Monte Carlo, the number of shuffles, and the random seed.

Read a permutation test as a direct experiment on your own data: if the labels meant nothing, how often would a random shuffle look at least as striking as what you actually observed?