# How to do a Wilcoxon signed-rank test for matched pairs (in Python, using SciPy)

See all solutions.

Assume we have two samples of data that come in matched pairs, $x_1, x_2, x_3, \ldots x_k$ and $x’_1, x’_2, x’_3, \ldots x’_k$, which we might pair up as $(x_1,x’_1),(x_2,x’_2),\ldots,(x_k,x’_k)$. The two samples may be from different populations. Also assume that the sample sizes are small or the populations are not normally distributed.

Consider measuring the difference in each pair, $x_1-x’_1,x_2-x’_2,\ldots,x_k-x’_k$. We want to perform tests that compare the median of those differences, $m_D$, to a hypothesized value (equal, greater, or less). One method is the Wilcoxon Signed-Rank Test for Matched Pairs.

## Solution

The method we will use is equivalent to subtracting the two samples and then performing the signed-rank test. See how to do a Wilcoxon signed-rank test to compare the two methods.

We’re going to use fake data for illustrative purposes, but you can replace our fake data with your real data.

1
2
3
4
import numpy as np
# Replace sample1 and sample2 with your data
sample1 = np.array([156, 133, 90, 176, 119, 120, 40, 52, 167, 80])
sample2 = np.array([45, 36, 78, 54, 12, 25, 39, 48, 52, 70])


We choose a value, $0 \le \alpha \le 1$, as the Type I Error Rate. We’ll let $\alpha$ be 0.05.

### Two-tailed test

To test the null hypothesis $H_0: m_D = 0$, we use a two-tailed test:

1
2
3
from scipy import stats
from scipy.stats import wilcoxon
wilcoxon(sample1 - sample2)

1
WilcoxonResult(statistic=0.0, pvalue=0.001953125)


Our p-value, 0.001953125, is less than $\alpha=0.05$, so we have sufficient evidence to reject the null hypothesis. The median difference is significantly different from zero.

### Right-tailed test

To test the null hypothesis $H_0: m_D \le 0$, we use a right-tailed test:

1
wilcoxon(sample1 - sample2, alternative = 'greater')

1
WilcoxonResult(statistic=55.0, pvalue=0.0009765625)


Our p-value, 0.0009765625, is less than $\alpha=0.05$, so we have sufficient evidence to reject the null hypothesis. The median difference is significantly greater than zero.

### Left-tailed test

To test the null hypothesis $H_0: m_D \ge 0$, we use a left-tailed test:

1
wilcoxon(sample1 - sample2, alternative = 'less')

1
WilcoxonResult(statistic=55.0, pvalue=1.0)


Our p-value, 1.0, is greater than $\alpha$, so we do not have sufficient evidence to reject the null hypothesis. We should continue to assume that the mean difference may be less than (or equal to) zero.