The Wilcoxon signed-rank test is the rank-based partner of the paired t-test. It compares two measurements taken on the same units, such as a wafer before and after a cleaning step, by looking at the within-pair differences. It uses both the direction of each change and the rank of its size, so it is more informative than a plain sign test and less sensitive to a wild difference than the t-test.
The procedure:
The statistic. Let $d_i$ be the difference for pair $i$ and discard pairs with $d_i=0$, leaving $n$ pairs. Rank the absolute values $|d_i|$ from $1$ to $n$, then add the ranks belonging to positive differences and to negative differences separately. The two sums always total $n(n+1)/2$, and the test statistic is the smaller of them:
If the typical change is zero and the differences are symmetric around zero, each rank is equally likely to carry a plus or a minus sign, giving $2^n$ equally likely sign patterns. The p-value is the share of those patterns at least as lopsided as the one observed.
Worked example. Eight wafers show these after-minus-before changes in film thickness: $-4.2,\,-1.1,\,-5.6,\,-2.4,\,+0.8,\,-3.7,\,-6.3,\,-3.1$. The absolute sizes rank as $6,\,2,\,7,\,3,\,1,\,5,\,8,\,4$. Only one change is positive, $+0.8$, which carries the smallest rank, so $W^+=1$ and $W^-=35$.
For $n=8$ there are $2^8=256$ sign patterns. Exactly $2$ of them give $W^+\le1$ (rank sums of $0$ and $1$), and by symmetry $2$ give $W^+\ge35$, so the exact two-sided p-value is $4/256=0.0156$. This matches scipy. For comparison, the paired t-test on the same data gives $t=-3.90$ and $p=0.0059$.
Where the rank test earns its keep. Replace the $-6.3$ change with an extreme $-26.3$. It is still the largest drop, so its rank is unchanged and the Wilcoxon p-value stays at $0.0156$. The mean change and its standard deviation both shift, and the paired t-test falls to $p=0.101$, no longer significant. A single wild difference can hide the pattern from the t-test but not from the ranks.
Large samples and effect size. For larger $n$ the statistic is approximately normal, with mean and standard deviation:
Here $\mu_W=18$ and $\sigma_W\approx7.14$, so $z\approx-2.38$ and $p\approx0.0173$, a little above the exact $0.0156$. To report a size in the original units, use the Hodges-Lehmann estimate: the median of all $n(n+1)/2=36$ pairwise averages of the differences, which is $-3.3$ for this data.
Things to check.
- Symmetry: the test assumes the distribution of differences is symmetric around its centre under the null. For badly skewed differences, the sign test is safer, though weaker.
- Compare with the sign test: the sign test uses only direction. Seven drops out of eight gives an exact $p=0.0703$, so the rank test is stronger by using the sizes of the changes.
- Zeros and ties: zero differences are dropped, and tied $|d_i|$ share average ranks. Exact p-values then need a correction, and software may switch to the normal approximation.
- Pairing must be real: use the test only when each before value is matched to the right after value. For two independent groups use Mann-Whitney U instead.
- Interpretation: it tests whether the typical change is zero. It does not say the change matters in practice, so report the estimated shift alongside the p-value.
Read the Wilcoxon signed-rank test as a ranked tally of ups and downs: if the big changes all go one way, the sum of ranks on one side collapses, and shuffled signs rarely produce that.