Wilcoxon signed-rank test: one-sample example and formula
The 1-sample Wilcoxon signed-rank test checks whether the median of a symmetric distribution differs from a stated value. It ranks the distances of the observations from that value and adds up the ranks of the values above it. It uses more information than the sign test, because a value far above the hypothesized median counts more than one just above it, yet a single outlier can only get the top rank, so it cannot dominate the result the way it can dominate a mean.
When to use it
- You want to test a center against a target, the sample is small, and the data have outliers or heavier tails than the normal.
- The data are roughly symmetric, but you do not trust the normality assumption of the 1-sample t-test.
- You have paired data (before/after, two methods) and want a robust alternative to the paired t-test: apply the test to the differences.
- If the data are clearly skewed, the symmetry assumption fails; use the sign test instead.
How it works
Let η be the population median and η₀ the stated value.
- H₀: η = η₀.
- H₁: η ≠ η₀, or η > η₀, or η < η₀.
Steps:
- Compute dᵢ = xᵢ − η₀ and drop any dᵢ equal to zero. Let n be the number left.
- Rank the absolute values |dᵢ| from 1 (smallest) to n. Tied absolute values get the average of their ranks.
- W₊ is the sum of the ranks of the positive dᵢ; W₋ the sum for the negative ones. W₊ + W₋ = n(n + 1)/2.
Under H₀, each rank is equally likely to carry a plus or a minus sign. For small n without ties, the exact distribution of W₊ comes from listing all 2ⁿ sign patterns, and this article uses that exact p-value. For larger n or with ties, the normal approximation is used:
z = (W₊ − n(n + 1)/4) / √( n(n + 1)(2n + 1)/24 )with a continuity correction of 0.5 toward the mean and, with ties, a smaller variance.
Estimate and interval. The matching point estimate is the Hodges–Lehmann estimator: the median of all n(n + 1)/2 Walsh averages (xᵢ + xⱼ)/2 for i ≤ j. The confidence interval runs between two of the sorted Walsh averages, chosen from the exact distribution of W₊.
Assumptions: independent observations from a continuous distribution that is symmetric around its median. Symmetry makes the median equal to the mean, so the test speaks to both.
Example
A detergent plant installs a new servo-piston filler for 1,000 mL bottles, set to fill 1,005 mL. During qualification the engineer weighs 12 bottles and converts each to volume, in mL:
| Volume (mL) | |||||
|---|---|---|---|---|---|
| 1006.8 | 1004.1 | 1007.9 | 1005.6 | 1019.6 | 1006.2 |
| 1003.7 | 1008.5 | 1006.9 | 1009.3 | 1004.6 | 1007.3 |
Bottle 5 (1,019.6 mL) is far from the rest. Test H₀: η = 1005 against H₁: η ≠ 1005 at α = 0.05.
| Bottle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| d = x − 1005 | 1.8 | −0.9 | 2.9 | 0.6 | 14.6 | 1.2 | −1.3 | 3.5 | 1.9 | 4.3 | −0.4 | 2.3 |
| Rank of abs(d) | 6 | 3 | 9 | 2 | 12 | 4 | 5 | 10 | 7 | 11 | 1 | 8 |
- W₋ = 3 + 5 + 1 = 9, so W₊ = 78 − 9 = 69, with n = 12.
- Exact p-value: P(W₊ ≥ 69) = 0.0081, so the two-sided p-value is 0.016. (The normal approximation gives z = 2.31 and p = 0.021.)
- Hodges–Lehmann estimate of the median: 1006.78 mL.
- Exact confidence interval: the 14th smallest to the 14th largest of the 78 Walsh averages, 1005.40 to 1008.90 mL, with achieved confidence 95.8%.
For comparison, the 1-sample t-test gives t = 2.11 with p = 0.058. Bottle 5 raises the standard deviation from 1.8 mL (without it) to 4.2 mL, which widens the t interval to 1004.9 to 1010.2 mL. The sign test (9 above, 3 below) gives p = 0.146, because it ignores how far each bottle is from 1,005.
How to read the results
- The p-value of 0.016 says the filler's median volume is not 1,005 mL. The best estimate is about 1,006.8 mL, so it fills almost 2 mL more than its setting.
- The interval of 1,005.4 to 1,008.9 mL says the overfill is between about 0.4 and 3.9 mL per bottle. At high volume, even 2 mL per bottle is a real cost in product given away; a setting adjustment of about −1.8 mL is the obvious next step.
- Bottle 5 needs its own investigation. A 14.6 mL overfill on one bottle looks like a double stroke or a sensor fault, not ordinary variation. The Wilcoxon test kept it from distorting the estimate of the typical fill, but it does not make it go away.
- Check symmetry. Apart from bottle 5, the deviations are reasonably balanced around their center. If they were clearly skewed, the sign test would be the safer choice.
Common mistakes
- Using it on skewed data. The test assumes symmetry; with a long tail on one side it can reject H₀ even when the median is exactly η₀.
- Forgetting to drop zero differences. Values equal to η₀ carry no sign and must be removed before ranking.
- Treating it as a test of the mean for skewed data. Only under symmetry do mean and median coincide.
- Ignoring the outlier because the test handled it. A robust test protects the conclusion about the typical value; the outlier is still a process signal.
Frequently asked questions
When should I use the Wilcoxon test instead of the t-test?
When the sample is small (under about 20) and the data have outliers or heavy tails but are roughly symmetric. With normal data, the Wilcoxon test is nearly as powerful as the t-test (about 95% efficiency), so little is lost by using it.
What is the Hodges–Lehmann estimate?
The median of all pairwise averages (Walsh averages) of the data, including each value with itself. It is the point estimate that matches the Wilcoxon test and is much less sensitive to outliers than the mean.
How are ties handled?
Tied absolute differences get the average of the ranks they span, and the variance in the normal approximation is reduced to account for them. With many ties the exact p-value is no longer available from the standard tables, so the normal approximation is used.
Is there a 2-sample version?
Yes: the Mann–Whitney test, also called the Wilcoxon rank-sum test, compares two independent groups.