A permutation test compares two samples by reshuffling the group labels — distribution-free, and exact when randomisation was part of the design.
Under the strong null (the two groups are exchangeable — the labels are random), any permutation of the group labels gives an equally likely test statistic. The permutation p-value is the proportion of permutations where the recomputed statistic is at least as extreme as the observed.
For two independent samples, permute the group labels. For paired data, randomly flip the signs of the within-pair differences. With enough permutations (R = 5000+), the p-value converges to the exact randomisation p; with all permutations enumerated (computationally feasible only for small n), the test is exact.
The permutation distribution requires the *exchangeability* assumption: under H₀, all permutations of labels are equally likely. For randomised experiments this is by design. For observational data, exchangeability is more questionable and the test is approximate.
Two numeric columns + test statistic + R + alternative + paired flag.
Observed statistic, (count + 1)/(R + 1) p-value (continuity-corrected so p > 0 even with R draws all less extreme).
Permutation p ≈ analytic p when the analytic test's assumptions hold; useful as a sanity check on the t-test in borderline-non-normal data.