How to Conduct a Wilcoxon Signed-Rank Test in Python?

A statistical test that compares two matched groups nonparametrically is the Wilcoxon test. Either the signed-rank test version or the rank sum test might be referenced. The tests are efficient in computing the difference between pairs of pairings and examining this difference to see if it is statistically different from the other. In this article, we'll examine the Wilcoxon signed-rank test and demonstrate how to run it in Python.

What is Wilcoxon signed-rank test?

The Wilcoxon signed-rank test is a non-parametric univariate test that can be used instead of the dependent t-test. The statistic value is typically referred to as a T value, which is when the Wilcoxon T test is also used. It is the basis for the computation made by scipy.stats.wilcoxon(). When the evidence contradicts the assumption of normalcy, this test is advised to be used. It is used to determine if ratings between the "before" and "after" conditions of a certain therapy or intervention change significantly. An illustration of this would be taking a person's blood pressure before and after undergoing a certain therapy, observing them during a certain condition, or at a specific period.

The hypothesis being tested is −

• Null Hypothesis (H0) − The difference between the pairings has a symmetric distribution around zero.

• Alternative Hypothesis (HA)The difference between the pairings does not have a symmetric distribution around zero.

Assumptions of Wilcoxon signed-rank test

The following presumptions must be true in order for the Wilcoxon signed-rank test findings to be believed −

• The identical population provides both observations in a pair.

• The paired observations are selected separately and a priori at random.

• The dependent variable (DV) has to be continuous and assessed using an ordinal or continuous scale.

Alternate tests should be utilized if any of these presumptions are broken.

Wilcoxon Signed-Rank Test in Python

We will simply do a Wilcoxon signed-rank test on the provided two groups using the Python programming language and the Wilcoxon() function from the scipy.stats module.

Example

import scipy.stats as stats
a = [574, 654, 79, 963, 54, 40, 3, 39, 43, 55]
b = [55, 87, 951, 54, 753, 529, 754, 2, 97, 796]
# conduct the Wilcoxon-Signed Rank Test
stats.wilcoxon(a, b)


Output

WilcoxonResult(statistic=20.0, pvalue=0.4921875)


Conclusion

In statistics, the Wilcoxon signed-ranked test is mostly used to determine the data's insights. In this article, we discussed it and how to do it in Python.