The Wayback Machine - https://web.archive.org/web/20250416092956/https://www.geeksforgeeks.org/mann-and-whitney-u-test/
Open In App

Mann and Whitney U test

Last Updated : 22 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Mann and Whitney’s U-test or Wilcoxon rank-sum testis the non-parametric statistic hypothesis test that is used to analyze the difference between two independent samples of ordinal data. In this test, we have provided two randomly drawn samples and we have to verify whether these two samples is from the same population.

The Mann-Whitney U Test compares the distributions of two independent groups and determines whether one tends to have larger values than the other. It ranks all observations from both groups together and assesses whether the ranks differ significantly between the two groups.

Hypotheses in the Mann-Whitney U Test

While the test does not directly compare medians, it is often interpreted as a test of median differences when the data distributions are similarly shaped.

When to Use the Mann-Whitney U Test

The test is appropriate when:

  • The data are ordinal or continuous but not normally distributed.
  • There are two independent groups (e.g., treated vs. untreated patients, two different teaching methods, etc.).
  • The sample size is small (usually less than 30 per group).
  • The distributions of the two groups should have a similar shape for valid median comparisons.

If more than two groups need to be compared, the Kruskal-Wallis Test should be used instead.

Assumptions of the Mann-Whitney U Test

  1. Independence: Observations in each group must be independent.
  2. Ordinal or Continuous Data: The dependent variable should be measured at an ordinal or continuous level.
  3. Same Distribution Shape: Although normality is not required, the distributions of both groups should have a similar shape for meaningful results.
  4. Sufficient Sample Size: Each sample should have at least 5 observations for valid statistical conclusions.

Steps for Performing the Mann Whitney U test

1. Collect two independent samples (Sample 1 and Sample 2).

2. Rank the data from smallest to largest across both groups. If two observations have the same value, assign them the average rank.

3. Sum the ranks for each sample (denoted as R₁ and R₂).

4. Calculate the U-statistic using the formula:

[Tex]U_1 = n_{1}n_{2} +\frac{n_{1}\left ( n_{1}+1 \right )}{2} – R_{1}  [/Tex]

[Tex]U_2 = n_{1}n_{2} +\frac{n_{2}\left ( n_{2}+1 \right )}{2} – R_{2}[/Tex]

where:

  • n1 , n2 are the sample sizes for the two groups.
  • R1 , R2 are the rank sums of each group.

The final U-statistic is the smaller value of U₁ and U₂.

5. Compare U to the critical value from the Mann-Whitney U Table at the chosen significance level (e.g., 0.05).

6. Decision rule:

  • If U ≤ U₀ (critical value), reject the null hypothesis.
  • Otherwise, do not reject the null hypothesis.

Example: Comparing Student Test Scores

A test was conducted on two batches of students, and their scores are given below:

Batch 1Batch 2
39
47
25
610
28
56

Step 1: Define the Hypotheses

  • Null Hypothesis (H₀): There is no significant difference between the test scores of the two batches.
  • Alternative Hypothesis (H₁): There is a significant difference between the test scores of the two batches.

The level of significance (α) is set at 0.05.

Step 2: Rank the Scores

To perform the Mann-Whitney U test, we rank the combined data (both batches together) from lowest to highest. If two values are the same, we assign them the average rank.

Batch 1Rank (Batch 1)Batch 2Rank (Batch 2)
21.555.5
21.567.5
3379
44810
55.5911
67.51012
  • Rank Sum for Batch 1 (R₁) = 23
  • Rank Sum for Batch 2 (R₂) = 55

Step 3: Compute the Mann-Whitney U Statistic

Calculate U1 and U2 ​:

[Tex]U_1 = (6 \times 6) + \frac{6(6+1)}{2} – 23 = 36 + 21 – 23 = 34[/Tex]

[Tex]U_2 = (6 \times 6) + \frac{6(6+1)}{2} – 55 = 36 + 21 – 55 = 2[/Tex]

The Mann-Whitney U statistic is the smaller of U1​ and U2​:

U = min ⁡(34, 2) = 2

Step 4: Compare U with the Critical Value

Using a Mann-Whitney U critical values table for n1 = 6 and n2 = 6 at α = 0.05,
we find the critical value: U0 = 5

Since U = 2 is less than U0 = 5, we reject the null hypothesis.

Step 5: Conclusion

Since we rejected the null hypothesis, we conclude that there is a significant difference in test scores between the two student batches.

Implementation Mann and Whitney U test in Python

python
# code for Mann-Whitney U test
from scipy.stats import mannwhitneyu
# Take batch 1 and batch 2 data as per above example
batch_1 =[3, 4, 2, 6, 2, 5]
batch_2 =[9, 7, 5, 10, 8, 6]

# perform mann whitney test
stat, p_value = mannwhitneyu(batch_1, batch_2)
print('Statistics=%.2f, p=%.2f' % (stat, p_value))
# Level of significance
alpha = 0.05
# conclusion
if p_value < alpha:
    print('Reject Null Hypothesis (Significant difference between two samples)')
else:
    print('Do not Reject Null Hypothesis (No significant difference between two samples)')

Output:

Statistics=2.00, p=0.01
Reject Null Hypothesis (Significant difference between two samples)



Next Article
Article Tags :
Practice Tags :

Similar Reads