Nonparametric Statistical Tests using Python: An Introductory Tutorial

Nonparametric Statistical Tests using Python: An Introductory Tutorial
  1. Mann-Whitney U Test,
  2. Wilcoxon Signed-Rank Test,
  3. Kruskal-Wallis H Test and
  4. Friedman Test.

Mann-Whitney U Test

  • observations in each sample are independent and identically distributed (iid),
  • observations in each sample can be ranked.
  • H0: the distributions of both samples are equal,
  • H1: the distributions of both samples are not equal.
from numpy.random import seed
from numpy.random import randn
# seed the random number generator
seed(1)
# generate two independent samples
data1 = 15*randn(50)+30
data2 = 15*randn(50)+31
# Mann-Whitney U test 
from scipy.stats import mannwhitneyu
# compare samples
stat, p = mannwhitneyu(data1, data2)
# print the test statistic and p-value
print(‘stat: ‘,stat, ‘\np-value: ‘,p)
stat: 1062.0 
p-value: 0.19615247557393267
# interpret
alpha = 0.05 # 5% significance level
if p > alpha:
print(‘Fails to reject H0, i.e., same distribution’)
else:
print(‘Rejects H0, i.e., different distribution’)
Fails to reject H0, i.e., same distribution

Wilcoxon Signed-Rank Test

  • observations in each sample are independent and identically distributed (iid),
  • observations in each sample can be ranked,
  • observations across each sample are paired.
  • H0: the distributions of both samples are equal,
  • H1: the distributions of both samples are not equal.
# Wilcoxon Signed-Rank Test
from scipy.stats import wilcoxon
# compare samples
stat, p = wilcoxon(data1, data2)
# print the test statistic and p-value
print(‘stat: ‘,stat, ‘\np-value: ‘,p)
stat:  506.0 
p-value: 0.20429624539024516
# interpret
alpha = 0.05 # 5% significance level
if p > alpha:
print(‘Fails to reject H0, i.e., same distribution’)
else:
print(‘Rejects H0, i.e., different distribution’)
Fails to reject H0, i.e., same distribution

Kruskal-Wallis H Test

  • observations in each sample are independent and identically distributed (iid),
  • observations in each sample can be ranked.
  • H0: the distributions of all samples are equal,
  • H1: the distributions of one or more samples are not equal.
# Kruskal-Wallis H Test
from scipy.stats import kruskal
# compare samples
stat, p = kruskal(data1, data2)
# print the test statistic and p-value
print(‘stat: ‘,stat, ‘\np-value: ‘,p)
stat:  1.6797148514851301 
p-value: 0.1949623463490322
# interpret
alpha = 0.05 # 5% significance level
if p > alpha:
print(‘Fails to reject H0, i.e., same distribution’)
else:
print(‘Rejects H0, i.e., different distribution’)
Fails to reject H0, i.e., same distribution 

Friedman Test

  • observations in each sample are independent and identically distributed (iid),
  • observations in each sample can be ranked,
  • observations across each sample are paired.
  • H0: the distributions of all samples are equal,
  • H1: the distributions of one or more samples are not equal.
from numpy.random import seed
from numpy.random import randn
# seed the random number generator
seed(1)
# generate two independent samples
data1 = 15*randn(50)+30
data2 = 15*randn(50)+30.50
data3 = 15*randn(50)+31
# Friedman Test 
from scipy.stats import friedmanchisquare
# compare samples
stat, p = friedmanchisquare(data1, data2, data3)
# print the test statistic and p-value
print(‘stat: ‘,stat, ‘\np-value: ‘,p)
stat:  1.5600000000000591 
p-value: 0.45840601130520997
# interpret
alpha = 0.05 # 5% significance level
if p > alpha:
print(‘Fails to reject H0, i.e., same distribution’)
else:
print(‘Rejects H0, i.e., different distribution’)
Fails to reject H0, i.e., same distribution

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Soumen Atta, Ph.D.

Dr. Soumen Atta, Ph.D.

94 Followers

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.