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

The Wilcoxon signed-rank test is a non-parametric statistical test used to compare two matched groups. It is particularly useful when data doesn't meet the assumptions for a paired t-test, such as when the distribution is not normal. This test analyzes the differences between paired observations to determine if they are statistically significant.

What is the Wilcoxon Signed-Rank Test?

The Wilcoxon signed-rank test is a non-parametric alternative to the paired t-test. It examines whether the median difference between paired observations is significantly different from zero. This test is commonly used in before-and-after studies, such as measuring blood pressure before and after treatment, or comparing performance scores under different conditions.

The hypotheses being tested are:

  • Null Hypothesis (H?): The median difference between paired observations equals zero

  • Alternative Hypothesis (H?): The median difference between paired observations does not equal zero

Assumptions

For valid results, the Wilcoxon signed-rank test requires:

  • Paired observations from the same subjects or matched units

  • Independent pairs selected randomly

  • Data measured on at least an ordinal scale

  • Differences between pairs are approximately symmetric around the median

Conducting the Test in Python

Python's scipy.stats module provides the wilcoxon() function to perform this test. Let's see how to use it with a practical example:

import scipy.stats as stats

# Before and after treatment measurements
before = [120, 135, 140, 125, 130, 145, 138, 142, 128, 133]
after = [118, 132, 138, 123, 127, 140, 135, 138, 125, 130]

# Conduct the Wilcoxon signed-rank test
result = stats.wilcoxon(before, after)
print(f"Test statistic: {result.statistic}")
print(f"P-value: {result.pvalue}")
Test statistic: 8.0
P-value: 0.037109375

Interpreting the Results

The test returns two key values:

  • Test statistic (W): The sum of ranks for positive differences

  • P-value: The probability of observing the test statistic under the null hypothesis

In our example, with a p-value of 0.037 (less than 0.05), we reject the null hypothesis and conclude there is a significant difference between the before and after measurements.

Alternative Options

You can specify different alternative hypotheses:

import scipy.stats as stats

before = [120, 135, 140, 125, 130]
after = [118, 132, 138, 123, 127]

# Two-sided test (default)
result_two_sided = stats.wilcoxon(before, after, alternative='two-sided')

# One-sided tests
result_greater = stats.wilcoxon(before, after, alternative='greater')
result_less = stats.wilcoxon(before, after, alternative='less')

print(f"Two-sided p-value: {result_two_sided.pvalue:.4f}")
print(f"Greater p-value: {result_greater.pvalue:.4f}")
print(f"Less p-value: {result_less.pvalue:.4f}")
Two-sided p-value: 0.1250
Greater p-value: 0.0625
Less p-value: 0.9375

Conclusion

The Wilcoxon signed-rank test is a robust non-parametric alternative for comparing paired data when normality assumptions are violated. Use scipy.stats.wilcoxon() to perform the test, and interpret results based on the p-value relative to your significance level.

Updated on: 2026-03-26T22:48:25+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements