Chapter 8: Comparing Two Treatments

Example, page-248

In [1]:
# Variable declaration
from scipy import *
from pylab import *

data = array([[45,204.4,13825.3],[55,130.0,8632.0]])

size = data[0:2,0]
Mean = data[0:2,1]
variance = data[0:2,2]

alpha = 0.05             # level of significance
Z = 1.96                 # z value corresponding to alpha/2

# Calculation

y1 = (Mean[0] - Mean[1]) - ( Z* (sqrt( variance[0]/size[0] + variance[1]/size[1] )) )                          # Lower limit
y2 = (Mean[0] - Mean[1]) + ( Z* (sqrt( float(variance[0])/size[0] + float(variance[1])/size[1] )) )            # upper limit

y1 = round(y1,2)
y2 = round(y2,2)

# Result
print "95% confidence interval(in kWh per month): (",y1,",",y2,")"
95% confidence interval(in kWh per month): ( 32.17 , 116.63 )

Example, Page-249

In [2]:
# Variable declaration
from scipy import *
from pylab import *

Mean = array([0.136,0.083])
size = array([32,32])
variance = array([0.004,0.005])

alpha = 0.05             # level of significance

# Calculation
Z = (Mean[0] - Mean[1] - alpha)/(sqrt( variance[0]**2/size[0] + variance[1]**2/size[1] ))              # Lower limit
Z = round(Z,2)

# Result
if(Z>1.645):
    print "Null hypothesis rejected"
    print "The data substantiate the claim"
else:
    print "Null hypothesis accepted"
    print "The data does not substantiate the claim"
Null hypothesis rejected
The data substantiate the claim

Example, page-250

In [3]:
# Variable declaration

alpha = 0.05       #level of significance
n1 = 40            # size of sample-1
n2 = 40            # size of sample-2
std_dev1 = 27      # standard deviation-1
std_dev2 = 31      # standard deviation-2
Mean1 = 1647       # Mean of sample-1 ( in hours) 
Mean2 = 1638       # Mean of sample-2 ( in hours) 

# Calculation
from scipy import *
from pylab import *

# null hypothesis accepted if z>1.645  else rejected
Z = (Mean1 - Mean2) / (sqrt( square(std_dev1)/float(n1) + square(std_dev2)/float(n2) ))            # Z value
Z = round(Z,2)

# Result

if(Z>1.645):
    print "Null hypothesis rejected"
    print "observed difference between two samples is significant"
else:
    print "Null hypothesis accepted"
    print "observed difference between two samples is not significant"
Null hypothesis accepted
observed difference between two samples is not significant

Example, page-251

In [4]:
# Variable declaration

alpha = 0.05       #level of significance
n1 = 40            # size of sample-1
n2 = 40            # size of sample-2
std_dev1 = 27      # standard deviation-1
std_dev2 = 31      # standard deviation-2
Mean1 = 1647       # Mean of sample-1 ( in hours) 
Mean2 = 1638       # Mean of sample-2 ( in hours) 
delta1 = 0
delta2 = 16        # in hours

# Calculation
from scipy import *
from pylab import *

std_dev = sqrt( square(std_dev1) + square(std_dev2) )

Z = 1.645                                                 # at alpha = 0.05
Z1 = Z + sqrt(n1)*( (delta1 - delta2)/std_dev )       
Z1 = round(Z1,3)



# Probability of Z > -0.817 =0.793 
err_prob = 1 - 0.793                                      # type-2 error probability

# Result
print "type-2 error probability: ",err_prob
type-2 error probability:  0.207

Example, Page-253

In [5]:
# Variable declaration
from scipy import *
from pylab import *

val1 = 0
val2 = 0

Mine1 = array([8260,8130,8350,8070,8340])
Mine2 = array([7950,7890,7900,8140,7920,7840])

alpha = 0.01                        # level of significance
deg = len(Mine1) + len(Mine2) - 2   # Degree of freedom

Mean1 = sum(Mine1)/len(Mine1)
Mean2 = sum(Mine2)/len(Mine2)
 
# Calculation
for each in Mine1:
    val1 = val1 + (Mean1-each)**2
    
for each in Mine2:
    val2 = val2 + (Mean2-each)**2

var = (val1 + val2)/(len(Mine1)-1 + len(Mine2)-1)
std_dev = sqrt(var)

t = (Mean1 - Mean2)/(std_dev*(sqrt(1.0/len(Mine1) + 1.0/len(Mine2))))

# Result
if(t>3.250):
    print "Null hypothesis rejected"
    print "Average heat producing capacity is not same"
else:
    print "Null hypothesis accepted"
    print "Average heat producing capacity is same"
Null hypothesis rejected
Average heat producing capacity is not same

Example, page-254

In [9]:
# Variable declaration
f1 = []       #list for sample-1
f2 = []       #list for sample-2
n1 = 58       # sample-1 size
n2 = 27       # sample-2 size
t = 1.99      # t value corresponding to alpha/2

# Calculation
from scipy import *
from pylab import *

f1 = array([66.4,67.7,68.0,68.0,68.3,68.4,68.6,68.8,68.9,
             69.0,69.1,69.2,69.3,69.3,69.5,69.5,69.6,69.7,69.8,69.8,69.9,
             70.0,70.0,70.1,70.2,70.3,70.3,70.4,70.5,70.6,70.6,70.8,70.9,
             71.0,71.1,71.2,71.3,71.3,71.5,71.6,71.6,71.7,71.8,71.8,71.9,
             72.1,72.2,72.3,72.4,72.6,72.7,72.9,
             73.1,73.3,73.5,
             74.2,74.5,
             75.3])

Mean1 = mean(f1)
Mean1 = round(Mean1,2)
std_dev1 = std(f1)
std_dev1 = round(std_dev1,1) 

f2 = array([71.2,71.8,72.6,72.8,73.4,73.7,73.9,74.4,74.9,75.5,75.9,
            76.3,76.5,76.7,76.9,
            77.1,77.3,77.6,77.7,77.8,
            78.1,78.2,78.4,78.6,
            79.0,79.3,79.8])

Mean2 = mean(f2)
Mean2 = round(Mean2,2)
std_dev2 = std(f2)
std_dev2 = round(std_dev2,1)

y1 = (Mean1 - Mean2) - t*sqrt( ( (n1-1)*pow(std_dev1,2) + (n2-1)*pow(std_dev2,2) ) / (n1+n2-2) ) * sqrt( 1.0/n1 + 1.0/n2 )     # lower limit
y1 = round(y1,1)
y2 = (Mean1 - Mean2) + t*sqrt( ( (n1-1)*pow(std_dev1,2) + (n2-1)*pow(std_dev2,2) ) / (n1+n2-2) ) * sqrt( 1.0/n1 + 1.0/n2 )     # upper limit
y2 = round(y2,1)
if abs(y1)>abs(y2):
    m1=abs(y2)
    m2=abs(y1)
else:
    m1=abs(y1)
    m2=abs(y2)
# Result
print "95% large sample confidence interval(in thousand pounds per square): (",m1,",",m2,")"
95% large sample confidence interval(in thousand pounds per square): ( 4.5 , 6.4 )

Example, page-255

In [10]:
# Variable declaration
f1 = []       #list for sample-1
f2 = []       #list for sample-2
n1 = 58       # sample-1 size
n2 = 27       # sample-2 size
Z = 1.96      # Z value corresponding to alpha/2

# Calculation
from scipy import *
from pylab import *

f1 = [66.4,67.7,68.0,68.0,68.3,68.4,68.6,68.8,68.9,
     69.0,69.1,69.2,69.3,69.3,69.5,69.5,69.6,69.7,
     69.8,69.8,69.9,70.0,70.0,70.1,70.2,70.3,70.3,
     70.4,70.5,70.6,70.6,70.8,70.9,71.0,71.1,71.2,
     71.3,71.3,71.5,71.6,71.6,71.7,71.8,71.8,71.9,
     72.1,72.2,72.3,72.4,72.6,72.7,72.9,73.1,73.3,
     73.5,74.2,74.5,75.3]

Mean1 = mean(f1)
Mean1 = round(Mean1,2)
std_dev1 = std(f1)
std_dev1 = round(std_dev1,1) 

f2 = [71.2,71.8,72.6,72.8,73.4,73.7,73.9,
      74.4,74.9,75.5,75.9,76.3,76.5,76.7,
      76.9,77.1,77.3,77.6,77.7,77.8,78.1,
      78.2,78.4,78.6,79.0,79.3,79.8]

Mean2 = mean(f2)
Mean2 = round(Mean2,2)
std_dev2 = std(f2)
std_dev2 = round(std_dev2,1)

y1 = (Mean1 - Mean2) - Z*sqrt( pow(std_dev1,2)/n1 + pow(std_dev2,2)/n2 )    # lower limit
y1 = round(y1,2)
y1 = round(y1,1)
y2 = (Mean1 - Mean2) + Z*sqrt( pow(std_dev1,2)/n1 + pow(std_dev2,2)/n2 )    # upper limit
y2 = round(y2,1)
if abs(y1)>abs(y2):
    m1=abs(y2)
    m2=abs(y1)
else:
    m1=abs(y1)
    m2=abs(y2)

# Result
print "95% large sample confidence interval(in thousand pounds per square): (",m1,",",m2,")"
95% large sample confidence interval(in thousand pounds per square): ( 4.4 , 6.5 )

Example, Page-256

In [11]:
# Variable declaration
from scipy import *
from pylab import *

val1 = 0
val2 = 0

data1 = array([0.63,2.64,1.85,1.68,1.09,1.67,0.73,1.04,0.68])
data2 = array([3.71,4.09,4.11,3.75,3.49,3.27,3.72,3.49,4.26])
n1 = len(data1)
n2 = len(data2)

alpha = 0.05                        # level of significance
Mean1 = sum(data1)/len(data1)
Mean2 = sum(data2)/len(data2)

# Calculation

for each in data1:
    val1 = val1 + (Mean1-each)**2
    
for each in data2:
    val2 = val2 + (Mean2-each)**2
    
val1 = val1/(n1-1)
val2 = val2/(n2-1)

var = (val1 + val2)/(len(Mine1)-1 + len(Mine2)-1)

deg = pow((val1/n1 + val2/n2),2)/(pow(val1/n1,2)/(n1-1) + pow(val2/n2,2)/(n2-1))      
deg = round(deg,0)

t = (Mean1 - Mean2)/ sqrt( val1/n1 + val2/n2 )    # upper limit
t = round(t,2)

# Result
if(t<-2.201):
    print "Null hypothesis rejected"
    print "Mean product volume are different"
else:
    print "Null hypothesis accepted"
    print "Mean product volume are same"
Null hypothesis rejected
Mean product volume are different

Example, Page-257

In [12]:
# Variable declaration
from scipy import *
from pylab import *

val1 = 0
val2 = 0

data1 = array([0.63,2.64,1.85,1.68,1.09,1.67,0.73,1.04,0.68])
data2 = array([3.71,4.09,4.11,3.75,3.49,3.27,3.72,3.49,4.26])
n1 = len(data1)
n2 = len(data2)

alpha = 0.05                        # level of significance
Mean1 = sum(data1)/len(data1)
Mean2 = sum(data2)/len(data2)

# Calculation

for each in data1:
    val1 = val1 + (Mean1-each)**2
    
for each in data2:
    val2 = val2 + (Mean2-each)**2
    
val1 = val1/(n1-1)
val2 = val2/(n2-1)

var = (val1 + val2)/(len(Mine1)-1 + len(Mine2)-1)

deg = pow((val1/n1 + val2/n2),2)/(pow(val1/n1,2)/(n1-1) + pow(val2/n2,2)/(n2-1))      
deg = round(deg,0)

t = 2.201   # t-value for 11 degree of freedom

y1 = (Mean1 - Mean2 - t*(sqrt(val1/n1 + val2/n2)))    # Lower limit
y2 = (Mean1 - Mean2 + t*(sqrt(val1/n1 + val2/n2)))    # Lower limit

y1 = round(y1,3)
y2 = round(y2,3)

# Result
print "95% confidence interval(in gallons): (",y1,",",y2,")"
95% confidence interval(in gallons): ( -2.982 , -1.88 )

Example, page-259

In [13]:
# Variable declaration
n = 10

# Calculation
from scipy import *
from pylab import *
 
l = array([[45,36],[73,60],[46,44],[124,119],[33,35],[57,51],[83,77],[34,29],[26,24],[17,11]])
l1 = l[:,0]
l2 = l[:,1]
diff = l1 - l2       # diffrence in values
Mean = mean(diff)
std_dev = 4.08

# null hypothesis is rejected if t > 1.833 for v = 9 degrees of freedom
t = (Mean - 0) / (std_dev / sqrt(n))     # t value
t = round(t,2)

# Result
if(t > 1.833):
    print "null hypothesis is rejected"
    print "Industrial safety program is effective"
else:
    print "null hypothesis is accepted"
    print "Industrial safety program is effective"
null hypothesis is rejected
Industrial safety program is effective

Example, Page-260

In [15]:
# Variable declaration
n = 10

# Calculation
from scipy import *
from pylab import *
 
l = array([[45,36],[73,60],[46,44],[124,119],[33,35],[57,51],[83,77],[34,29],[26,24],[17,11]])
l1 = l[:,0]
l2 = l[:,1]
diff = l1 - l2       # diffrence in values
Mean = mean(diff)
std_dev = 4.08

t = 1.833

y1 = (Mean - t*std_dev/sqrt(n))    # Lower limit
y2 = (Mean + t*std_dev/sqrt(n))   # Lower limit

y1 = round(y1,1)
y2 = round(y2,1)

# Result
print "95% confidence interval(in gallons): (",y1,",",y2,")"
95% confidence interval(in gallons): ( 2.8 , 7.6 )

Example, Page-261

In [16]:
# Variable declaration
from scipy import *
from pylab import *
%matplotlib inline

val = 0
data1 = array([27,23,64,44,30,75,26,124,54,30,14])
data2 = array([15,13,22,29,31,64,30,64,56,20,21])
y = [1,1,1,1,1,1,1,1,1,1.1,1]
d = []

alpha = 0.05                        # level of significance
for i in range(0,11):
    d.append(data1[i]-data2[i])

n = len(d)    
Mean = sum(d)/float(len(d))

# Calculation
for each in d:
    val = val + (Mean-each)**2
    
val = val/float(n-1) 
deg = n-1

t = 2.228         # t-value for 10 degree of freedom

y1 = (Mean - t*(sqrt(val/n)))      # Lower limit
y2 = (Mean + t*(sqrt(val/n)))      # upper limit

y1 = round(y1,2)
y2 = round(y2,2)

# Result
print "95% confidence interval: (",y1,",",y2,")"
scatter(d,y)
title("Dot Diagram")
95% confidence interval: ( -0.47 , 27.02 )
Out[16]:
<matplotlib.text.Text at 0x83da0b8>