Run a two-sample permutation test for a difference in means
Why it matters Permutation tests are a robust way to check whether a new model version or prompt really beats the old one on a small eval set.
A permutation test asks: if the group labels were meaningless, how often would shuffling them give a difference as big as the one we saw? It needs no normality assumption.
Write permutation_test(a, b, perms). a and b are 1D samples. The statistic is mean(a) - mean(b). perms is a list of permutations of range(len(a) + len(b)), supplied so the result is deterministic. For each permutation, reorder the pooled sample np.concatenate([a, b]) by it, call the first len(a) values group a and the rest group b, and compute the statistic. Return (obs, p) as floats where obs is the observed statistic and p = (1 + count) / (1 + len(perms)), with count the number of permuted statistics whose absolute value is >= |obs| - 1e-12 (two-sided).
def permutation_test(a, b, perms)Example
a [5, 6, 7], b [1, 2, 3], perms = all 720 permutations of range(6) gives (4.0, 0.101248).
Understand the idea
Suppose model A scored 5, 6, 7 and model B scored 1, 2, 3. Is that a real difference or luck? A permutation test plays devil's advocate: if the labels A and B did not matter, any split of these six numbers into two groups of three would be equally likely. So we try the splits and see how often the difference is as big as what we observed.
Worked example: the observed difference is 6 - 2 = 4. Out of all 20 ways to choose which three numbers are 'A', only 2 give a difference of absolute value 4 or more: the real split and its mirror image. Over all 720 orderings that is 72 out of 720, so p = (1 + 72) / (1 + 720) = 0.101. With only three items per group, even a perfect separation is not very convincing.
Rule: p = (1 + #{|stat(permuted)| >= |stat(observed)|}) / (1 + number of permutations).
The +1 counts the observed labelling itself and keeps p above 0. Teams use this for small evaluation sets where t-test assumptions are shaky.