# 17.5.2.2 Algorithm (Paired-Sample Wilcoxon Signed Rank Test)

The paired sample WilCoxon sign rank test can be used to test whether the median of the first population is the same as that of the second population, which the two populations are matched with equal size.

1. For $\{x_i,y_i\}\,\!$, $i=1,2,\ldots ,n$. The null hypothesis $H_0\,\!$ is that the medians are the same, while the alternative hypothesis $H_1\,\!$ can be one or two tailed. The Wilcoxon sign rank test differs from the sign test in that the magnitude of the scores is taken into account, rather than simply the direction of such scores.
2. For $\{x_i,y_i\}\,\!$, $i=1,2,\ldots ,n$.The signed difference $d_i=x_i-y_i\,\!$ is found.
3. The absolute differences $\left| d_i\right|$ are ranked with rank and any tied values of $\left| d_i\right|$ are assigned the average of the tied ranks. The user may choose whether or not to ignore any cases where $d_i=0\,\!$ by removing them before or after ranking.
4. The number of non-zero $d_i=0\,\!$'s is found denoted as $n_1\,\!$.
5. To each rank is affixed the sign of the $d_i=0\,\!$ to which it corresponds. Let $s_i=sign(d_i)r_i\,\!$.
6. The sum of the positive-signed ranks $W_1=\sum_{s_i>0}s_i=\sum_{i=1}^n\max (s_i,0)$ is calculated.
7. The tail probability, $P\,\!$ , corresponding to $W\,\!$, depending on the choice of the alternative hypothesis, $H_1\,\!$. If $n_1\leq 80$, $P$ is computed exactly; otherwise, an approximation $P\,\!$ to is returned based on an approximate Normal test statistics $Z\,\!$, where $z=\frac{(W-\frac{n_1(n_1+1)}4)-\frac 12\cdot sign(W-\frac{n_1(n_1+1)}4)}{\sqrt{\frac 14\cdot \sum_{i=1}^nS_i^2}}$

The value $P\,\!$ of can be used to perform a significance test on the median against the alternative hypothesis. Let $\alpha\,\!$ be the size of significance test (that is, $\alpha\,\!$ is the probability of rejecting $H_0\,\!$ when $H_0\,\!$ is true). If $P<\alpha \,\!$ then the null hypothesis should be rejected. Typically $\alpha\,\!$ might be 0.05 or 0.01.

For more details of the algorithm, please refer to nag_wilcoxon_test (g08agc)