# Wilcoxon signed-rank test

Short description: Statistical hypothesis test

The Wilcoxon signed-rank test is a non-parametric statistical hypothesis test used either to test the location of a population based on a sample of data, or to compare the locations of two populations using two matched samples.[1] The one-sample version serves a purpose similar to that of the one-sample Student's t-test.[2] For two matched samples, it is a paired difference test like the paired Student's t-test (also known as the "t-test for matched pairs" or "t-test for dependent samples"). The Wilcoxon test can be a good alternative to the t-test when population means are not of interest; for example, when one wishes to test whether a population's median is nonzero, or whether there is a better than 50% chance that a sample from one population is greater than a sample from another population.

## History

The test is named for Frank Wilcoxon (1892–1965) who, in a single paper, proposed both it and the rank-sum test for two independent samples.[3] The test was popularized by Sidney Siegel (1956) in his influential textbook on non-parametric statistics.[4] Siegel used the symbol T for the test statistic, and consequently, the test is sometimes referred to as the Wilcoxon T-test.

## Test procedure

There are two variants of the signed-rank test. From a theoretical point of view, the one-sample test is more fundamental because the paired sample test is performed by converting the data to the situation of the one-sample test. However, most practical applications of the signed-rank test arise from paired data.

For a paired sample test, the data consists of samples $\displaystyle{ (X_1, Y_1), \dots, (X_n, Y_n) }$. Each sample is a pair of measurements. In the simplest case, the measurements are on an interval scale. Then they may be converted to real numbers, and the paired sample test is converted to a one-sample test by replacing each pair of numbers $\displaystyle{ (X_i, Y_i) }$ by its difference $\displaystyle{ X_i - Y_i }$.[5] In general, it must be possible to rank the differences between the pairs. This requires that the data be on an ordered metric scale, a type of scale that carries more information than an ordinal scale but may have less than an interval scale.[6]

The data for a one-sample test is a set of real number samples $\displaystyle{ X_1, \dots, X_n }$. Assume for simplicity that the samples have distinct absolute values and that no sample equals zero. (Zeros and ties introduce several complications; see below.) The test is performed as follows:[7][8]

1. Compute $\displaystyle{ |X_1|, \dots, |X_n| }$.
2. Sort $\displaystyle{ |X_1|, \dots, |X_n| }$, and use this sorted list to assign ranks $\displaystyle{ R_1, \dots, R_n }$: The rank of the smallest observation is one, the rank of the next smallest is two, and so on.
3. Let $\displaystyle{ \sgn }$ denote the sign function: $\displaystyle{ \sgn(x) = 1 }$ if $\displaystyle{ x \gt 0 }$ and $\displaystyle{ \sgn(x) = -1 }$ if $\displaystyle{ x \lt 0 }$. The test statistic is the signed-rank sum $\displaystyle{ T }$: $\displaystyle{ T = \sum_{i=1}^N \sgn(X_i)R_i. }$
4. Produce a $\displaystyle{ p }$-value by comparing $\displaystyle{ T }$ to its distribution under the null hypothesis.

The ranks are defined so that $\displaystyle{ R_i }$ is the number of $\displaystyle{ j }$ for which $\displaystyle{ |X_j| \le |X_i| }$. Additionally, if $\displaystyle{ \sigma \colon \{1, \dots, n\} \to \{1, \dots, n\} }$ is such that $\displaystyle{ |X_{\sigma(1)}| \lt \dots \lt |X_{\sigma(n)}| }$, then $\displaystyle{ R_{\sigma(i)} = i }$ for all $\displaystyle{ i }$.

The signed-rank sum $\displaystyle{ T }$ is closely related to two other test statistics. The positive-rank sum $\displaystyle{ T^+ }$ and the negative-rank sum $\displaystyle{ T^- }$ are defined by[9] \displaystyle{ \begin{align} T^+ &= \sum_{1 \le i \le n,\ X_i \gt 0} R_i, \\ T^- &= \sum_{1 \le i \le n,\ X_i \lt 0} R_i. \end{align} } Because $\displaystyle{ T^+ + T^- }$ equals the sum of all the ranks, which is $\displaystyle{ 1 + 2 + \dots + n = n(n + 1)/2 }$, these three statistics are related by:[10] \displaystyle{ \begin{align} T^+ &= \frac{n(n + 1)}{2} - T^- = \frac{n(n + 1)}{4} + \frac{T}{2}, \\ T^- &= \frac{n(n + 1)}{2} - T^+ = \frac{n(n + 1)}{4} - \frac{T}{2}, \\ T &= T^+ - T^- = 2T^+ - \frac{n(n + 1)}{2} = \frac{n(n + 1)}{2} - 2T^-. \end{align} } Because $\displaystyle{ T }$, $\displaystyle{ T^+ }$, and $\displaystyle{ T^- }$ carry the same information, any of them may be used as the test statistic.

The positive-rank sum and negative-rank sum have alternative interpretations that are useful for the theory behind the test. Define the Walsh average $\displaystyle{ W_{ij} }$ to be $\displaystyle{ \tfrac12(X_i + X_j) }$. Then:[11] \displaystyle{ \begin{align} T^+ = \#\{W_{ij} \gt 0 \colon 1 \le i \le j \le n\}, \\ T^- = \#\{W_{ij} \lt 0 \colon 1 \le i \le j \le n\}. \end{align} }

## Null and alternative hypotheses

### One-sample test

The one-sample Wilcoxon signed-rank test can be used to test whether data comes from a symmetric population with a specified median.[12] If the population median is known, then it can be used to test whether data is symmetric about its center.[13]

To explain the null and alternative hypotheses formally, assume that the data consists of independent and identically distributed samples from a distribution $\displaystyle{ F }$. If $\displaystyle{ X_1 }$ and $\displaystyle{ X_2 }$ are IID $\displaystyle{ F }$-distributed random variables, define $\displaystyle{ F^{(2)} }$ to be the cumulative distribution function of $\displaystyle{ \tfrac12(X_1 + X_2) }$. Set $\displaystyle{ p_2 = \Pr(\tfrac12(X_1 + X_2) \gt 0) = 1 - F^{(2)}(0). }$ Assume that $\displaystyle{ F }$ is continuous. The one-sample Wilcoxon signed-rank sum test is a test for the following null hypothesis against one of the following alternative hypotheses:[14]

Null hypothesis H0
$\displaystyle{ p_2 = \tfrac12 }$
One-sided alternative hypothesis H1
$\displaystyle{ p_2 \gt \tfrac12 }$.
One-sided alternative hypothesis H2
$\displaystyle{ p_2 \lt \tfrac12 }$.
Two-sided alternative hypothesis H3
$\displaystyle{ p_2 \neq \tfrac12 }$.

The alternative hypothesis being tested depends on whether the test statistic is used to compute a one-sided or two-sided p-value (and if one-sided, which side). If $\displaystyle{ \mu }$ is a fixed, predetermined quantity, then the test can also be used as a test for the value of $\displaystyle{ \Pr(\tfrac12(X_1 + X_2) \gt \mu) }$ by subtracting $\displaystyle{ \mu }$ from every data point.

The above null and alternative hypotheses are derived from the fact that $\displaystyle{ 2T^+ / n^2 }$ is a consistent estimator of $\displaystyle{ p_2 }$.[15] It can also be derived from the description of $\displaystyle{ T^+ }$ and $\displaystyle{ T^- }$ in terms of Walsh averages, since that description shows that the Wilcoxon test is the same as the sign test applied to the set of Walsh averages.[16]

Restricting the distributions of interest can lead to more interpretable null and alternative hypotheses. One mildly restrictive assumption is that $\displaystyle{ F^{(2)} }$ has a unique median. This median is called the pseudomedian of $\displaystyle{ F }$; in general it is different from the mean and the median, even when all three exist. If the existence of a unique pseudomedian can be assumed true under both the null and alternative hypotheses, then these hypotheses can be restated as:

Null hypothesis H0
The pseudomedian of $\displaystyle{ F }$ is located at zero.
One-sided alternative hypothesis H1
The pseudomedian of $\displaystyle{ F }$ is located at $\displaystyle{ \mu \lt 0 }$.
One-sided alternative hypothesis H2
The pseudomedian of $\displaystyle{ F }$ is located at $\displaystyle{ \mu \gt 0 }$.
Two-sided alternative hypothesis H3
The pseudomedian of $\displaystyle{ F }$ is located at $\displaystyle{ \mu \neq 0 }$.

Most often, the null and alternative hypotheses are stated under the assumption of symmetry. Fix a real number $\displaystyle{ \mu }$. Define $\displaystyle{ F }$ to be symmetric about $\displaystyle{ \mu }$ if a random variable $\displaystyle{ X }$ with distribution $\displaystyle{ F }$ satisfies $\displaystyle{ \Pr(X \le \mu - x) = \Pr(X \ge \mu + x) }$ for all $\displaystyle{ x }$. If $\displaystyle{ F }$ has a density function $\displaystyle{ f }$, then $\displaystyle{ F }$ is symmetric about $\displaystyle{ \mu }$ if and only if $\displaystyle{ f(\mu + x) = f(\mu - x) }$ for every $\displaystyle{ x }$.[17]

If the null and alternative distributions of $\displaystyle{ F }$ can be assumed symmetric, then the null and alternative hypotheses simplify to the following:[18]

Null hypothesis H0
$\displaystyle{ F }$ is symmetric about $\displaystyle{ \mu = 0 }$.
One-sided alternative hypothesis H1
$\displaystyle{ F }$ is symmetric about $\displaystyle{ \mu \lt 0 }$.
One-sided alternative hypothesis H2
$\displaystyle{ F }$ is symmetric about $\displaystyle{ \mu \gt 0 }$.
Two-sided alternative hypothesis H3
$\displaystyle{ F }$ is symmetric about $\displaystyle{ \mu \neq 0 }$.

If in addition $\displaystyle{ \Pr(X = \mu) = 0 }$, then $\displaystyle{ \mu }$ is a median of $\displaystyle{ F }$. If this median is unique, then the Wilcoxon signed-rank sum test becomes a test for the location of the median.[19] When the mean of $\displaystyle{ F }$ is defined, then the mean is $\displaystyle{ \mu }$, and the test is also a test for the location of the mean.[20]

The restriction that the alternative distribution is symmetric is highly restrictive, but for one-sided tests it can be weakened. Say that $\displaystyle{ F }$ is stochastically smaller than a distribution symmetric about zero if an $\displaystyle{ F }$-distributed random variable $\displaystyle{ X }$ satisfies $\displaystyle{ \Pr(X \lt -x) \ge \Pr(X \gt x) }$ for all $\displaystyle{ x \ge 0 }$. Similarly, $\displaystyle{ F }$ is stochastically larger than a distribution symmetric about zero if $\displaystyle{ \Pr(X \lt -x) \le \Pr(X \gt x) }$ for all $\displaystyle{ x \ge 0 }$. Then the Wilcoxon signed-rank sum test can also be used for the following null and alternative hypotheses:[21][22]

Null hypothesis H0
$\displaystyle{ F }$ is symmetric about $\displaystyle{ \mu = 0 }$.
One-sided alternative hypothesis H1
$\displaystyle{ F }$ is stochastically smaller than a distribution symmetric about zero.
One-sided alternative hypothesis H2
$\displaystyle{ F }$ is stochastically larger than a distribution symmetric about zero.

The hypothesis that the data are IID can be weakened. Each data point may be taken from a different distribution, as long as all the distributions are assumed to be continuous and symmetric about a common point $\displaystyle{ \mu_0 }$. The data points are not required to be independent as long as the conditional distribution of each observation given the others is symmetric about $\displaystyle{ \mu_0 }$.[23]

### Paired data test

Because the paired data test arises from taking paired differences, its null and alternative hypotheses can be derived from those of the one-sample test. In each case, they become assertions about the behavior of the differences $\displaystyle{ X_i - Y_i }$.

Let $\displaystyle{ F(x, y) }$ be the joint cumulative distribution of the pairs $\displaystyle{ (X_i, Y_i) }$. If $\displaystyle{ F }$ is continuous, then the most general null and alternative hypotheses are expressed in terms of $\displaystyle{ p_2 = \Pr(\tfrac12(X_i - Y_i + X_j - Y_j) \gt 0) }$ and are identical to the one-sample case:

Null hypothesis H0
$\displaystyle{ p_2 = \tfrac12 }$
One-sided alternative hypothesis H1
$\displaystyle{ p_2 \gt \tfrac12 }$.
One-sided alternative hypothesis H2
$\displaystyle{ p_2 \lt \tfrac12 }$.
Two-sided alternative hypothesis H3
$\displaystyle{ p_2 \neq \tfrac12 }$.

Like the one-sample case, under some restrictions the test can be interpreted as a test for whether the pseudomedian of the differences is located at zero.

A common restriction is to symmetric distributions of differences. In this case, the null and alternative hypotheses are:[24][25]

Null hypothesis H0
The observations $\displaystyle{ X_i - Y_i }$ are symmetric about $\displaystyle{ \mu = 0 }$.
One-sided alternative hypothesis H1
The observations $\displaystyle{ X_i - Y_i }$ are symmetric about $\displaystyle{ \mu \lt 0 }$.
One-sided alternative hypothesis H2
The observations $\displaystyle{ X_i - Y_i }$ are symmetric about $\displaystyle{ \mu \gt 0 }$.
Two-sided alternative hypothesis H3
The observations $\displaystyle{ X_i - Y_i }$ are symmetric about $\displaystyle{ \mu \neq 0 }$.

These can also be expressed more directly in terms of the original pairs:[26]

Null hypothesis H0
The observations $\displaystyle{ (X_i, Y_i) }$ are exchangeable, meaning that $\displaystyle{ (X_i, Y_i) }$ and $\displaystyle{ (Y_i, X_i) }$ have the same distribution. Equivalently, $\displaystyle{ F(x, y) = F(y, x) }$.
One-sided alternative hypothesis H1
For some $\displaystyle{ \mu \lt 0 }$, the pairs $\displaystyle{ (X_i, Y_i) }$ and $\displaystyle{ (Y_i + \mu, X_i - \mu) }$ have the same distribution.
One-sided alternative hypothesis H2
For some $\displaystyle{ \mu \gt 0 }$, the pairs $\displaystyle{ (X_i, Y_i) }$ and $\displaystyle{ (Y_i + \mu, X_i - \mu) }$ have the same distribution.
Two-sided alternative hypothesis H3
For some $\displaystyle{ \mu \neq 0 }$, the pairs $\displaystyle{ (X_i, Y_i) }$ and $\displaystyle{ (Y_i + \mu, X_i - \mu) }$ have the same distribution.

The null hypothesis of exchangeability can arise from a matched pair experiment with a treatment group and a control group. Randomizing the treatment and control within each pair makes the observations exchangeable. For an exchangeable distribution, $\displaystyle{ X_i - Y_i }$ has the same distribution as $\displaystyle{ Y_i - X_i }$, and therefore, under the null hypothesis, the distribution is symmetric about zero.[27]

Because the one-sample test can be used as a one-sided test for stochastic dominance, the paired difference Wilcoxon test can be used to compare the following hypotheses:[28]

Null hypothesis H0
The observations $\displaystyle{ (X_i, Y_i) }$ are exchangeable.
One-sided alternative hypothesis H1
The differences $\displaystyle{ X_i - Y_i }$ are stochastically smaller than a distribution symmetric about zero, that is, for every $\displaystyle{ x \ge 0 }$, $\displaystyle{ Pr(X_i \lt Y_i - x) \ge \Pr(X_i \gt Y_i + x) }$.
One-sided alternative hypothesis H2
The differences $\displaystyle{ X_i - Y_i }$ are stochastically larger than a distribution symmetric about zero, that is, for every $\displaystyle{ x \ge 0 }$, $\displaystyle{ Pr(X_i \lt Y_i - x) \le \Pr(X_i \gt Y_i + x) }$.

## Zeros and ties

In real data, it sometimes happens that there is a sample $\displaystyle{ X_i }$ which equals zero or a pair $\displaystyle{ (X_i, Y_i) }$ with $\displaystyle{ X_i = Y_i }$. It can also happen that there are tied samples. This means that for some $\displaystyle{ i \neq j }$, we have $\displaystyle{ X_i = X_j }$ (in the one-sample case) or $\displaystyle{ X_i - Y_i = X_j - Y_j }$ (in the paired sample case). This is particularly common for discrete data. When this happens, the test procedure defined above is usually undefined because there is no way to uniquely rank the data. (The sole exception is if there is a single sample $\displaystyle{ X_i }$ which is zero and no other zeros or ties.) Because of this, the test statistic needs to be modified.

### Zeros

Wilcoxon's original paper did not address the question of observations (or, in the paired sample case, differences) that equal zero. However, in later surveys, he recommended removing zeros from the sample.[29] Then the standard signed-rank test could be applied to the resulting data, as long as there were no ties. This is now called the reduced sample procedure.

Pratt[30] observed that the reduced sample procedure can lead to paradoxical behavior. He gives the following example. Suppose that we are in the one-sample situation and have the following thirteen observations:

0, 2, 3, 4, 6, 7, 8, 9, 11, 14, 15, 17, −18.

The reduced sample procedure removes the zero. To the remaining data, it assigns the signed ranks:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, −12.

This has a one-sided p-value of $\displaystyle{ 55/2^{12} }$, and therefore the sample is not significantly positive at any significance level $\displaystyle{ \alpha \lt 55/2^{12} \approx 0.0134 }$. Pratt argues that one would expect that decreasing the observations should certainly not make the data appear more positive. However, if the zero observation is decreased by an amount less than 2, or if all observations are decreased by an amount less than 1, then the signed ranks become:

−1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, −13.

This has a one-sided p-value of $\displaystyle{ 109/2^{13} }$. Therefore the sample would be judged significantly positive at any significance level $\displaystyle{ \alpha \gt 109/2^{13} \approx 0.0133 }$. The paradox is that, if $\displaystyle{ \alpha }$ is between $\displaystyle{ 109/2^{13} }$ and $\displaystyle{ 55/2^{12} }$, then decreasing an insignificant sample causes it to appear significantly positive.

Pratt therefore proposed the signed-rank zero procedure. This procedure includes the zeros when ranking the samples. However, it excludes them from the test statistic, or equivalently it defines $\displaystyle{ \sgn(0) = 0 }$. Pratt proved that the signed-rank zero procedure has several desirable behaviors not shared by the reduced sample procedure:[31]

1. Increasing the observed values does not make a significantly positive sample insignificant, and it does not make an insignificant sample significantly negative.
2. If the distribution of the observations is symmetric, then the values of $\displaystyle{ \mu }$ which the test does not reject form an interval.
3. A sample is significantly positive, not significant, or significantly negative, if and only if it is so when the zeros are assigned arbitrary non-zero signs, if and only if it is so when the zeros are replaced with non-zero values which are smaller in absolute value than any non-zero observation.
4. For a fixed significance threshold $\displaystyle{ \alpha }$, and for a test which is randomized to have level exactly $\displaystyle{ \alpha }$, the probability of calling a set of observations significantly positive (respectively, significantly negative) is a non-decreasing (respectively, non-increasing) function of the observations.

Pratt remarks that, when the signed-rank zero procedure is combined with the average rank procedure for resolving ties, the resulting test is a consistent test against the alternative hypothesis that, for all $\displaystyle{ i \neq j }$, $\displaystyle{ \Pr(X_i + X_j \gt 0) }$ and $\displaystyle{ \Pr(X_i + X_j \lt 0) }$ differ by at least a fixed constant that is independent of $\displaystyle{ i }$ and $\displaystyle{ j }$.[32]

The signed-rank zero procedure has the disadvantage that, when zeros occur, the null distribution of the test statistic changes, so tables of p-values can no longer be used.

When the data is on a Likert scale with equally spaced categories, the signed-rank zero procedure is more likely to maintain the Type I error rate than the reduced sample procedure.[33]

From the viewpoint of statistical efficiency, there is no perfect rule for handling zeros. Conover found examples of null and alternative hypotheses that show that neither Wilcoxon's and Pratt's methods are uniformly better than the other. When comparing a discrete uniform distribution to a distribution where probabilities linearly increase from left to right, Pratt's method outperforms Wilcoxon's. When testing a binomial distribution centered at zero to see whether the parameter of each Bernoulli trial is $\displaystyle{ \tfrac12 }$, Wilcoxon's method outperforms Pratt's.[34]

### Ties

When the data does not have ties, the ranks $\displaystyle{ R_i }$ are used to calculate the test statistic. In the presence of ties, the ranks are not defined. There are two main approaches to resolving this.

The most common procedure for handling ties, and the one originally recommended by Wilcoxon, is called the average rank or midrank procedure. This procedure assigns numbers between 1 and n to the observations, with two observations getting the same number if and only if they have the same absolute value. These numbers are conventionally called ranks even though the set of these numbers is not equal to $\displaystyle{ \{1, \dots, n\} }$ (except when there are no ties). The rank assigned to an observation is the average of the possible ranks it would have if the ties were broken in all possible ways. Once the ranks are assigned, the test statistic is computed in the same way as usual.[35][36]

For example, suppose that the observations satisfy $\displaystyle{ |X_3|\lt |X_2|= |X_5|\lt |X_6|\lt |X_1|= |X_4|= |X_7|. }$ In this case, $\displaystyle{ X_3 }$ is assigned rank 1, $\displaystyle{ X_2 }$ and $\displaystyle{ X_5 }$ are assigned rank $\displaystyle{ (2 + 3) / 2 = 2.5 }$, $\displaystyle{ X_6 }$ is assigned rank 4, and $\displaystyle{ X_1 }$, $\displaystyle{ X_4 }$, and $\displaystyle{ X_7 }$ are assigned rank $\displaystyle{ (5 + 6 + 7) / 3 = 6 }$. Formally, suppose that there is a set of observations all having the same absolute value $\displaystyle{ v }$, that $\displaystyle{ k - 1 }$ observations have absolute value less than $\displaystyle{ v }$, and that $\displaystyle{ \ell }$ observations have absolute value less than or equal to $\displaystyle{ v }$. If the ties among the observations with absolute value $\displaystyle{ v }$ were broken, then these observations would occupy ranks $\displaystyle{ k }$ through $\displaystyle{ \ell }$. The average rank procedure therefore assigns them the rank $\displaystyle{ (k + \ell) / 2 }$.

Under the average rank procedure, the null distribution is different in the presence of ties.[37][38] The average rank procedure also has some disadvantages that are similar to those of the reduced sample procedure for zeros. It is possible that a sample can be judged significantly positive by the average rank procedure; but increasing some of the values so as to break the ties, or breaking the ties in any way whatsoever, results in a sample that the test judges to be not significant.[39][40] However, increasing all the observed values by the same amount cannot turn a significantly positive result into an insignificant one, nor an insignificant one into a significantly negative one. Furthermore, if the observations are distributed symmetrically, then the values of $\displaystyle{ \mu }$ which the test does not reject form an interval.[41][42]

The other common option for handling ties is a tiebreaking procedure. In a tiebreaking procedure, the observations are assigned distinct ranks in the set $\displaystyle{ \{1, \dots, n\} }$. The rank assigned to an observation depends on its absolute value and the tiebreaking rule. Observations with smaller absolute values are always given smaller ranks, just as in the standard rank-sum test. The tiebreaking rule is used to assign ranks to observations with the same absolute value. One advantage of tiebreaking rules is that they allow the use of standard tables for computing p-values.[43]

Random tiebreaking breaks the ties at random. Under random tiebreaking, the null distribution is the same as when there are no ties, but the result of the test depends not only on the data but on additional random choices. Averaging the ranks over the possible random choices results in the average rank procedure.[44] One could also report the probability of rejection over all random choices.[45] Random tiebreaking has the advantage that the probability that a sample is judged significantly positive does not decrease when some observations are increased.[46] Conservative tiebreaking breaks the ties in favor of the null hypothesis. When performing a one-sided test in which negative values of $\displaystyle{ T }$ tend to be more significant, ties are broken by assigning lower ranks to negative observations and higher ranks to positive ones. When the test makes positive values of $\displaystyle{ T }$ significant, ties are broken the other way, and when large absolute values of $\displaystyle{ T }$ are significant, ties are broken so as to make $\displaystyle{ |T| }$ as small as possible. Pratt observes that when ties are likely, the conservative tiebreaking procedure "presumably has low power, since it amounts to breaking all ties in favor of the null hypothesis."[47]

The average rank procedure can disagree with tiebreaking procedures. Pratt gives the following example.[48] Suppose that the observations are:

1, 1, 1, 1, 2, 3, −4.

The average rank procedure assigns these the signed ranks

2.5, 2.5, 2.5, 2.5, 5, 6, −7.

This sample is significantly positive at the one-sided level $\displaystyle{ \alpha = 14 / 2^7 }$. On the other hand, any tiebreaking rule will assign the ranks

1, 2, 3, 4, 5, 6, −7.

At the same one-sided level $\displaystyle{ \alpha = 14 / 2^7 }$, this is not significant.

Two other options for handling ties are based around averaging the results of tiebreaking. In the average statistic method, the test statistic $\displaystyle{ T }$ is computed for every possible way of breaking ties, and the final statistic is the mean of the tie-broken statistics. In the average probability method, the p-value is computed for every possible way of breaking ties, and the final p-value is the mean of the tie-broken p-values.[49]

## Computing the null distribution

Computing p-values requires knowing the distribution of $\displaystyle{ T }$ under the null hypothesis. There is no closed formula for this distribution.[50] However, for small values of $\displaystyle{ n }$, the distribution may be computed exactly. Under the null hypothesis that the data is symmetric about zero, each $\displaystyle{ X_i }$ is exactly as likely to be positive as it is negative. Therefore the probability that $\displaystyle{ T = t }$ under the null hypothesis is equal to the number of sign combinations that yield $\displaystyle{ T = t }$ divided by the number of possible sign combinations $\displaystyle{ 2^n }$. This can be used to compute the exact distribution of $\displaystyle{ T }$ under the null hypothesis.[51]

Computing the distribution of $\displaystyle{ T }$ by considering all possibilities requires computing $\displaystyle{ 2^n }$ sums, which is intractable for all but the smallest $\displaystyle{ n }$. However, there is an efficient recursion for the distribution of $\displaystyle{ T^+ }$.[52][53] Define $\displaystyle{ u_n(t^+) }$ to be the number of sign combinations for which $\displaystyle{ T^+ = t^+ }$. This is equal to the number of subsets of $\displaystyle{ \{1, \dots, n\} }$ which sum to $\displaystyle{ t^+ }$. The base cases of the recursion are $\displaystyle{ u_0(0) = 1 }$, $\displaystyle{ u_0(t^+) = 0 }$ for all $\displaystyle{ t^+ \neq 0 }$, and $\displaystyle{ u_n(t^+) = 0 }$ for all $\displaystyle{ t \lt 0 }$ or $\displaystyle{ t \gt n(n + 1)/2 }$. The recursive formula is $\displaystyle{ u_n(t^+) = u_{n - 1}(t^+) + u_{n - 1}(t^+ - n). }$ The formula is true because every subset of $\displaystyle{ \{1, \dots, n\} }$ which sums to $\displaystyle{ t^+ }$ either does not contain $\displaystyle{ n }$, in which case it is also a subset of $\displaystyle{ \{1, \dots, n - 1\} }$, or it does contain $\displaystyle{ n }$, in which case removing $\displaystyle{ n }$ from the subset produces a subset of $\displaystyle{ \{1, \dots, n - 1\} }$ which sums to $\displaystyle{ t^+ - n }$. Under the null hypothesis, the probability mass function of $\displaystyle{ T^+ }$ satisfies $\displaystyle{ \Pr(T^+ = t^+) = u_n(t^+) / 2^n }$. The function $\displaystyle{ u_n }$ is closely related to the integer partition function.[54]

If $\displaystyle{ p_n(t^+) }$ is the probability that $\displaystyle{ T^+ = t^+ }$ under the null hypothesis when there are $\displaystyle{ n }$ samples, then $\displaystyle{ p_n(t^+) }$ satisfies a similar recursion:[55] $\displaystyle{ 2p_n(t^+) = p_{n-1}(t^+) + p_{n-1}(t^+ - n) }$ with similar boundary conditions. There is also a recursive formula for the cumulative distribution function $\displaystyle{ \Pr(T^+ \le t^+) }$.[56]

For very large $\displaystyle{ n }$, even the above recursion is too slow. In this case, the null distribution can be approximated. The null distributions of $\displaystyle{ T }$, $\displaystyle{ T^+ }$, and $\displaystyle{ T^- }$ are asymptotically normal with means and variances:[57] \displaystyle{ \begin{align} \mathbf{E}[T^+] &= \mathbf{E}[T^-] = \frac{n(n + 1)}{4}, \\ \mathbf{E}[T] &= 0, \\ \operatorname{Var}(T^+) &= \operatorname{Var}(T^-) = \frac{n(n + 1)(2n + 1)}{24}, \\ \operatorname{Var}(T) &= \frac{n(n + 1)(2n + 1)}{6}. \end{align} }

Better approximations can be produced using Edgeworth expansions. Using a fourth-order Edgeworth expansion shows that:[58][59] $\displaystyle{ \Pr(T^+ \le k) \approx \Phi(t) + \phi(t)\Big(\frac{3n^2 + 3n - 1}{10n(n + 1)(2n + 1)}\Big)(t^3 - 3t), }$ where $\displaystyle{ t = \frac{k + \tfrac12 - \frac{n(n + 1)}{4}}{\sqrt{\frac{n(n + 1)(2n + 1)}{6}}}. }$ The technical underpinnings of these expansions are rather involved, because conventional Edgeworth expansions apply to sums of IID continuous random variables, while $\displaystyle{ T^+ }$ is a sum of non-identically distributed discrete random variables. The final result, however, is that the above expansion has an error of $\displaystyle{ O(n^{-3/2}) }$, just like a conventional fourth-order Edgeworth expansion.[58]

The moment generating function of $\displaystyle{ T }$ has the exact formula:[60] $\displaystyle{ M(t) = \frac{1}{2^n}\prod_{j=1}^n (1 + e^{jt}). }$

When zeros are present and the signed-rank zero procedure is used, or when ties are present and the average rank procedure is used, the null distribution of $\displaystyle{ T }$ changes. Cureton derived a normal approximation for this situation.[61][62] Suppose that the original number of observations was $\displaystyle{ n }$ and the number of zeros was $\displaystyle{ z }$. The tie correction is $\displaystyle{ c = \sum t^3 - t, }$ where the sum is over all the size $\displaystyle{ t }$ of each group of tied observations. The expectation of $\displaystyle{ T }$ is still zero, while the expectation of $\displaystyle{ T^+ }$ is $\displaystyle{ \mathbf{E}[T^+] = \frac{n(n + 1)}{4} - \frac{z(z + 1)}{4}. }$ If $\displaystyle{ \sigma^2 = \frac{n(n + 1)(2n + 1) - z(z + 1)(2z + 1) - c/2}{6}, }$ then \displaystyle{ \begin{align} \operatorname{Var}(T) &= \sigma^2, \\ \operatorname{Var}(T^+) &= \sigma^2 / 4. \end{align} }

## Alternative statistics

Wilcoxon[63] originally defined the Wilcoxon rank-sum statistic to be $\displaystyle{ \min(T^+, T^-) }$. Early authors such as Siegel[64] followed Wilcoxon. This is appropriate for two-sided hypothesis tests, but it cannot be used for one-sided tests.

Instead of assigning ranks between 1 and n, it is also possible to assign ranks between 0 and $\displaystyle{ n - 1 }$. These are called modified ranks.[65] The modified signed-rank sum $\displaystyle{ T_0 }$, the modified positive-rank sum $\displaystyle{ T_0^+ }$, and the modified negative-rank sum $\displaystyle{ T_0^- }$ are defined analogously to $\displaystyle{ T }$, $\displaystyle{ T^+ }$, and $\displaystyle{ T^- }$ but with the modified ranks in place of the ordinary ranks. The probability that the sum of two independent $\displaystyle{ F }$-distributed random variables is positive can be estimated as $\displaystyle{ 2T_0^+/(n(n - 1)) }$.[66] When consideration is restricted to continuous distributions, this is a minimum variance unbiased estimator of $\displaystyle{ p_2 }$.[67]

## Example

$\displaystyle{ i }$ $\displaystyle{ x_{2,i} }$ $\displaystyle{ x_{1,i} }$ $\displaystyle{ x_{2,i} - x_{1,i} }$
$\displaystyle{ \sgn }$ $\displaystyle{ \text{abs} }$
1 125 110 1 15
2 115 122  –1 7
3 130 125 1 5
4 140 120 1 20
5 140 140   0
6 115 124  –1 9
7 140 123 1 17
8 125 137  –1 12
9 140 135 1 5
10 135 145  –1 10
order by absolute difference
$\displaystyle{ i }$ $\displaystyle{ x_{2,i} }$ $\displaystyle{ x_{1,i} }$ $\displaystyle{ x_{2,i} - x_{1,i} }$
$\displaystyle{ \sgn }$ $\displaystyle{ \text{abs} }$ $\displaystyle{ R_i }$ $\displaystyle{ \sgn \cdot R_i }$
5 140 140   0
3 130 125 1 5 1.5 1.5
9 140 135 1 5 1.5 1.5
2 115 122  –1 7 3  –3
6 115 124  –1 9 4  –4
10 135 145  –1 10 5  –5
8 125 137  –1 12 6  –6
1 125 110 1 15 7 7
7 140 123 1 17 8 8
4 140 120 1 20 9 9

$\displaystyle{ \sgn }$ is the sign function, $\displaystyle{ \text{abs} }$ is the absolute value, and $\displaystyle{ R_i }$ is the rank. Notice that pairs 3 and 9 are tied in absolute value. They would be ranked 1 and 2, so each gets the average of those ranks, 1.5.

$\displaystyle{ W = 1.5+1.5-3-4-5-6+7+8+9=9 }$
$\displaystyle{ |W| \lt W_{\operatorname{crit}(\alpha = 0.05,\ 9 \text{, two-sided})} = 15 }$
$\displaystyle{ \therefore \text{failed to reject } H_0 }$ that the median of pairwise differences is different from zero.
The $\displaystyle{ p }$-value for this result is $\displaystyle{ 0.6113 }$

## Effect size

To compute an effect size for the signed-rank test, one can use the rank-biserial correlation.

If the test statistic T is reported, the rank correlation r is equal to the test statistic T divided by the total rank sum S, or r = T/S. [68] Using the above example, the test statistic is T = 9. The sample size of 9 has a total rank sum of S = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) = 45. Hence, the rank correlation is 9/45, so r = 0.20.

If the test statistic T is reported, an equivalent way to compute the rank correlation is with the difference in proportion between the two rank sums, which is the Kerby (2014) simple difference formula.[68] To continue with the current example, the sample size is 9, so the total rank sum is 45. T is the smaller of the two rank sums, so T is 3 + 4 + 5 + 6 = 18. From this information alone, the remaining rank sum can be computed, because it is the total sum S minus T, or in this case 45 − 18 = 27. Next, the two rank-sum proportions are 27/45 = 60% and 18/45 = 40%. Finally, the rank correlation is the difference between the two proportions (.60 minus .40), hence r = .20.

## Software implementations

• R includes an implementation of the test as wilcox.test(x,y, paired=TRUE), where x and y are vectors of equal length.[69]
• ALGLIB includes implementation of the Wilcoxon signed-rank test in C++, C#, Delphi, Visual Basic, etc.
• GNU Octave implements various one-tailed and two-tailed versions of the test in the wilcoxon_test function.
• SciPy includes an implementation of the Wilcoxon signed-rank test in Python
• Accord.NET includes an implementation of the Wilcoxon signed-rank test in C# for .NET applications
• MATLAB implements this test using "Wilcoxon rank sum test" as [p,h] = signrank(x,y) also returns a logical value indicating the test decision. The result h = 1 indicates a rejection of the null hypothesis, and h = 0 indicates a failure to reject the null hypothesis at the 5% significance level
• Julia HypothesisTests package includes the Wilcoxon signed-rank test as "value(SignedRankTest(x, y))"

## References

1. Conover, W. J. (1999). Practical nonparametric statistics (3rd ed.). John Wiley & Sons, Inc.. ISBN 0-471-16068-7. , p. 350
2. Wilcoxon, Frank (Dec 1945). "Individual comparisons by ranking methods". Biometrics Bulletin 1 (6): 80–83. doi:10.2307/3001968.
3. Siegel, Sidney (1956). Non-parametric statistics for the behavioral sciences. New York: McGraw-Hill. pp. 75–83. ISBN 9780070573482.
4. Conover, p. 352
5. Siegel, p. 76
6. Conover, p. 353
7. Pratt, John W.; Gibbons, Jean D. (1981). Concepts of Nonparametric Theory. Springer-Verlag. ISBN 978-1-4612-5933-6. , p. 148
8. Pratt and Gibbons, p. 148
9. Pratt and Gibbons, p. 148
10. Pratt and Gibbons, p. 150
11. Conover, pp. 352–357
12. Hettmansperger, Thomas P. (1984). Statistical Inference Based on Ranks. John Wiley & Sons. ISBN 0-471-88474-X. , pp. 32, 50
13. Pratt and Gibbons, p. 153
14. Pratt and Gibbons, pp. 153–154
15. Hettmansperger, pp. 38–39
16. Pratt and Gibbons, pp. 146–147
17. Pratt and Gibbons, pp. 146–147
18. Hettmansperger, pp. 30–31
19. Conover, p. 353
20. Pratt and Gibbons, pp. 155–156
21. Hettmansperger, pp. 49–50
22. Pratt and Gibbons, p. 155
23. Conover, p. 354
24. Hollander, Myles; Wolfe, Douglas A.; Chicken, Eric (2014). Nonparametric Statistical Methods (Third ed.). John Wiley & Sons, Inc.. ISBN 978-0-470-38737-5. , pp. 39–41
25. Pratt and Gibbons, p. 147
26. Pratt and Gibbons, p. 147
27. Hettmansperger, pp. 49–50
28. Wilcoxon, Frank (1949). Some Rapid Approximate Statistical Procedures. American Cynamic Co..
29. Pratt, J. (1959). "Remarks on zeros and ties in the Wilcoxon signed rank procedures". Journal of the American Statistical Association 54 (287): 655–667. doi:10.1080/01621459.1959.10501526.
30. Pratt, p. 659
31. Pratt, p. 663
32. Derrick, B; White, P (2017). "Comparing Two Samples from an Individual Likert Question". International Journal of Mathematics and Statistics 18 (3): 1–13.
33. Conover, William Jay (1973). "On Methods of Handling Ties in the Wilcoxon Signed-Rank Test". Journal of the American Statistical Association 68 (344): 985–988. doi:10.1080/01621459.1973.10481460.
34. Pratt and Gibbons, p. 162
35. Conover, pp. 352–353
36. Pratt and Gibbons, p. 164
37. Conover, pp. 358–359
38. Pratt, p. 660
39. Pratt and Gibbons, pp. 168–169
40. Pratt, pp. 661–662
41. Pratt and Gibbons, p. 170
42. Pratt and Gibbons, pp. 163, 166
43. Pratt, p. 660
44. Pratt and Gibbons, p. 166
45. Pratt and Gibbons, p. 171
46. Pratt, p. 661
47. Pratt, p. 660
48. Gibbons, Jean D.; Chakraborti, Subhabrata (2011). Nonparametric Statistical Inference (Fifth ed.). Chapman & Hall/CRC. ISBN 978-1-4200-7762-9. , p. 194
49. Hettmansperger, p. 34
50. Pratt and Gibbons, pp. 148–149
51. Pratt and Gibbons, pp. 148–149, pp. 186–187
52. Hettmansperger, p. 171
53. Pratt and Gibbons, p. 187
54. Pratt and Gibbons, p. 187
55. Pratt and Gibbons, p. 187
56. Pratt and Gibbons, p. 149
57. Kolassa, John E. (1995). "Edgeworth approximations for rank sum test statistics". Statistics and Probability Letters 24 (2): 169–171. doi:10.1016/0167-7152(95)00164-H.
58. Hettmansperger, p. 37
59. Hettmansperger, p. 35
60. Cureton, Edward E. (1967). "The normal approximation to the signed-rank sampling distribution when zero differences are present". Journal of the American Statistical Association 62 (319): 1068–1069. doi:10.1080/01621459.1967.10500917.
61. Pratt and Gibbons, p. 193
62. Wilcoxon, p. 82
63. Siegel, p. 76
64. Pratt and Gibbons, p. 158
65. Pratt and Gibbons, p. 159
66. Pratt and Gibbons, p. 191
67. Kerby, Dave S. (2014), "The simple difference formula: An approach to teaching nonparametric correlation.", Comprehensive Psychology 3: 11.IT.3.1, doi:10.2466/11.IT.3.1
68. Dalgaard, Peter (2008). Introductory Statistics with R. Springer Science & Business Media. pp. 99–100. ISBN 978-0-387-79053-4.