Chapter 3: Probability

Example, Page-47

In [19]:
#Variable declaration
Sample = set([(0,0),(0,1),(1,0),(1,1),(0,2),(2,0)])
C = set([(1,0),(0,1)])
D = set([(0,0),(0,1),(0,2)])
E = set([(0,0),(1,1)])

#Calculation
from scipy import *
from pylab import *
from sets import Set

un = C|E
inter = C & D
D_comp = Sample - D

# Result
print "Part A: ",un
print "Part B: ",inter
print "Part C: ",D_comp
Part A:  set([(0, 1), (1, 0), (0, 0), (1, 1)])
Part B:  set([(0, 1)])
Part C:  set([(2, 0), (1, 0), (1, 1)])

Example, page-50

In [20]:
#Variable declaration
n=25   #total members

#Calculation
from scipy import *
from pylab import *
def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)

def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

ways=comb(25,1) * comb(24,1)    # total number of methods

#Results
print "total ways: ",int(ways)
total ways:  600

Example, Page-50

In [2]:
#Variable declaration
n=12   #total Questions

#Calculation
from scipy import *
from pylab import *

# Each question can be answered in 2 ways,
ways = pow(2,float(n))    # total number of methods

#Results
print "total ways: ",int(ways)
total ways:  4096

Example, Page-51

In [22]:
#Variable declaration
n1 = 4    # Total operators
n2 = 3    # Total Machines
n3 = 8    # Test specimens for each pair

#Calculation
Total_pairs = n1*n2
Total_specimens = n1*n2*n3

#Results
print "Total Pairs: ",Total_pairs
print "Total Test specimens: ",Total_specimens
Total Pairs:  12
Total Test specimens:  96

Example, Page-52

In [3]:
#Variable declaration
n = 5    # Total chips
r = 5   # Total positions

#Calculation
from scipy import *
from pylab import *

def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)

def perm(n,r):
    return fact(n)/(fact(n-r))

ways=perm(5,5)   # total number of methods

#Results
print "Total methods: ",int(ways)
Total methods:  120

Example, page-53

In [4]:
#Variable declaration
n=20    # Total Assistants
r=3     # Assistants to be chosen

#Calculation
from scipy import *
from pylab import *

def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)

def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

ways=comb(20,3)   # total number of methods

#Results
print "Total methods: ",int(ways)
Total methods:  1140

Example, Page-53

In [5]:
#Variable declaration
n = 15    # Total machines
r = 3   

#Calculation
from scipy import *
from pylab import *

def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)

def comb(n,r):
    return fact(n)/(fact(r)*fact(n-r))

ways=comb(n,r)   # total number of methods

#Results
print "Total methods: ",int(ways)
Total methods:  455

Example, page-53

In [6]:
#Variable declaration
n_chem=7    # Total chemists
r_chem=2    # chemists to be selected
n_phy=9     # Total physicists
r_phy=3     # physicists to be selected

#Calculation
from scipy import *
from pylab import *

def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)
    
def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

ways=comb(n_chem,r_chem) * comb(n_phy,r_phy)   # total number of methods

#Results
print "Total methods: ",int(ways)
Total methods:  1764

Example, page-56

In [27]:
#Variable declaration
n=52                                  # Total number of cards
m=4                                   # Total number of ace

#Calculation
from scipy import *
from pylab import *
def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)
    
def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

prob = float(comb(m,1))/comb(n,1)     # Required probability
prob=round(prob,3)                    # i.e. 1/13

#Results
print "Required probability: ",prob,"or 1/13"
Required probability:  0.077 or 1/13

Example, Page-56

In [28]:
#Variable declaration
n1 = 10    # Total machines
n2 = 8     # Working Machines
r = 2      # To be selected

#Calculation
from scipy import *
from pylab import *

def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)

def comb(n,r):
    return fact(n)/(fact(r)*fact(n-r))

p1 = comb(n2,r)/float(comb(n1,r))   
p2 = comb(n2,1)*comb(n1-n2,1)/float(comb(n1,r))

#Results
print "Probability: Both motors works: ",round(p1,3)
print "Probability: Exactly one works: ",round(p2,3)
Probability: Both motors works:  0.622
Probability: Exactly one works:  0.356

Example, page-57

In [32]:
#Variable declaration
n=300                                        # Total insulators
m=294       				     # insulators having capacity of withstand shock

#Calculation
from scipy import *
from pylab import *

def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)
    
def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

prob = float(comb(m,1)) / comb(n,1)          # Resultant probability
prob=round(prob,3)

#Results
print "Required probability: ",prob
Required probability:  0.98

Example, Page-59

In [33]:
#Variable declaration

# Part A
p1 = 1.0/3
p2 = 1.0/3
p3 = 1.0/3
print "[Part:A]  p1+p2+p3 = 1 so it is permissible"

# Part B
p1 = 0.64
p2 = 0.38
p3 = -0.02
print "[Part:B]  p3 < 0 so it is not permissible"

# Part C
p1 = 0.35
p2 = 0.52
p3 = 0.26
print "[Part:C]  p1+p2+p3 > 1 so it is not permissible"

# Part D
p1 = 0.57
p2 = 0.24
p3 = 0.19
print "[Part:D]  p1+p2+p3 = 1 so it is not permissible"
[Part:A]  p1+p2+p3 = 1 so it is permissible
[Part:B]  p3 < 0 so it is not permissible
[Part:C]  p1+p2+p3 > 1 so it is not permissible
[Part:D]  p1+p2+p3 = 1 so it is not permissible

Example, Page-60

In [35]:
#Variable declaration
l = [0.07, 0.12, 0.17, 0.32, 0.21, 0.11]

#Calculation
from scipy import *
from pylab import *

p1 = l[0]+l[1]+l[2]+l[3]   
p2 = l[3]+l[4]+l[5]

#Results
print "Part(a): Probability: ",p1
print "Part(b): Probability: ",p2
Part(a): Probability:  0.68
Part(b): Probability:  0.64

Example, page-63

In [36]:
#Variable Declaration
p1=0.87       # probability of Repair on the engine
p2=0.36       # probability of drive train
p3=0.29       # probability of rapair as well as drive train

#Calculation
from scipy import *
from pylab import *
P = p1+p2-p3    # Required probability

#Results
print "probability of at least one kind of repair: ",P
probability of at least one kind of repair:  0.94

Example, page-68

In [37]:
#Variable Declaration
p1=0.81     # High fidelity
p2=0.18     # High fidelity and high selectivity

#Calculation
from scipy import *
from pylab import *
P = p2/p1    # i.e. required probability
P=round(P,3)   # i.e. 2/9

#Results
print "Required probability: ",P,"or 2/9"
Required probability:  0.222 or 2/9

Example, page-69

In [38]:
#Variable Declaration
n=20     # Total workers
m=8      # favourable condition
r=2      # workers to be selected

#Calculation
def fact(n):
    """returns factorial of a given number"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)
    
def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))
P = float(comb(8,2))/comb(20,2)         # Required probability=Favourable cases/Total cases
P=round(P,3)       # i.e. 14/95

#Results
print "required probability is: ",P,"or 14/95"
required probability is:  0.147 or 14/95

Example, Page-69

In [39]:
#Variable declaration
p = 0.5    # Probability of getting head in one flip

#Calculation
from scipy import *
from pylab import *

#Results
print "Probability: ",p*p
Probability:  0.25

Example, page-69

In [8]:
#Variable Declaration
n = 52        # Total cards
m = 4         # Total aces in a deck
r = 2         # Cards to be drawn

#Calculation
def fact(n):
    """returns factorial of a given number"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)
    
def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

p1 = (float(comb(m,1))/comb(n,1)) * (float(comb(m,1))/comb(n,1))   # Required probability for (a)
p2 = (float(comb(m,1))/comb(n,1)) * (float(comb(m-1,1))/comb(n-1,1))   # Required probability for (b)
p1=round(p1,3)    # i.e. (4/52)*(4/52)
p2=round(p2,3)    # i.e. (4/52)*(3/51)

#Results
print "probability of two aces with replacement: ",p1,"or 1/169"
print "probability of two aces without replacement: ",p2,"or 1/221"
probability of two aces with replacement:  0.006 or 1/169
probability of two aces without replacement:  0.005 or 1/221

Example, page-70

In [13]:
#Variable Declaration
p1 = 0.65   # probability of event C
p2 = 0.40   # probability of event D
p3 = 0.40   # probability of event C and D both Occuring symultanously

#Calculation
# two events are independent if P(C)*P(D)=P(C and D)
Mul = p1*p2

#Results
if(Mul==p3):
    print p1*p2,"is equal to",p3,"Thus C and D are INDEPENDENT EVENTS"
else:
    print p1*p2,"is not equal to",p3,"Thus C and D are DEPENDENT EVENTS"
0.26 is not equal to 0.4 Thus C and D are DEPENDENT EVENTS

Example, Page-70

In [14]:
#Variable Declaration
p1 = 0.8   # probability of event A
p2 = 0.7   # probability of event B

#Calculation
P_Intersection = p1*p2

#Results
print "P(A and B):",P_Intersection
P(A and B): 0.56

Example, page-70

In [17]:
#Variable Declaration
n = 6        # Total possible outcomes after one roll
m = 5        # favourable outcomes of not getting 6 for each roll

#Calculation
from scipy import *
from pylab import *
def fact(n):
    """returns factorial of number n"""
    if(n==1 or n==0):
        return 1
    else:
        return n*fact(n-1)
    
def comb(n,r):
    """returns number of total combination of selecting "r" items out of "n" """
    return fact(n)/(fact(r)*fact(n-r))

P = float(comb(5,1))/comb(6,1)     # probability of not getting 6 in one roll
P = P**4                           # required probability
P=round(P,3)   # equal to 625/1296

#Results
print "Required probability:",P,"or 625/1296"
Required probability: 0.482 or 625/1296

Example, Page-70

In [43]:
#Variable declaration
p = 0.99    # Probability of specimen to be in compliance

#Calculation
from scipy import *
from pylab import *

#Results
print "Probability of both in compliance: ",round(p*p,4)
print "Probability of both in compliance: ",round(pow(p,104.0),2)
Probability of both in compliance:  0.9801
Probability of both in compliance:  0.35

Example, page-74

In [44]:
#Variable Declaration
# Event B-service incomplete
# Event A1- breakdown service by Janet; Event A2- breakdown service by Tom; Event A3- breakdown service by Georgia; Event A4- breakdown service by Peter
# Event B1- incomplete repair by Janet; Event B2- incomplete repair by Tom; Event B3- incomplete repair by Georgia; Event B4- incomplete repair by Peter
# we need to find P(A1/B) i.e.=(P(A1).P(B/A1))/(P(A1).P(B/A1)+P(A2).P(B/A2)+P(A3).P(B/A3)+P(A4).P(B/A4))

#Calculation
from scipy import *
from pylab import *
P = (0.20 * .05)/((0.20*.05)+(0.60*.10)+(0.15*.10)+(0.05*.05))         # Required probability
P=round(P,3)

#Results
print "Required probability: ",P
Required probability:  0.114