Method · Statistics Library

Wilcoxon signed-rank test

The Wilcoxon signed-rank test checks whether the median of paired differences (or one sample against a hypothesized median) is zero, using the ranks of the differences rather than their raw values, the nonparametric alternative to the paired t-test.

Topic indexA reference reading outside the guided chapter sequence

Understand the idea

This test handles the same situation as the paired t-test (before/after measurements on the same subjects, or one sample compared to a target value), but without assuming the differences follow a bell curve. It works by taking every within-pair difference, ranking them by absolute size, then attaching each rank's original sign back on and summing the positive-ranked and negative-ranked totals separately. If positive and negative differences are roughly balanced in both count and magnitude, nothing has really shifted; if one side clearly dominates, that's the signal of a real change.

A difference of exactly zero (no change at all for that pair) contributes NOTHING to the test and is simply dropped before ranking: there's no direction to rank if nothing changed.

There is a genuinely important subtlety here worth knowing: there are actually multiple different mathematically valid ways to turn the test statistic into a p-value, and they do not always agree, especially with small samples that have tied values. The card always names exactly which route it used, because comparing your result against another tool (like R) that might have picked a different route can otherwise look like a disagreement about the DATA when it's really just a difference of METHOD.

The Wilcoxon signed-rank test is the standard nonparametric paired test. The effect size for the Wilcoxon signed-rank test is the matched-pairs rank-biserial correlation, which runs from -1 to 1.

Practitioner explanation

KernelStats computes W = min(W+, W-) (the sums of ranks of positive and negative differences) via scipy.stats.wilcoxon, with rank-biserial correlation r_rb = (W+ - W-)/(W+ + W-) as the effect size. THREE distinct p-value computation routes exist and the card EXPLICITLY NAMES which one it used (in the detail line, the APA sentence, and statistics.method): exact signed-rank distribution (no tied |differences|, none exactly zero; the full null distribution is enumerated, giving an exact p-value), exact sign-flip permutation (ties or zeros present AND n<=13; every one of the 2^n possible sign assignments to the observed |differences| is enumerated, still exact but now conditional on the observed magnitudes), or normal approximation (larger tied samples, or n>50; W standardized using a tie-corrected standard error, with explicitly NO continuity correction applied).

The product states directly and specifically how this can create a cross-tool discrepancy: R's wilcox.test has NO permutation route at all; when ties appear, it warns 'cannot compute exact p-value with ties' and falls back to the normal approximation WITH a continuity correction, which on a small tied sample can differ from this card's p-value by roughly a factor of two, enough to move a borderline result across the .05 threshold. This is stated explicitly as a difference of METHOD, not an error in either program, and the R code export is deliberately PINNED to reproduce THIS card's chosen route (not R's own default), specifically so the exported script's numbers match the card rather than silently diverging from it.

A specific guard exists for a genuinely easy-to-make Inspector mistake: selecting the identical column for both paired variables is refused by name (it would otherwise make every difference exactly zero, both a raw polars error internally and a statistically meaningless test), and a case where ALL paired differences are exactly zero is refused with a clear message ('nothing to test') rather than reporting a fabricated result.

When to use it

Consider it when

Two paired (matched or repeated) measurements per subject, or one sample against a hypothesized median, when the differences are not normally distributed, are ordinal, or contain outliers.

Use caution when

The paired differences are roughly normal (the paired t-test is more powerful), the two groups being compared are independent rather than paired (use Mann-Whitney U), or every single paired difference is exactly zero (there is nothing to test, and the card refuses rather than fabricating a result).

Formulas and symbols

Wilcoxon signed-rank statistic

W=min⁡(W+,W−)W = \min(W^+, W^-)

W+ and W- the sums of ranks of the positive and negative paired differences (or differences from mu0 in the one-sample case). scipy reports the smaller of the two.

Rank-biserial correlation

rrb=W+−W−W++W−r_{rb} = \dfrac{W^+ - W^-}{W^+ + W^-}

Ranges -1 to +1; larger magnitude means the differences point more consistently in one direction.

Assumptions and checks

  • The Wilcoxon signed-rank test is only valid for paired measurements, where each value in one column corresponds to exactly one value in the other (e.g. before/after on the same subject); applying it to unrelated groups produces a meaningless result.

    In KernelStats: Not checked by KernelStats as a statistical test: assumed from the design; the card states this directly.

  • The signed-rank test's usual interpretation (a test about the median of the paired differences) assumes those differences are symmetric about their median; markedly asymmetric differences make the median interpretation less clean, though the test remains a valid test of whether the differences tend toward one side.

    In KernelStats: Not checked by KernelStats: assumed; stated directly as a standing reminder.

  • Normality (of residuals, not of the raw data). If the paired differences are actually close to normal, a paired t-test would be more powerful than the rank-based Wilcoxon test for the same data; conversely, strongly non-normal differences (often exactly why Wilcoxon was chosen) are precisely what the rank-based approach protects against.

    In KernelStats: Shapiro-Wilk on the paired differences, reported directly on the card using the same severity thresholds as the parametric tests (ok >= .05, warn >= .01, fail below), so a significant normality rejection is reported honestly as such rather than marked 'passed' just because a nonparametric test was chosen.

Alternatives

  • Paired t-Test. The paired differences are roughly normally distributed, where the parametric paired t-test is more powerful.
  • Mann-Whitney U Test. The two groups being compared are independent (unpaired) rather than the same subjects measured twice.

Worked example

Scenario

Pain scores measured before and after treatment on 18 patients, with 2 zero differences and several tied difference magnitudes among the remaining 16.

Result

16 nonzero differences with ties present; since ties are present and n exceeds 13, the normal-approximation route is used (no continuity correction); W = min(W+, W-) = 24, so W+ = 136 - 24 = 112 and p = .018; rank-biserial r = (112 - 24)/136 = 0.65 (large).

Interpretation

Pain scores decreased significantly between the two measurements, with a large and consistent effect across most patients (attributing the drop to the treatment needs a control group); the card's explicit statement of which p-value route it used (the normal approximation, since the tied sample exceeds the n<=13 threshold for the exact permutation route) means the result can be reproduced exactly by the accompanying code export.

Common misconceptions

Misconception

The Wilcoxon signed-rank test compares the medians of the two original measurements directly.

Correction

The test's null hypothesis is that the median of the WITHIN-PAIR DIFFERENCES is zero: it works entirely on the differences, not on comparing the two original distributions' medians as separate quantities. This distinction matters because the test also assumes the differences are roughly SYMMETRIC around their median for the result to translate cleanly into a statement about a location shift.

Read more: Misconception: the Mann-Whitney/Wilcoxon test always compares medians

Misconception

If this card's p-value doesn't match what R's wilcox.test reports on the identical data, one of the two programs has a bug.

Correction

With tied or zero differences, R's wilcox.test falls back to a normal approximation WITH a continuity correction (it has no exact permutation route at all), while this card may use an exact sign-flip permutation route instead, a genuine, stated difference of METHOD that can move a borderline p-value by roughly a factor of two, not a computational error in either tool. The card always names which route it used specifically so this is never a silent surprise.

References

  1. Individual comparisons by ranking methods, Wilcoxon, F. (1945), Biometrics Bulletin, 1(6), 80-83.
  2. NIST/SEMATECH e-Handbook of Statistical Methods, §1.3.5.14 Wilcoxon signed rank test, NIST/SEMATECH (2012), §1.3.5.14.