Chapter 35 : Sampling And Inference

Example 35.1, page no. 864

In [1]:
print "Suppose the coin is unbiased"
print "Then probability of getting the head in a toss = 1/2 "
print "Then, expected no. of successes = a = 1/2∗400"
a = 1/2*400
print "Observed no. of successes = 216"
b = 216
print "The excess of observed value over expected value = ",b-a
print "S. D. of simple sampling = (n∗p∗q)ˆ0.5 = c "
c = (400*0.5*0.5)**0.5
print "Hence, z = (b−a)/c = ",(b-a)/c
print "As z<1.96, the hypothesis is accepted at 5% level of significance"
Suppose the coin is unbiased
Then probability of getting the head in a toss = 1/2 
Then, expected no. of successes = a = 1/2∗400
Observed no. of successes = 216
The excess of observed value over expected value =  216
S. D. of simple sampling = (n∗p∗q)ˆ0.5 = c 
Hence, z = (b−a)/c =  21.6
As z<1.96, the hypothesis is accepted at 5% level of significance

Example 35.2, page no. 865

In [2]:
print "Suppose the die is unbiased "
print "Then probability of getting 5 or 6 with one die=1/3 "
print "Then, expected no. of successes = a = 1/3∗9000 "
a = 1./3*9000
print "Observed no. of successes = 3240"
b = 3240
print "The excess of observed value over expected value = ",b-a
print "S. D. of simple sampling = (n∗p∗q)ˆ0.5=c"
c = (9000*(1./3)*(2./3))**0.5
print "Hence, z = (b−a)/c = ",(b-a)/c
print "As z>2.58, the hypothesis has to be rejected at 1% level of significance"
Suppose the die is unbiased 
Then probability of getting 5 or 6 with one die=1/3 
Then, expected no. of successes = a = 1/3∗9000 
Observed no. of successes = 3240
The excess of observed value over expected value =  240.0
S. D. of simple sampling = (n∗p∗q)ˆ0.5=c
Hence, z = (b−a)/c =  5.366563146
As z>2.58, the hypothesis has to be rejected at 1% level of significance

Example 35.3, page no. 865

In [3]:
p = 206./840
print "q = 1−p "
q = 1-p
n = 840
print "Standard error of the population of families having a monthly income of rs. 250 or less=(p∗q/n)ˆ0.5 = ",(p*q/n)**0.5
print "Hence taking 103/420 to be the estimate of families having a monthly income of rs. 250 or less, the limits are 20% and 29% approximately"
q = 1−p 
Standard error of the population of families having a monthly income of rs. 250 or less=(p∗q/n)ˆ0.5 =  0.0148442858929
Hence taking 103/420 to be the estimate of families having a monthly income of rs. 250 or less, the limits are 20% and 29% approximately

Example 35.4, page no. 866

In [6]:
n1 = 900
n2 = 1600
p1 = 20./100
p2 = 18.5/100
print "p=(n1∗p1+n2∗p2)/(n1+n2)"
p = (n1*p1+n2*p2)/(n1+n2)
print p
print "q = 1−p "
q = 1-p
print q
print "e=(p∗q∗(1/n1+1/n2))ˆ0.5"
e = (p*q*((1./n1)+(1./n2)))**0.5
print e
z = (p1-p2)/e
print "z = ",z
print "As z<1, the difference between the proportions is not significant. "
p=(n1∗p1+n2∗p2)/(n1+n2)
0.1904
q = 1−p 
0.8096
e=(p∗q∗(1/n1+1/n2))ˆ0.5
0.0163590274093
z =  0.916924926202
As z<1, the difference between the proportions is not significant. 

Example 35.5, page no. 867

In [7]:
p1 = 0.3
p2 = 0.25
print "q1 = 1−p1 "
q1 = 1-p1
print "q2=1−p2"
q2 = 1-p2
n1 = 1200
n2 = 900
print "e=((p1∗q1/n1)+(p2*q2/n2))ˆ0.5"
e = ((p1*q1/n1)+(p2*q2/n2))**0.5
z = (p1-p2)/e
print "Hence, it is likely that real difference will be hidden." 
q1 = 1−p1 
q2=1−p2
e=((p1∗q1/n1)+(p2*q2/n2))ˆ0.5
Hence, it is likely that real difference will be hidden.

Example 35.6, page no. 868

In [11]:
print "m and n represents mean and number of objects in sample respectively "
m = 3.4
n = 900.
M = 3.25
d = 1.61
print "z=(m−M)/(d/(nˆ0.5)"
z = (m-M)/(d/(n**0.5))
print "z = ",z
print "As z>1.96, it cannot be regarded as a random sample"
m and n represents mean and number of objects in sample respectively 
z=(m−M)/(d/(nˆ0.5)
z =  2.7950310559
As z>1.96, it cannot be regarded as a random sample

Example 35.9, page no. 871

In [12]:
print "m1 and n1 represents mean and no. of objects in sample 1"
print "m2 and n2 represents mean and no. of objects in sample 2"
m1 = 67.5
m2 = 68.
n1 = 1000.
n2 = 2000.
d = 2.5
print "On the hypothesis that the samples are drawn from the same population of d = 2.5, we get"
z = (m1-m2)/(d*((1/n1)+(1/n2))**0.5)
print "z = ",z
print "Since |z|>1.96, thus samples cannot be regarded as drawn from the same population"
m1 and n1 represents mean and no. of objects in sample 1
m2 and n2 represents mean and no. of objects in sample 2
On the hypothesis that the samples are drawn from the same population of d = 2.5, we get
z =  -5.16397779494
Since |z|>1.96, thus samples cannot be regarded as drawn from the same population

Example 35.10, page no. 872

In [13]:
print "m1, d1 and n1 denotes mean, deviation and no.of objects in first sample "
m1 = 67.85
d1 = 2.56
n1 = 6400.
print "m2, d2 and n2 denotes mean, deviation and no.of objects in second sample"
m2 = 68.55
d2 = 2.52
n2 = 1600.
print "S. E. of the difference of the mean heights is ",
e = ((d**2/n1)+(d2**2/n2))**0.5
print e
print m1-m2
print "|m1−m2|>10e, this is highly significant. hence, the data indicates that the sailors are on the average taller than the soldiers."
m1, d1 and n1 denotes mean, deviation and no.of objects in first sample 
m2, d2 and n2 denotes mean, deviation and no.of objects in second sample
S. E. of the difference of the mean heights is  0.0703246933872
-0.7
|m1−m2|>10e, this is highly significant. hence, the data indicates that the sailors are on the average taller than the soldiers.

Example 35.12, page no. 874

In [18]:
import numpy

A = numpy.zeros((3,9))
n = 9
print "First of row denotes the different values of sample "
A[0,:] = [45,47,50,52,48,47,49,53,51]
print "The second row denotes the corresponding deviation"
for i in range(0,9):
    A[1,i] = A[0,i]-48
print "The third row denotes the corresponding square of deviation"
for i in range(0,9):
    A[2,i] = A[1,i]**2
print "The sum of second row elements = ",
a  =0
for i in range(0,9):
    a = a+A[1,i]
print a
print "The sum of third row elements = ",
b = 0
for i in range(0,9):
    b = b+A[2,i]
print b
print "let m be the mean "
m = 48+a/n
print "let d be the standard deviation"
d = ((b/n)-(a/n)**2)**0.5
t = (m-47.5)*(n-1)**0.5/d
print t
First of row denotes the different values of sample 
The second row denotes the corresponding deviation
The third row denotes the corresponding square of deviation
The sum of second row elements =  10.0
The sum of third row elements =  66.0
let m be the mean 
let d be the standard deviation
1.84522581263

Example 35.13, page no. 876

In [19]:
print "d and n represents the deviation and no. objects in given sample"
n = 10.
d = 0.04
m = 0.742
M = 0.700
print "Taking the hypothesis that the product is not inferior i.e. there is no significant difference between m and M"
t = (m-M)*(n-1)**0.5/d
print t
print "Degrees of freedom= "
f = n-1
print f
d and n represents the deviation and no. objects in given sample
Taking the hypothesis that the product is not inferior i.e. there is no significant difference between m and M
3.15
Degrees of freedom= 
9.0

Example 34.15, page no. 878

In [21]:
import numpy

A = numpy.zeros((6,11))
n = 11
print "The first row denotes the boy no. "
A[0,:] = [1,2,3,4,5,6,7,8,9,10,11]
print "The second row denotes the marks in test I(x1)"
A[1,:] = [23,20,19,21,18,20,18,17,23,16,19]
print "The third row denotes the marks in test I(x2)"
A[2,:] = [24,19,22,18,20,22,20,20,23,20,17]
print "The fourth row denotes the difference of marks in two tests(d)"
for i in range (0,11):
    A[3,i] = A[2,i]-A[1,i]
print "The fifth row denotes the (d−1)"
for i in range (0,11):
    A[4,i] = A[3,i]-1
print "The sixth row denotes the square of elements of fourth row "
for i in range(0,11):
    A[5,i] = A[3,i]**2
print A
a = 0
print "The sum of elements of fourth row="
for i in range(0,11):
    a = a+A[3,i]
print a
b = 0
print "The sum of elements of sixth row= "
for i in range(0,11):
    b = b + A[5,i]
print b
print "Standard deviation"
d = (b/(n-1))**0.5
t = (1-0)*(n)**0.5/2.24
print "d = ",d
print "t = ",t
The first row denotes the boy no. 
The second row denotes the marks in test I(x1)
The third row denotes the marks in test I(x2)
The fourth row denotes the difference of marks in two tests(d)
The fifth row denotes the (d−1)
The sixth row denotes the square of elements of fourth row 
[[  1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.]
 [ 23.  20.  19.  21.  18.  20.  18.  17.  23.  16.  19.]
 [ 24.  19.  22.  18.  20.  22.  20.  20.  23.  20.  17.]
 [  1.  -1.   3.  -3.   2.   2.   2.   3.   0.   4.  -2.]
 [  0.  -2.   2.  -4.   1.   1.   1.   2.  -1.   3.  -3.]
 [  1.   1.   9.   9.   4.   4.   4.   9.   0.  16.   4.]]
The sum of elements of fourth row=
11.0
The sum of elements of sixth row= 
61.0
Standard deviation
d =  2.46981780705
t =  1.48063606712