Chapter 14: Nonparametric Tests

Example, page-448

In [1]:
# Variable declaration
alpha = 0.01                           # level of significance

# Calculation
from scipy import *
from pylab import *

# null hypothesis: if U = 98 else alternative hypothesis
# x: number of Plus signs with respect to 98.0
# sample size = 14

l = array([99.0,102.3,99.8,100.5,99.7,96.2,99.1,102.5,103.3,97.4,100.4,98.9,98.3,98.0,101.6])
l = l-98.0

count =0                              # counts number of +ve signs

for each in l:
    if(each>0):
        count  = count + 1

# probability corresponding to count >=12 and n=14

p = 1 - 0.9935
p = round(p,4)

# Result
if(p>0.01):
    print "null hypothesis can not be rejected, i.e. median octane rating does not exceed 98.0"
else:
    print "null hypothesis rejected, i.e. median octane rating exceeds 98.0"
null hypothesis rejected, i.e. median octane rating exceeds 98.0

Example, Page-448

In [2]:
# Variable declaration
alpha = 0.05                          # level of significance
n = 10
l = [[45,73,46,124,33,57,83,34,26,17],[36,60,44,119,35,51,77,29,24,11]]

# Calculation
from scipy import *
from pylab import *

# null hypothesis: if U = 0 else alternative hypothesis
# sample size = 10

count =0                              # counts number of +ve signs

for i in range(0,10):
    if(l[0][i]>l[1][i]):
            count  = count + 1

# probability corresponding to x >=count and n=10
p = 1 - 0.9893
p = round(p,4)

# Result
if(p>0.05):
    print "null hypothesis can not be rejected, i.e. safety program is not effective"
else:
    print "null hypothesis rejected, i.e. safety program is effective"
null hypothesis rejected, i.e. safety program is effective

Example, Page-451

In [3]:
# Variable declaration
alpha = 0.01                          # level of significance
n1 = 15
n2 = 14
l = array([[45,73,46,124,33,57,83,34,26,17],[36,60,44,119,35,51,77,29,24,11]])

# Calculation
from scipy import *
from pylab import *

# null hypothesis: populations are identical else alternative hypothesis
# x: number of Plus signs with respect to 0
# sample size = 10

z_thr = 2.575

W1 = 162
U1 = W1 - (n1*(n1+1))/2

MU1 = n1*n2/2

sigma_sq_U1 = n1*n2*(n1+n2+1)/12
sigma_U1 = sqrt(sigma_sq_U1)

z_prt = (U1 - MU1)/sigma_U1

# Result
print "practical z:",round(z_prt,2)
if(z_prt<-2.575):
    print "null hypothesis rejected, i.e. Difference in population of grain size"
else:
    print "null hypothesis can not be rejected, i.e. No Difference in population of grain size"
practical z: -2.75
null hypothesis rejected, i.e. Difference in population of grain size

Example, page-452

In [4]:
# Variable declaration
alpha = 0.05                           # level of significance

# Calculation
from scipy import *
from pylab import *

# null hypothesis: if populations are identical, else : Alternative hypothesis

chi_sq_thr = 5.991                     # theoritical value of chi square at alpha = 0.05 with v = 2

a = [6,13,14,16,17,18]
b = [1,4.5,8,9,10,11,12]
c = [2,3,4.5,7,15]

# we know chi square calculated value

chi_sq_prt = (12.0/(18*19)) * (pow(sum(a),2)/6 + pow(sum(b),2)/7 + pow(sum(c),2)/5) - 3*19 
chi_sq_prt = round(chi_sq_prt,1)

# Result
print "practical chi square:",round(chi_sq_prt,2)
if(chi_sq_prt > chi_sq_thr):
    print "null hypothesis must be rejected,three methods against corrosion are not equally effective"
else:
    print "null hypothesis can not be rejected,three methods against corrosion are equally effective"
practical chi square: 6.7
null hypothesis must be rejected,three methods against corrosion are not equally effective

Example, Page-453

In [5]:
# Variable declaration
alpha = 0.05                          # level of significance
n = 10
R = [5,8,6,10,3,7,9,4,2,1]
S = [5,8,6,10,4,7,9,3,2,1]

# Calculation
from scipy import *
from pylab import *

RS = 0

for i in range(0,10):
    RS = RS + R[i]*S[i]
    
rs = (RS - n*((n+1)**2)/4.0)/(n*(n**2-1)/12.0)
rs = round(rs,3)

# Result
print "rs value:",rs,"i.e. there is strong association along an increasing curve"
rs value: 0.988 i.e. there is strong association along an increasing curve

Example, Page-456

In [6]:
# Variable declaration
alpha = 0.01                          # level of significance
n1 = 10
n2 = 17
U = 6

# Calculation
from scipy import *
from pylab import *

z_thr = 2.575

MU = (2*n1*n2)/float(n1+n2) + 1

SigmaU = sqrt(((2*n1*n2)*(2*n1*n2-n1-n2)) / float(((n1+n2)**2)*(n1+n2-1)))

z_prt = (U - MU)/SigmaU

# Result
print "practical Z:",round(z_prt,2)
if(z_prt < -2.575):
    print "null hypothesis must be rejected, Arrangement is not random"
else:
    print "null hypothesis can not be rejected,Arrangement is random"
practical Z: -3.2
null hypothesis must be rejected, Arrangement is not random

Example, Page-457

In [7]:
# Variable declaration
alpha = 0.01                          # level of significance
n1 = 0
n2 = 0
R = array([0.261,0.258,0.249,0.251,0.247,0.256,0.250,0.247,0.255,0.243,
           0.252,0.250,0.253,0.247,0.251,0.243,0.258,0.251,0.245,0.250,
           0.248,0.252,0.254,0.250,0.247,0.253,0.251,0.246,0.249,0.252,
           0.247,0.250,0.253,0.247,0.249,0.253,0.246,0.251,0.249,0.253])

# Calculation
from scipy import *
from pylab import *

z_thr = 2.33

Median = median(R)

for i in range(0,40):
    if(R[i]>Median):
        n1 = n1 + 1
    elif(R[i]<Median):
        n2 = n2 + 1

U = 27
MU = (2*n1*n2)/float(n1+n2) + 1

SigmaU = sqrt(((2*n1*n2)*(2*n1*n2-n1-n2)) / float(((n1+n2)**2)*(n1+n2-1)))

z_prt = (U - MU)/SigmaU

# Result
print "practical Z:",round(z_prt,2)
if(z_prt > 2.33):
    print "null hypothesis must be rejected"
else:
    print "null hypothesis can not be rejected"
practical Z: 2.98
null hypothesis must be rejected

Example, Page-459

In [8]:
# Variable declaration
alpha = 0.05             # level of significance
x = array([2.4,4.4,4.8,6.2,14.8,19.5,23.1,25.0,28.2,28.7])
n = 10          
l = []
# Calculation
from scipy import *
from pylab import *

A_sq = 0

for i in range(0,10):
    l.append(x[i]/30)
    
for i in range(1,11):
    A_sq = A_sq + (2*i-1)*(math.log(l[i-1]) + math.log(1 - l[n-i]))
    
A_sq = (-A_sq)/n - n
A_sq = round(A_sq,4)

# Result
print "A square value:",A_sq
print "As A_sq is large, We can not reject Null hypothesis"
A square value: 0.5266
As A_sq is large, We can not reject Null hypothesis