Chapter 34 : Probability And Distributions

Example 34.1, page no. 830

In [1]:
print "From the principle of counting, the required no. of ways are 12∗11∗10∗9= "
print 12*11*10*9
From the principle of counting, the required no. of ways are 12∗11∗10∗9= 
11880

Example 34.2.1, page no. 831

In [2]:
import math

print "no. of permutations=9!/(2!∗2!∗2!)"
print math.factorial(9)/(math.factorial(2)*math.factorial(2)*math.factorial(2))
no. of permutations=9!/(2!∗2!∗2!)
45360

Example 34.2.2, page no. 831

In [3]:
import math

print "no. of permutations=9!/(2!∗2!∗3!*3!)"
print math.factorial(9)/(math.factorial(2)*math.factorial(2)*math.factorial(3)*math.factorial(3))
no. of permutations=9!/(2!∗2!∗3!*3!)
2520

Example 34.3.1, page no. 832

In [4]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "no. of committees=C(6,3)∗C(5,2)=’"
print C(6,3)*C(5,2)
no. of committees=C(6,3)∗C(5,2)=’
200

Example 34.3.2, page no. 832

In [5]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "no. of committees=C(4,1)∗C(5,2)=’"
print C(4,1)*C(5,2)
no. of committees=C(4,1)∗C(5,2)=’
40

Example 34.3.3, page no. 833

In [6]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "no. of committees=C(6,3)∗C(4,2)=’"
print C(6,3)*C(4,2)
no. of committees=C(6,3)∗C(4,2)=’
120

Example 34.4.1, page no. 834

In [9]:
import math

print "The probability of getting a four is 1/6= ",1./6
The probability of getting a four is 1/6=  0.166666666667

Example 34.4.2, page no. 834

In [10]:
import math

print "The probability of getting an even no. 1/2= ",1./2
The probability of getting an even no. 1/2=  0.5

Example 34.5, page no. 835

In [11]:
import math

print "The probability of 53 Sundays is 2/7= ",2./7
The probability of 53 Sundays is 2/7=  0.285714285714

Example 34.6, page no. 835

In [13]:
import math

print "The five digits can be arranged in 5! ways = ",math.factorial(5)
print "Of which 4! will begin with 0 = ",math.factorial(4)
print "So, total no. of five digit numbers = 5!−4! = ",math.factorial(5)-math.factorial(4)
print "The numbers ending in 04, 12, 20, 24, 32, 40 will be divisible by 4 "
print "numbers ending in 04 = 3! = ",math.factorial(3)
print "numbers ending in 12 = 3!−2! = ",math.factorial(3)-math.factorial(2)
print "numbers ending in 20 = 3! = ",math.factorial(3)
print "numbers ending in 24 = 3!−2! = ",math.factorial(3)-math.factorial(2)
print "numbers ending in 32 = 3!−2! = ",math.factorial(3)-math.factorial(2)
print "numbers ending in 40 = 3! = ",math.factorial(3)
print "So, total no. of favourable ways = 6+4+6+4+4+6 = ",6+4+6+4+4+6
print "probability = 30/96 = ",30./96
The five digits can be arranged in 5! ways =  120
Of which 4! will begin with 0 =  24
So, total no. of five digit numbers = 5!−4! =  96
The numbers ending in 04, 12, 20, 24, 32, 40 will be divisible by 4 
numbers ending in 04 = 3! =  6
numbers ending in 12 = 3!−2! =  4
numbers ending in 20 = 3! =  6
numbers ending in 24 = 3!−2! =  4
numbers ending in 32 = 3!−2! =  4
numbers ending in 40 = 3! =  6
So, total no. of favourable ways = 6+4+6+4+4+6 =  30
probability = 30/96 =  0.3125

Example 34.7, page no. 836

In [15]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Total no. of possible cases = C(40,4) = ",C(40,4)
print "Favourable outcomes = C(24,2)∗C(15,1) = ",C(24,2)*C(15,1)
print "Probability = ",float(C(24,2)*C(15,1))/C(40,4)
Total no. of possible cases = C(40,4) =  91390
Favourable outcomes = C(24,2)∗C(15,1) =  4140
Probability =  0.0453003610898

Example 34.8, page no. 836

In [16]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Total no. of possible cases = C(15,8) = ",C(15,8)
print "Favourable outcomes = C(5,2)∗C(10,6) = ",C(5,2)*C(10,6)
print "Probability = ",float(C(5,2)*C(10,6))/C(15,8)
Total no. of possible cases = C(15,8) =  6435
Favourable outcomes = C(5,2)∗C(10,6) =  2100
Probability =  0.32634032634

Example 34.9.1, page no. 837

In [17]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Total no. of possible cases = C(9,3) = ",C(9,3)
print "Favourable outcomes = C(2,1)∗C(3,1)*C(4,1) = ",C(2,1)*C(3,1)*C(4,1)
print "Probability = ",float(C(2,1)*C(3,1)*C(4,1))/C(9,3)
Total no. of possible cases = C(9,3) =  84
Favourable outcomes = C(2,1)∗C(3,1)*C(4,1) =  24
Probability =  0.285714285714

Example 34.9.2, page no. 837

In [20]:
import math


def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Total no. of possible cases = C(9,3) = ",C(9,3)
print "Favourable outcomes = C(2,2)∗C(7,1)+C(3,2)∗C(6,1)+C(4,2)∗C(5,1) = ",C(2,2)*C(7,1)+C(3,2)*C(6,1)+C(4,2)*C(5,1)
print "Probability = ",float(C(2,2)*C(7,1)+C(3,2)*C(6,1)+C(4,2)*C(5,1))/C(9,3)
Total no. of possible cases = C(9,3) =  84
Favourable outcomes = C(2,2)∗C(7,1)+C(3,2)∗C(6,1)+C(4,2)∗C(5,1) =  55
Probability =  0.654761904762

Example 34.9.3, page no. 838

In [22]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Total no. of possible cases = C(9,3) = ",C(9,3)
print "Favourable outcomes = C(3,3)+C(4,3) = ",C(3,3)+C(4,3)
print "Probability = ",5./84
Total no. of possible cases = C(9,3) =  84
Favourable outcomes = C(3,3)+C(4,3) =  5
Probability =  0.0595238095238

Example 34.13, page no. 840

In [25]:
print "Probability of drawing an ace or spade or both from pack of 52 cards = 4/52+13/52−1/52= ",4+13-1/52
Probability of drawing an ace or spade or both from pack of 52 cards = 4/52+13/52−1/52=  17

Example 34.14.1, page no. 841

In [30]:
print "Probability of first card being a king = 4/52 = ",4./52
print "Probability of second card being a queen = 4/52 = ",4./52
print "Probability of drawing both cards in succession = 4/52∗4/52= ",(4./52)*(4./52)
Probability of first card being a king = 4/52 =  0.0769230769231
Probability of second card being a queen = 4/52 =  0.0769230769231
Probability of drawing both cards in succession = 4/52∗4/52=  0.00591715976331

Example 34.15.1, page no. 842

In [32]:
print "Probability of getting 7 in first toss and not getting it in second toss = 1/6∗5/6 = ",(1./6)*(5./6)
print "Probability of not getting 7 in first toss and getting it in second toss = 5/6∗1/6 = ",(5./6)*(1./6)
print "Required probability = 1/6∗5/6+5/6∗1/6 = ",((1./6)*(5./6))+((5./6)*(1./6))
Probability of getting 7 in first toss and not getting it in second toss = 1/6∗5/6 =  0.138888888889
Probability of not getting 7 in first toss and getting it in second toss = 5/6∗1/6 =  0.138888888889
Required probability = 1/6∗5/6+5/6∗1/6 =  0.277777777778

Example 34.15.2, page no. 842

In [33]:
print "Probability of not getting 7 in either toss = 5/6∗5/6 = ",(5./6)*(5./6)
print "Probability of getting 7 at least once = 1−5/6∗5/6 = ",1-(5./6)*(5./6)
Probability of not getting 7 in either toss = 5/6∗5/6 =  0.694444444444
Probability of getting 7 at least once = 1−5/6∗5/6 =  0.305555555556

Example 34.15.3, page no. 842

In [34]:
print "Probability of getting 7 twice = 1/6∗1/6 = ",(1./6)*(1./6)
Probability of getting 7 twice = 1/6∗1/6 =  0.0277777777778

Example 34.16, page no. 843

In [35]:
print "Probability of engineering subject being choosen = (1/3∗3/8)+(2/3∗5/8) = ",((1./3)*(3./8)) +((2./3)*(5./8))
Probability of engineering subject being choosen = (1/3∗3/8)+(2/3∗5/8) =  0.541666666667

Example 34.17, page no. 844

In [36]:
print "Probability of white ball being choosen = 2/6∗6/13+4/6∗5/13 = ",((2./6)*(6./13))+((4./6)*(5./13))
Probability of white ball being choosen = 2/6∗6/13+4/6∗5/13 =  0.410256410256

Example 34.18, page no. 844

In [38]:
print "Chances of winning of A=1/2+(1/2)ˆ2∗(1/2)+(1/2)ˆ4∗(1/2)+(1/2)ˆ6∗(1/2)+..= ",(1./2)/(1-(1./2)**2)
print "Chances of winning of B=1−chances of winning of A = ",1-(2/3)
Chances of winning of A=1/2+(1/2)ˆ2∗(1/2)+(1/2)ˆ4∗(1/2)+(1/2)ˆ6∗(1/2)+..=  0.666666666667
Chances of winning of B=1−chances of winning of A =  1

Example 34.19.1, page no. 845

In [40]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Total no. of possible outcomes = C(10,2) = ",C(10,2)
print "Favourable outcomes = 5*5 = ",5*5
print "P = ",25./45
Total no. of possible outcomes = C(10,2) =  45
Favourable outcomes = 5*5 =  25
P =  0.555555555556

Example 34.19.2, page no. 845

In [41]:
print "Total no. of possible outcomes = 10*10 = ",10*10
print "Favourable outcomes = 5*5+5*5 = ",5*5+5*5
print "P = ",50./100
Total no. of possible outcomes = 10*10 =  100
Favourable outcomes = 5*5+5*5 =  50
P =  0.5

Example 34.20, page no. 846

In [42]:
A = 1./4
B = 1./3
AorB = 1./2
AandB = A+B-AorB
print "Probability of A/B = AandB/B = ",AandB/B
print "Probability of B/A = AandB/A = ",AandB/A
print "Probability of AandBnot = A−AandB = ",A-AandB
print "Probability of A/Bnot = AandBnot/Bnot = ",(1./6)/(1-1./3)
Probability of A/B = AandB/B =  0.25
Probability of B/A = AandB/A =  0.333333333333
Probability of AandBnot = A−AandB =  0.166666666667
Probability of A/Bnot = AandBnot/Bnot =  0.25

Example 34.22, page no. 846

In [43]:
print "Probability of A hitting target = 3/5"
print "Probability of B hitting target = 2/5"
print "Probability of C hitting target = 3/4"
print "Probability that two shots hit = 3/5∗2/5∗(1−3/4)+2/5∗3/4∗(1−3/5)+3/4∗3/5∗(1−2/5) = "
print (3./5)*(2./5)*(1-3./4)+(2./5)*(3./4)*(1-3./5)+(3./4)*(3./5)*(1-2./5)
Probability of A hitting target = 3/5
Probability of B hitting target = 2/5
Probability of C hitting target = 3/4
Probability that two shots hit = 3/5∗2/5∗(1−3/4)+2/5∗3/4∗(1−3/5)+3/4∗3/5∗(1−2/5) = 
0.45

Example 34.23, page no. 847

In [44]:
print "Probability of problem not getting solved = 1/2∗2/3∗3/4 = ",(1./2)*(2./3)*(3./4)
print "Probability of problem getting solved = 1−(1/2∗2/3∗3/4) = ",1-((1./2)*(2./3)*(3./4))
Probability of problem not getting solved = 1/2∗2/3∗3/4 =  0.25
Probability of problem getting solved = 1−(1/2∗2/3∗3/4) =  0.75

Example 34.25, page no. 848

In [46]:
import sympy

print "Total frequency=integrate(f,x,0,2) =" 
n = sympy.integrate('x**3',('x',0,1))+sympy.integrate('(2-x)**3',('x',1,2))
print "u1 about origin = ",
u1 = (1/n)*(sympy.integrate('(x)*(x**3)',('x',0,1))+sympy.integrate('(x)*((2-x)**3)',('x',1,2)))
print u1
print "u2 about origin = ",
u2 = (1/n)*(sympy.integrate('(x**2)*(x**3)',('x',0,1))+sympy.integrate('(x**2)*((2-x)**3)',('x',1,2)))
print u2
print "Standard deviation = (u2−u1ˆ2)ˆ0.5= ",(u2-u1**2)**0.5
print "Mean deviation about the mean = (1/n)∗(integrate(|x−1|∗(xˆ3),x,0,1)+integrate(|x−1|∗((2−x)ˆ3),x,1,2))"
print (1/n)*(sympy.integrate('(1-x)*(x**3)',('x',0,1))+sympy.integrate('(x-1)*((2-x)**3)',('x',1,2)))
Total frequency=integrate(f,x,0,2) =
u1 about origin =  1
u2 about origin =  16/15
Standard deviation = (u2−u1ˆ2)ˆ0.5=  0.258198889747161
Mean deviation about the mean = (1/n)∗(integrate(|x−1|∗(xˆ3),x,0,1)+integrate(|x−1|∗((2−x)ˆ3),x,1,2))
1/5

Example 34.26, page no. 849

In [47]:
print "Probability = (0.45∗0.03)/(0.45∗0.03+0.25∗0.05+0.3∗0.04 = ",(0.45*0.03)/(0.45*0.03+0.25*0.05+0.3*0.04)
Probability = (0.45∗0.03)/(0.45∗0.03+0.25∗0.05+0.3∗0.04 =  0.355263157895

Example 34.27, page no. 849

In [49]:
print "Probability = (1/3∗2/6∗3/5)/(1/3∗2/6∗3/5+1/3∗1/6∗2/5+1/3∗3/6∗1/5) = ",
print ((1./3)*(2./6)*(3./5))/((1./3)*(2./6)*(3./5))+((1./3)*(1./6)*(2./5))+((1./3)*(3./6)*(1./5))
Probability = (1/3∗2/6∗3/5)/(1/3∗2/6∗3/5+1/3∗1/6∗2/5+1/3∗3/6∗1/5) =  1.05555555556

Example 34.28, page no. 850

In [50]:
import numpy

print "Probability of no success = 8/27 "
print "Probability of a success = 1/3 "
print "Probability of one success = 4/9"
print "Probability of two success = 2/9"
print "Probability of three success = 2/9"
A = numpy.array([[0,1,2,3],[8./27,4./9,2./9,1./27]])
print "mean=sum of i∗pi = ",
print A[0,0]*A[1,0]+A[0,1]*A[1,1]+A[0,3]*A[1,3]+A[0,2]*A[1,2]
print "sum of i∗piˆ2 = ",
print A[0,0]**2*A[1,0]+A[0,1]**2*A[1,1]+A[0,3]**2*A[1,3]+A[0,2]**2*A[1,2]
print "variance = (sum of i∗piˆ2)−1= ",
print A[0,0]**2*A[1,0]+A[0,1]**2*A[1,1]+A[0,3]**2*A[1,3]+A[0,2]**2*A[1,2]-1
Probability of no success = 8/27 
Probability of a success = 1/3 
Probability of one success = 4/9
Probability of two success = 2/9
Probability of three success = 2/9
mean=sum of i∗pi =  1.0
sum of i∗piˆ2 =  1.66666666667
variance = (sum of i∗piˆ2)−1=  0.666666666667

Example 34.29, page no. 851

In [53]:
import sympy,numpy

k = sympy.Symbol('k')
A =numpy.array([[0,1,2,3,4,5,6],[k,3*k,5*k,7*k,9*k,11*k,13*k]])
print "Sumof all pi = 1 "
print "Hence ,"
k = 1./49
print "p(x<4) = ",
a = A[1,0]+A[1,1]+A[1,3]+A[1,2]
print a.evalf()
print "p(x>=5) = ",
b = A[1,5]+A[1,6]
print b.evalf()
print "p(3<x<=6) = ",
c = A[1,4]+A[1,5]+A[1,6]
print c.evalf()
print "p(x<=2) = ",
e = A[1,0]+A[1,1]+A[1,2]
print e.evalf()
Sumof all pi = 1 
Hence ,
p(x<4) =  16.0*k
p(x>=5) =  24.0*k
p(3<x<=6) =  33.0*k
p(x<=2) =  9.0*k

Example 34.30, page no. 853

In [57]:
import sympy,numpy,math

k = sympy.Symbol('k')
A =numpy.array([[0,1,2,3,4,5,6,7],[0,k,2*k,2*k,3*k,k**2,2*k**2,7*k**2+ k]])
print "Sum of all pi = 1"
print "Hence,"
k = 1./10
print "p(x<6) = ",
a = A[1,0]+A[1,1]+A[1,3]+A[1,2]+ A[1,3]+A[1,4]+A[1,6]
print a.evalf()
print "p(x>=6) = ",
b = A[1,6]+A[1,7]
print b.evalf()
print "p(3<x<5) = ",
c = A[1,1]+A[1,2]+A[1,3]+A[1,4]
print c.evalf()
 Sum of all pi = 1
Hence,
p(x<6) =  2.0*k**2 + 10.0*k
p(x>=6) =  9.0*k**2 + k
p(3<x<5) =  8.0*k

Example 34.31, page no. 853

In [62]:
import sympy,numpy,math

y = sympy.Symbol('y')
f = math.e**(-y)
print "Clearly, f>0 for every x in (1,2) and integrate(f,x,0,numpy.inf)= ",
print sympy.integrate(math.e**(-y),('y',0,sympy.oo))
print "Required probability=p(1<=x<=2)= integrate(f,x,1,2) = ",
print sympy.integrate(math.e**(-y),('y',1,2))
print "Cumulative probability function f(2)=integrate(f,x,−%inf,2) = ",
print sympy.integrate(math.e**(-y),('y',0,2))
Clearly, f>0 for every x in (1,2) and integrate(f,x,0,numpy.inf)=  1.00000000000000
Required probability=p(1<=x<=2)= integrate(f,x,1,2) =  0.232544157934830
Cumulative probability function f(2)=integrate(f,x,−%inf,2) =  0.864664716763387

Example 34.33, page no. 854

In [65]:
import sympy

k = sympy.Symbol('k')
print "Total probability = integrate(f,x,0,6)="
p = sympy.integrate('k*x',('x',0,2))
q = sympy.integrate('2*k',('x',2,4))
r = sympy.integrate('-k*x+6*k',('x',4,6))
print p
print q
print r
Total probability = integrate(f,x,0,6)=
2*k
4*k
2*k

Example 34.34, page no. 854

In [4]:
import numpy

A = numpy.array([[-3.,6.,9.],[1./6,1./2,1./3]])
print "First row of A displays the value of x"
print "The second row of x displays the probability of corresponding to x"
print "E(x) = ",
c = A[0,0]*A[1,0]+A[0,1]*A[1,1]+A[0,2]*A[1,2]
print c
print "E(x)ˆ2 = ",
b = A[0,0]**2*A[1,0]+A[0,1]**2*A[1,1]+A[0,2]**2*A[1,2]
print b
print "E(2∗x+1)^2=E(4∗xˆ2+4∗x+1) = ",4*b+4*c+1
First row of A displays the value of x
The second row of x displays the probability of corresponding to x
E(x) =  5.5
E(x)ˆ2 =  46.5
E(2∗x+1)^2=E(4∗xˆ2+4∗x+1) =  209.0

Example 34.35, page no. 855

In [6]:
import sympy

print "Total frequency=integrate(f,x,0,2)= "
n = sympy.integrate('x**3',('x',0,1))+sympy.integrate('(2-x)**3',('x',1,2))
print "u1 about origin = "
u1 = (1/n)*(sympy.integrate('(x)*(x**3)',('x',0,1))+sympy.integrate('(x)*((2-x)**3)',('x',1,2)))
print "u2 about origin = "
u2 = (1/n)*(sympy.integrate('(x**2)*(x**3)',('x',0,1))+sympy.integrate('(x**2)*((2-x)**3)',('x',1,2)))
print "standard deviation = (u2−u1ˆ2)ˆ0.5 = ",(u2-u1**2)**0.5
print "Mean deviation about the mean = (1/n)∗(integrate|x−1|∗(xˆ3),x,0,1)+integrate(|x−1|∗((2−x)ˆ3),x,1,2))"
print (1/n)*(sympy.integrate('(1-x)*(x**3)',('x',0,1))+sympy.integrate('(x-1)*((2-x)**3)',('x',1,2)))
Total frequency=integrate(f,x,0,2)= 
u1 about origin = 
u2 about origin = 
standard deviation = (u2−u1ˆ2)ˆ0.5 =  0.258198889747161
Mean deviation about the mean = (1/n)∗(integrate|x−1|∗(xˆ3),x,0,1)+integrate(|x−1|∗((2−x)ˆ3),x,1,2))
1/5

Example 34.38, page no. 857

In [8]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Probability that exactly two will be defective = C(12,2)∗(0.1)ˆ2∗(0.9)ˆ10 = ",C(12,2)*(0.1)**2*(0.9)**10
print "Probability that at least two will be defective = 1−(C(12,0)∗(0.9)ˆ12+C(12,1)∗(0.1)∗(0.9)ˆ11) = ",
print 1-(C(12,0)*(0.9)**12+C(12,1)*(0.1)*(0.9)**11)
print "The probability that none will be defective = C(12,12)∗(0.9)ˆ12 = ",C(12,12)*(0.9)**12
 Probability that exactly two will be defective = C(12,2)∗(0.1)ˆ2∗(0.9)ˆ10 =  0.230127770466
Probability that at least two will be defective = 1−(C(12,0)∗(0.9)ˆ12+C(12,1)∗(0.1)∗(0.9)ˆ11) =  0.340997748211
The probability that none will be defective = C(12,12)∗(0.9)ˆ12 =  0.282429536481

Example 34.39, page no. 858

In [11]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Probability of 8 heads and 4 tail sin 12 trials = p(8) = C(12,8)∗(1/2)ˆ8∗(1/2)ˆ4= ",C(12.,8.)*(1./2)**8*(1./2)**4
print "The expected no. of such cases in 256 sets = 256∗p(8) = ",256*(495./4096)
Probability of 8 heads and 4 tail sin 12 trials = p(8) = C(12,8)∗(1/2)ˆ8∗(1/2)ˆ4=  0.120849609375
The expected no. of such cases in 256 sets = 256∗p(8) =  30.9375

Example 34.40, page no. 859

In [14]:
import math

def C(a,b):
    x = math.factorial(a)/(math.factorial(b)*math.factorial(a-b))
    return x
print "Probability of a defective part = 2/20 =0.1 "
print "Probability of a non defective part = 0.9 "
print "Probabaility of atleast three defectives in a sample = ",
print 1-(C(20.,0.)*(0.9)**20+C(20.,1.)*(0.1)*(0.9)**19+C(20.,2.)*(0.1)**2*(0.9)**18)
print "No. of sample shaving three defective parts = 1000∗0.323 = ",1000*0.323
Probability of a defective part = 2/20 =0.1 
Probability of a non defective part = 0.9 
Probabaility of atleast three defectives in a sample =  0.323073194811
No. of sample shaving three defective parts = 1000∗0.323 =  323.0