Easy way to perform Wilcoxon signed-rank test with Python.

The Wilcoxon signed-rank test is often also called non-parametric t-test. The word “non-parametric” in this case means that you know the population data does not have a normal distribution.

Wilcoxon signed-rank test features:
-Nonparametric alternative to the one- or two-samples t-test.
-Mainly used when the data do not conform to the normality assumptions.
-Tests for differences in medians instead of differences in means because mediand insensitive to outliers.

There are two slightly different versions of the test used:
- The Wilcoxon signed rank test compares your sample median against a hypothetical median.
- The Wilcoxon matched-pairs signed rank test computes the difference between each set of matched pairs, then follows the same procedure as the signed rank test to compare the sample against some median.

Generating initial data for Wilcoxon signed-rank test:

import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats

## generate the data

N = 30

data1 = np.random.poisson(1.4,N)
data2 = np.random.poisson(1,N)

colors = 'kr'
for i in range(N):
plt.plot([data1[i], data2[i]],[i, i],colors[int(data1[i]<=data2[i])])

plt.plot(data1,np.arange(N),'ks',markerfacecolor='k',label='data1')
plt.plot(data2,np.arange(N),'ro',markerfacecolor='r',label='data2')

plt.ylabel('Data index')
plt.xlabel('Data value')
plt.legend()

plt.show() Wilcoxon signed-rank test using the Python scipy library:

t,p = stats.wilcoxon(data1,data2)
print('Wilcoxon z=%g, p=%g'%(t,p))

OUT: Wilcoxon z=74, p=0.046319