Mann-Whitney U test (also called the Wilcoxon rank-sum test) is a non-parametric statistical test used to compare two independent groups to determine whether one tends to have larger values than the other. It is an alternative to the independent samples t-test when the assumptions of normality and equal variances are not met.
When to Use the Mann-Whitney U Test:
- Comparing Two Independent Groups – You have two separate groups and want to compare their distributions (e.g., treatment vs. control, males vs. females).
- Non-Normal Data – The data is not normally distributed, making a parametric test (like a t-test) inappropriate.
- Ordinal or Continuous Data – It works for ordinal (ranked) and continuous data but not for nominal (categorical) data.
- Unequal Sample Sizes – It can handle situations where the two groups have different sample sizes.
How It Works:
- It ranks all observations from both groups together.
- It then sums the ranks for each group.
- The U statistic is calculated based on rank sums and sample sizes.
- The test determines if the rank distributions between the two groups are significantly different.
Python Coding to perform this test:
import numpy as np
import scipy.stats as stats
# Sample data (two independent groups)
group1 = [45, 48, 52, 46, 47, 50, 49] # Example treatment group
group2 = [52, 53, 55, 51, 56, 54, 57] # Example control group
# Perform the Mann-Whitney U test
stat, p_value = stats.mannwhitneyu(group1, group2, alternative='two-sided')
# Print results
print(f"Mann-Whitney U Statistic: {stat}")
print(f"P-value: {p_value}")
# Interpretation
alpha = 0.05
if p_value < alpha:
print("Reject the null hypothesis: The distributions are significantly different.")
else:
print("Fail to reject the null hypothesis: No significant difference between the groups.")
Explanation:
stats.mannwhitneyu(group1, group2, alternative='two-sided'): Computes the Mann-Whitney U test.alternative='two-sided': Tests for any difference (use'less'or'greater'for one-sided tests).
- The U statistic is calculated, along with the p-value.
- If
p < 0.05, we reject the null hypothesis (no difference between groups) and conclude that the distributions differ.
Example Output:
Mann-Whitney U Statistic: 4.0
P-value: 0.0096
Reject the null hypothesis: The distributions are significantly different.

