2 Permutations, combinations, and discrete probability

Example 01:page 69

In [1]:
print "This is to find the number of ways to schedule three exams in five days such that no two exams fall on the same day"
first_exam=5#number of available days for the first exam
second_exam=4#number of available days for the second exam
third_exam=3#number of available days for the third exam
total=first_exam*second_exam*third_exam#number of combinations
print "Total number of ways is :",total
This is to find the number of ways to schedule three exams in five days such that no two exams fall on the same day
Total number of ways is : 60

Example 02:Page 69

In [2]:
print "To find the number of ways in which seven rooms can be allocated to four programmers"
a=7 # The number of possible rooms for first programmer
b=6 # The number of possible rooms for second programmer
c=5 # The number of possible rooms for third programmer
d=4 # The number of possible rooms for fourth programmer
res=a*b*c*d
print "The assignment can be made in ",res,"different ways"
To find the number of ways in which seven rooms can be allocated to four programmers
The assignment can be made in  840 different ways

Example 03:Page 69

In [3]:
print "Number of four digit decimal numbers which have no repeated digits"
thousands_place=9 # Only nine digits can be placed there. zero cannot be placed there as it will become a three digit number
hundreds_place=9 #As one slot is already filled with a number, the remaining nine digits can only be placed here
tens_place=8 # As two digits have already been used, there are only eight more digits for this place
ones_place=7 # Three digits have occupied three places and only seven digits remain more
total=thousands_place*hundreds_place*tens_place*ones_place
print total," possible four digit decimal numbers can be formed without any repetition of digits"
Number of four digit decimal numbers which have no repeated digits
4536  possible four digit decimal numbers can be formed without any repetition of digits

Example 04:Page 70

In [4]:
import math
print "To find the number of ways in which we can make up strings of four distinct letters followed by three distinct digits"
def permutations(n,r):
    res=math.factorial(n)/math.factorial(n-r)#Mathematical formula for permutations
    return res
print "Total number of possible ways are",permutations(26,4)*permutations(10,3)
To find the number of ways in which we can make up strings of four distinct letters followed by three distinct digits
Total number of possible ways are 258336000

Example 05:Page 70

In [5]:
print "To schedule three examinations within a five day period with no restrictions on the number of examinations scheduled each day"
print "The total number of ways are", pow(5,3)
To schedule three examinations within a five day period with no restrictions on the number of examinations scheduled each day
The total number of ways are 125

Example 08:Page 71

In [6]:
print "We print all five-digit numbers on slips of paper with one number on each slip."
print "We have",pow(10,5),"distinct five-digit numbers" 
print "Among these numbers",pow(5,5),"of them can be read either right side up or upside down"#Since the digits 0,1,6,8,9 become 0,1,9,8,6 when they are read upside down, there are pairs of numbers that can share the same slip if the slips are read right side up or upside down
print "There are",pow(5,5)-(3*pow(5,2)),"numbers that can be read either right side up or upside down but will read differently.So,hey can be divided into pairs so that every pair of numbers can share one slip."
print "The total number of distinct slips we need is",pow(10,5)-((pow(5,5)-3*pow(5,2))/2)
We print all five-digit numbers on slips of paper with one number on each slip.
We have 100000 distinct five-digit numbers
Among these numbers 3125 of them can be read either right side up or upside down
There are 3050 numbers that can be read either right side up or upside down but will read differently.So,hey can be divided into pairs so that every pair of numbers can share one slip.
The total number of distinct slips we need is 98475

Example 09:Page 72

In [7]:
print "There are",pow(4,7),"different schedules for a seven day period with four subjects"
print "To find the number of schedules which devote at least one day to each subject namely mathematics, physics, chemistry,and economics."
print "A1 denote the set of schedules in which mathematics is never included"
print "A2 denote the set of schedules in which physics is never included"
print "A3 denote the set of schedules in which chemistry is never included"
print "A4 denote the set of schedules in which economics is never included"
print "A1_union_A2_union_A3_union_A4 is the set of schedules in which one or more  of the subjects is not included. We obtain |A1 U A2 U A3 U A4|=",
A1=A2=A3=A4=pow(3,7)
A1_intersection_A2=A1_intersection_A3=A1_intersection_A4=A2_intersection_A3=A2_intersection_A4=A3_intersection_A4=pow(2,7)
A1_intersection_A2_intersection_A3=A1_intersection_A2_intersection_A4=A1_intersection_A3_intersection_A4=A2_intersection_A3_intersection_A4=1
A1_intersection_A2_intersection_A3_intersection_A4=0
A1_union_A2_union_A3_union_A4=A1+A2+A3+A4-A1_intersection_A2-A1_intersection_A3-A1_intersection_A4-A2_intersection_A3-A2_intersection_A4-A3_intersection_A4+A1_intersection_A2_intersection_A3+A1_intersection_A2_intersection_A4+A1_intersection_A3_intersection_A4+A2_intersection_A3_intersection_A4
print A1_union_A2_union_A3_union_A4
print "The number of schedules in which all subjects will be included",pow(4,7)-A1_union_A2_union_A3_union_A4
There are 16384 different schedules for a seven day period with four subjects
To find the number of schedules which devote at least one day to each subject namely mathematics, physics, chemistry,and economics.
A1 denote the set of schedules in which mathematics is never included
A2 denote the set of schedules in which physics is never included
A3 denote the set of schedules in which chemistry is never included
A4 denote the set of schedules in which economics is never included
A1_union_A2_union_A3_union_A4 is the set of schedules in which one or more  of the subjects is not included. We obtain |A1 U A2 U A3 U A4|= 7984
The number of schedules in which all subjects will be included 8400

Example 10:Page 73

In [8]:
import math
num_office=12 # Total number of offices to be painted
num_of_green=3 # Number of offices to be painted green
num_of_pink=2 # Number of offices to be painted pink
num_of_yellow=2 # Number of offices to be painted yellow
num_of_white=5 # Number of offices to be painted white
res=math.factorial(num_office)/(math.factorial(num_of_green)*math.factorial(num_of_pink)*math.factorial(num_of_yellow)*math.factorial(num_of_white))#Mathematical implementation of permutation with similarities
print "Total number of ways to paint 12 offices so that 3 of them are green,2 of them are pink,2 of them are yellow and the remaining with white are ", res
Total number of ways to paint 12 offices so that 3 of them are green,2 of them are pink,2 of them are yellow and the remaining with white are  166320

Example 11:Page 73

In [9]:
import math
length=5#Total length of the message
num_of_dashes=3#Number of dashes in message 
num_of_dots=2#Number of dots in the message
res=math.factorial(length)/(math.factorial(num_of_dashes)*math.factorial(num_of_dots))
print "The number of different messages that can be represented by a sequence of three dashes and two dots :",res
    
The number of different messages that can be represented by a sequence of three dashes and two dots : 10

Example 12:Page 74

In [10]:
import math
days=7 # Number of days in a week
spaghetti=3 # Number of days a housekeeper wants to serve speghetti in a week
res=math.factorial(days)/(math.factorial(spaghetti)*math.factorial(days-spaghetti))#Mathematical implementation of permutation swith similarities
print "Number of possible ways in which a house keeper can serve spagetti dinner three times each week is",res
Number of possible ways in which a house keeper can serve spagetti dinner three times each week is 35

Example 13:Page 74

In [12]:
import math
length=32 # Total length of binary sequence
num_of_one=7 # Total number of places to be filled with one
def c(n,r):
    res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))
    return res
print "Total number of combinations of binary sequences of length 32 in each of which there are exactly seven 1s is ",c(length,num_of_one)
# Because we can view the problem as placing 7 ones in 32 numbered boxes(and then fill the empty boxes with 0s)
Total number of combinations of binary sequences of length 32 in each of which there are exactly seven 1s is  3365856

Example 14:Page 74

In [13]:
import math

total_senators=11 # Total number of senators
committee_mem=5 # Number of members in the committee

def c(n,r):
    res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))
    return res

#Finding number of combinations for selecting a committee of 5 members from 11 senators
print "The number of ways to select a committee of 5 members among 11 senators are",c(total_senators,committee_mem);

# Finding number of combinations to select a committee of five members so that a particular senator , senator A is always included
print "The number of ways to select a committee of five members so that a particular senator , senator A is always included are", c(total_senators-1,committee_mem-1);
# Since senator A is always included in the committee, we need to find combinations for the remaining 4 using remaining 10 senators

# Finding number of combinations to select a committee of five members so that a particular senator , senator A is always excluded
print "The number of ways to select a committee of five members so that a particular senator , senator A is always excluded are", c(total_senators-1,committee_mem);
# Since senator A is alway excluded from the committee, the committee members are selected from the remaining 10 senators


print "To find number of combinations to select a committee of five members so that at least one of senator A and senator B will be included"

both_A_and_B=c(total_senators-2,committee_mem-2)
print "Number of selections including both senator A and senator B ",both_A_and_B
# Since both senator A and senator B are already included, we need to find combinations for the remaining 3 committee members using remaining 9 senators

#Number of ways to select a committee of five members including senator A and excluding senator B
A_but_not_B=c(total_senators-2,committee_mem-1)
print "Number of selections including senator A and excluding senator B ",A_but_not_B
# Since senator A is included and senator B is always excluded, find combinations for remaining 4 committee members using remaining 9 senators

#Finding number of combinations to select a committee of five members including senator B and excluding senator A
B_but_not_A=c(total_senators-2,committee_mem-1)
print "Number of selections including senator B and excluding senator A ",B_but_not_A
# Since senator B is included and senator A is always excluded, find combinations for remaining 4 committee members using remaining 9 senators

res=both_A_and_B+A_but_not_B+B_but_not_A
print "Number of ways to select a committee of five members so that at least one of senator A and senator B will be included",res

print "Alternative method"

print "Number of ways to select a committee of five members so that at least one of senator A and senator B will be included",c(total_senators,committee_mem)-c(total_senators-2,committee_mem)


print "By applying principle of inclusion and exclusion"
#A1 denotes the set of ways of selection that include senator A
#A2 denotes the set of ways of selection that include senator B
A1=c(total_senators-1,committee_mem-1)
A2=c(total_senators-1,committee_mem-1)
A1_intersection_A2=c(total_senators-2,committee_mem-2)
print "|A1 U A2|",A1+A2-A1_intersection_A2
The number of ways to select a committee of 5 members among 11 senators are 462
The number of ways to select a committee of five members so that a particular senator , senator A is always included are 210
The number of ways to select a committee of five members so that a particular senator , senator A is always excluded are 252
To find number of combinations to select a committee of five members so that at least one of senator A and senator B will be included
Number of selections including both senator A and senator B  84
Number of selections including senator A and excluding senator B  126
Number of selections including senator B and excluding senator A  126
Number of ways to select a committee of five members so that at least one of senator A and senator B will be included 336
Alternative method
Number of ways to select a committee of five members so that at least one of senator A and senator B will be included 336
By applying principle of inclusion and exclusion
|A1 U A2| 336

Example 15:Page 75

In [14]:
import math
print "To find the total number of line segments the diagonals are divided into by their intersections"
def c(n,r):
    res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))
    return res
nodes=10#Decagon has 10 vertex points
pair=2#Denotes pair of edges to form a disgonal
num_of_diagonals=(c(nodes,pair)-nodes)
print "Number of diagonals :",num_of_diagonals
#There are c(10,2) straight lines joining c(10,2) pairs of vertices. But since 10 of them are sides of the decagon, we subtract 10
num_of_intersections=c(nodes,4)
print "Total number of intersections",num_of_intersections
# Since for every 4 vertices we can count exactly one intersection between the diagonals
print "Total number of line segments",(num_of_diagonals+(2*num_of_intersections))
#Since a diagonal is divided into k+1 line segments when there are k intersecting points lying on it, and since each intersecting point lies on two diagonals
To find the total number of line segments the diagonals are divided into by their intersections
Number of diagonals : 35
Total number of intersections 210
Total number of line segments 455

Example 16:Page 76

In [15]:
import math
def c(n,r):
    res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Mathematical implementation of combinations
    return res
days=7#Total days in a week
choice=3#Number of days to be selected
print "The number of ways to choose three out of seven days(with repititions allowed) is c(7+3-1,3)=c(9,3)=", c(days+choice-1,choice)
print "The number of ways to choose seven out of three days(with repititions necessarily allowed) is c(3+7-1,7)=c(9,7)=", c(choice+days-1,days)
The number of ways to choose three out of seven days(with repititions allowed) is c(7+3-1,3)=c(9,3)= 84
The number of ways to choose seven out of three days(with repititions necessarily allowed) is c(3+7-1,7)=c(9,7)= 36

Example 17:Page 76

In [16]:
import math
total=7#Total number of diferent markings in a domino
squares=2#Number of squares in a domino
def c(n,r):
    res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))
    return res
print "The number of distinct dominoes in a set is C(7+2-1,2)=C(8,2)=",c(total+squares-1,squares)
#Equivalent to selecting two subjects from seven objects "one","two","three","four","five","six","blank"
The number of distinct dominoes in a set is C(7+2-1,2)=C(8,2)= 28

Example 18:page 76

In [17]:
import math
dice=3#Number of dice being rolled
num=6#Total number of outcomes in one dice
def C(n,r):
    return math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Formula to calculate combinations
print "When three dice are rolled, the number of different outcomes are C(6+3-1,3)=C(8,3)=",C(num+dice-1,dice),"because rolling three dice is equivalent to selecting three numbers from six numbers 1,2,3,4,5,6, with repetitions allowed"
#Since it is equivalent to selecting 3 numbers from 6 numbers namely 1,2,3,4,5,6 with repetitions allowed
When three dice are rolled, the number of different outcomes are C(6+3-1,3)=C(8,3)= 56 because rolling three dice is equivalent to selecting three numbers from six numbers 1,2,3,4,5,6, with repetitions allowed

Example 19:Page 77

In [18]:
import math
def C(n,r):
    res= math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Formula to calculate combinations
    return res
moves=14#total possible moves in both directions
eastward=7#Possible number of eastward moves in total
northward=7#Possible number of northward moves in total
print "The number of different paths for a rook to move from the southwest corner of a chessboard to the northwest corner by moving eastward and northward only."
print "0 denote an eastward step and 1 denote a northward step, the number of paths is equal to the number of ways of arranging seven 0s and seven 1s, which is",math.factorial(14)/(math.factorial(7)*math.factorial(7))
#Formula of permutations involving indistinguishable objects ie.,7 similar eastward moves and 7 similar northward moves
#This is the same as that of placing seven indistinguishable balls in four distinct boxes with no box left empty.
east_move=4#Number of northward moves
north_move=3#Number of eastward moves
num=C(east_move+north_move-1,north_move)#Number of ways of making up a path with four eastward moves
num1=C(north_move+east_move-1,east_move)#Number of ways of making up a path with three northward moves
print "Number of ways of making up a path with four eastward moves is ",num#Same as that of placing seven indistinguishable balls in four boxes with no box left empty
print "Number of ways of making up a path with three northward moves is ",num1
print "Therefore, the answer to our question is ",num,"*",num1,"=",num*num1
The number of different paths for a rook to move from the southwest corner of a chessboard to the northwest corner by moving eastward and northward only.
0 denote an eastward step and 1 denote a northward step, the number of paths is equal to the number of ways of arranging seven 0s and seven 1s, which is 3432
Number of ways of making up a path with four eastward moves is  20
Number of ways of making up a path with three northward moves is  15
Therefore, the answer to our question is  20 * 15 = 300

Example 20:Page 77

In [33]:
import math
chairs=12#Total number of chairs
boys=5#Number of boys to be seated
objects=6#Number of differenr objects
def C(n,r):
    return math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Mathematical implementation of combinations
print "To determine the number of ways to seat five boys in a row of 12 chairs.The number of arrangements would be",math.factorial(chairs)/math.factorial(chairs-boys)#Because it is similar to arranging 12 objects which are of 6 different types ie.five boys are of different kind each and the remaining unoccupied chairs are of a kind
print "Alternative way:"
print "Suppose, we arrange the first five boys in a row and then distribute the seven unoccupied chairs arbitrarily either between any two boys or at the two ends. The distribution problem then becomes that of placing seven balls of the same color in six boxes. Thus, the number of ways to do is 5!*C(6+7-1,7)=5!*(12!/7!*5!)=12!/7!=",math.factorial(boys)*C(objects+(chairs-boys)-1,(chairs-boys))
print "Suppose, we want to seat the boys so that no two boys are next to each other."
print "5!*C(6+3-1,3)=5!*(8!/3!*5!)=",math.factorial(5)*C(6+3-1,3)
To determine the number of ways to seat five boys in a row of 12 chairs.The number of arrangements would be 95040
Alternative way:
Suppose, we arrange the first five boys in a row and then distribute the seven unoccupied chairs arbitrarily either between any two boys or at the two ends. The distribution problem then becomes that of placing seven balls of the same color in six boxes. Thus, the number of ways to do is 5!*C(6+7-1,7)=5!*(12!/7!*5!)=12!/7!= 95040
Suppose, we want to seat the boys so that no two boys are next to each other.
5!*C(6+3-1,3)=5!*(8!/3!*5!)= 6720

Example 22:page 83

In [20]:
from fractions import Fraction
print "For the experiment of rolling a dice, the sample space consists of six samples. We suppose the probability of occurence of each of these samples is 1/6."
print "probability of getting an odd number is", (Fraction(1,6)+Fraction(1,6)+Fraction(1,6)) # Since out of the six samples, three of them are odd numbers and the probability of getting each of them is 1/6
print "On the other hand, suppose that we have a 'crooked' die such that the probability of getting a 1 is 1/3 and the probability of getting each of the remaining numbers is 2/15."
print "The probability of getting an odd number is",(Fraction(1,3)+Fraction(2,15)+Fraction(2,15)) # Since out of the six samples, three of them are odd numbers and the probability of getting a 1 is 1/3 and each of the remaining is 2/15
print "The probability of getting an even number is",(Fraction(2,15)+Fraction(2,15)+Fraction(2,15)) # Since out of the six samples, three of them are even numbers and the probability of getting each of them is 2/15
For the experiment of rolling a dice, the sample space consists of six samples. We suppose the probability of occurence of each of these samples is 1/6.
probability of getting an odd number is 1/2
On the other hand, suppose that we have a 'crooked' die such that the probability of getting a 1 is 1/3 and the probability of getting each of the remaining numbers is 2/15.
The probability of getting an odd number is 3/5
The probability of getting an even number is 2/5

Example 23:Page 84

In [21]:
from fractions import Fraction
print "For the experiment of rolling two dice, the sample space consists of 36 samples. Suppose we want to find the probability of getting a sum of 9 in this experiment. We can get the sum 9 in four different ways such as {(3,6),(4,5),(5,4),(6,3)}"
print "probability of getting a sum 9 is",(Fraction(1,36)+Fraction(1,36)+Fraction(1,36)+Fraction(1,36)) # Since the probability of occurence of each sample is 1/36 and we have four samples which give a sum of 9
For the experiment of rolling two dice, the sample space consists of 36 samples. Suppose we want to find the probability of getting a sum of 9 in this experiment. We can get the sum 9 in four different ways such as {(3,6),(4,5),(5,4),(6,3)}
probability of getting a sum 9 is 1/9

Example 24:Page 84

In [22]:
from decimal import *
import math
def c(n,r):
    res=math.factorial(n)/(math.factorial(r)*math.factorial(n-r))#Mathematical implementation of combinations
    return res

cards=52#Total number of cards in a deck
poker_cards=5#5 cards make up a poker hand
aces=48.0#Number of outcomes with 4 aces
print "Consider the problem of dealing a poker hand out of a deck of 52 cards."
print "The sample space consists of",c(cards,poker_cards),"sample points corresponding to the c(52,5) different hands that can be dealt"
print "Assume that the outcomes have equal probabilities;that is,the probability that a particular hand was dealt is equal to 1/c(52,5)=",1.0/(c(cards,poker_cards))
getcontext().prec=3#Decides the number of decimal digits
print "To determine the probability of geting four aces, we note that 48 of the c(52,5) possible outcomes contain four aces;thus,the probability is 48/c(52,5)=",Decimal(aces)/Decimal(c(cards,poker_cards))
Consider the problem of dealing a poker hand out of a deck of 52 cards.
The sample space consists of 2598960 sample points corresponding to the c(52,5) different hands that can be dealt
Assume that the outcomes have equal probabilities;that is,the probability that a particular hand was dealt is equal to 1/c(52,5)= 3.84769292332e-07
To determine the probability of geting four aces, we note that 48 of the c(52,5) possible outcomes contain four aces;thus,the probability is 48/c(52,5)= 0.0000185

Example 25:Page 84

In [23]:
import math
from decimal import *
def P(n,r):
    res=math.factorial(n)/math.factorial(n-r)#Mathematical implementation of permutations
    return res
print "Consider 23 people out of which the chance is less than 50-50 that no two of them will have the same birthday. Sample space consists of 366^23 samples corresponding to all possible distributions of birthdays of 23 people."
print "Assume: The distributions are equiprobable."
getcontext().prec=3#Decides the number of decimal digits
print "Since out of the 366^23 samples, P(366,23) of them correspond to distributions of birthdays such that no two of the 23 people have the same birthday, the probability that no two people have the same birthday is P(366,23)/366^23=",(Decimal(P(366,23))/Decimal(pow(366,23)))
Consider 23 people out of which the chance is less than 50-50 that no two of them will have the same birthday. Sample space consists of 366^23 samples corresponding to all possible distributions of birthdays of 23 people.
Assume: The distributions are equiprobable.
Since out of the 366^23 samples, P(366,23) of them correspond to distributions of birthdays such that no two of the 23 people have the same birthday, the probability that no two people have the same birthday is P(366,23)/366^23= 0.494

Example 26:Page 84

In [24]:
from decimal import *
import math
students=8#Total number of students
freshmen=2#Total number of freshmen
sophomores=2#Total number of sophomores
juniors=2#Total number of juniors
seniors=2#Total number of seniors
sample_space=pow(4,8)#Total number of samples
print "Eight students are standing in line for an interview. We want to determine the probability that there are exactly two fishermen, two sophomores, two juniors, and two seniors in the line. The sample space consists of 4^8 samples corresponding to all possibilities of classes the students are from. "
print "Assume: All the samples are equiprobable"
getcontext().prec=3#Decides the number of decimal digits
print "There are 8!/2!2!2!2! samples corresponding to the case in which there are two students from each class. Thus the probability is ",(Decimal(math.factorial(students))/(Decimal(math.factorial(freshmen)*math.factorial(sophomores)*math.factorial(juniors)*math.factorial(seniors)*sample_space)))
Eight students are standing in line for an interview. We want to determine the probability that there are exactly two fishermen, two sophomores, two juniors, and two seniors in the line. The sample space consists of 4^8 samples corresponding to all possibilities of classes the students are from. 
Assume: All the samples are equiprobable
There are 8!/2!2!2!2! samples corresponding to the case in which there are two students from each class. Thus the probability is  0.0385

Example 28:Page 85

In [25]:
print "To find the probability of various number of buffers being filled by digital data from a remote site"
A=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]# A denotes that initial 16 buffers are full
B=[1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31]#B denotes that odd buffers are full
A_inter_B=[1,3,5,7,9,11,13,15]#set representing A intersection B
P_A=0.0#initial probability corresponding to set A is zero
P_A_and_B=0.0#initial probability of set A intersection B is zero
P_B=0.0#initial probability of set B is zero
for q in A:
    P_A+=(1.0/561.0)*(33.0-q)
print "Probability of buffers in set A being filled is",round(P_A,3)
for q in B:
    P_B+=(1.0/561.0)*(33.0-q)
print "Probability of buffers in set B being filled is",round(P_B,3)
for q in A_inter_B:
    P_A_and_B+=(1.0/561.0)*(33.0-q)
print "Probability of buffers in set A intersection B being filled is",round(P_A_and_B,3)
To find the probability of various number of buffers being filled by digital data from a remote site
Probability of buffers in set A being filled is 0.758
Probability of buffers in set B being filled is 0.485
Probability of buffers in set A intersection B being filled is 0.357

Example 29:Page 86

In [26]:
print "Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random. We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair."
P_fb=0.090 # Probability of selecting a bald female 
P_fh=0.425 # Probabbility of selecting a female with hair
P_mb=0.302 # Probability of selecting a bald male
p_mh=0.183 # Probability of selecting a male with hair
print "Given:"
print "P(fb)=0.090"
print "P(fh)=0.425"
print "P(mb)=0.302"
print "P(mh)=0.183"

print "Let A denote the event that a bald person was chosen"
print "Let B denote the event that a female was chosen"
print "Then A intersection B is the event that a bald female was chosen, A union B the event that a bald person or a female was chosen,A EXOR B the event that a female with hair or a bald male was chosen and B-A the event that a female with hair was chosen."
P_A=P_fb+P_mb # Includes both male bald and female bald
P_B=P_fb+P_fh # Includes all females
P_A_intersection_B=P_fb # iIncludes all females who are bald
P_A_union_B=P_fb+P_fh+P_mb # Includes all females and bald male
P_A_EXOR_B=P_fh+P_mb # Includes female with hair and bald male
P_B_minus_A=P_fh # Includes only female with hair
print "P(A)=",P_A
print "P(B)",P_B
print "P(A intersection B)=",P_A_intersection_B
print "P(A union B)=",P_A_union_B
print "P(A EXOR B)=",P_A_EXOR_B
print "P(B-A)=",P_B_minus_A
Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random. We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair.
Given:
P(fb)=0.090
P(fh)=0.425
P(mb)=0.302
P(mh)=0.183
Let A denote the event that a bald person was chosen
Let B denote the event that a female was chosen
Then A intersection B is the event that a bald female was chosen, A union B the event that a bald person or a female was chosen,A EXOR B the event that a female with hair or a bald male was chosen and B-A the event that a female with hair was chosen.
P(A)= 0.392
P(B) 0.515
P(A intersection B)= 0.09
P(A union B)= 0.817
P(A EXOR B)= 0.727
P(B-A)= 0.425

Example 31:Page 88

In [27]:
print "Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random.We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair."
print "Let A denote the event that a bald person was chosen, B the event that a female was chosen, and C the event that a male was chosen."
P_fb=0.090 # Probability of selecting a bald female 
P_fh=0.425 # Probabbility of selecting a female with hair
P_mb=0.302 # Probability of selecting a bald male
P_mh=0.183 # Probability of selecting a male with hair
print "Given:"
print "P(fb)=0.090"
print "P(fh)=0.425"
print "P(mb)=0.302"
print "P(mh)=0.183"
P_A=P_fb+P_mb # Includes both male bald and female bald
P_B=P_fb+P_fh # Includes all female
P_C=P_mb+P_mh # Includes all male
P_A_intersection_C=P_mb # Includes all males who are bald
P_C_intersection_A=P_mb # Since A intersection C and C intersection A are the same, this also includes all males who are bald
P_A_intersection_B=P_fb # Includes all females who are bald
P_B_intersection_A=P_fb # Since A intersection B and B intersection A are the same,this also includes all females who are bald

P_A_given_B=P_A_intersection_B/P_B # By conditional probability
print "P(A|B)=",round(P_A_given_B,3);
print "This is less than P(A) which is 0.392"
P_A_given_C=P_A_intersection_C/P_C # By conditional probability

print "P(A|C)=",round(P_A_given_C,3)
print "This is quite a bit greater than P(A)"
P_B_given_A=P_B_intersection_A/P_A# By conditional probability
print "P(B|A)=",round(P_B_given_A,2)
P_C_given_A=P_C_intersection_A/P_A# By conditional probability
print "P(C|A)=",round(P_C_given_A,2)
print "Indeed, although P(B) is slightly larger than P(C),P(B|A) is much smaller than P(C|A)"
Out of 100,000 people, 51,500 are female and 48,500 are male. Among the females 9,000 are bald, and among the males 30,200 are bald. Suppose we are to choose a person at random.We shall have S={fb,fh,mb,mh} as the sample space with fb denoting bald female, fh a femele with hair, mb a bald male, and mh a male with hair.
Let A denote the event that a bald person was chosen, B the event that a female was chosen, and C the event that a male was chosen.
Given:
P(fb)=0.090
P(fh)=0.425
P(mb)=0.302
P(mh)=0.183
P(A|B)= 0.175
This is less than P(A) which is 0.392
P(A|C)= 0.623
This is quite a bit greater than P(A)
P(B|A)= 0.23
P(C|A)= 0.77
Indeed, although P(B) is slightly larger than P(C),P(B|A) is much smaller than P(C|A)

Example 32:Page 89

In [28]:
from fractions import Fraction
from decimal import *
print "A coin was chosen at random and tossed."
p_fc_hd=Fraction('1/3') # Probability that a fair coin was chosen and head shows
p_fc_tl=Fraction('1/3') # Probability that a fair coin was chosen and tail shows
p_ufc_hd=Fraction('1/12') # Probability that a unfair coin was chosen and head shows
p_ufc_tl=Fraction('1/4') # Probability that a unfair coin was chosen and head shows

print "The probability that head shows is",(Fraction(p_fc_hd)+Fraction(p_ufc_hd)) # Probability of head in both fair and unfair coins
print "The probability that an unfair coin was chosen is",(Fraction(p_ufc_hd)+Fraction(p_ufc_tl)) # Probability of head and tail in unfair coin
p_head=(Fraction(p_fc_hd)+Fraction(p_ufc_hd))
print "The conditional probability that an unfair coin was chosen given that head shows is",(Fraction(p_ufc_hd)/Fraction(p_head))# By conditional probability, probability of head in unfair coin to the probabilty of head in both fair and unfair coins
print "The conditional probability that head shows given that an unfair coin was chosen is",(Fraction(p_ufc_hd)/(Fraction(p_ufc_hd)+Fraction(p_ufc_tl))) # By conditional probability,probability of head in unfair coin to the probability of head and tail in unfair coin
A coin was chosen at random and tossed.
The probability that head shows is 5/12
The probability that an unfair coin was chosen is 1/3
The conditional probability that an unfair coin was chosen given that head shows is 1/5
The conditional probability that head shows given that an unfair coin was chosen is 1/4

Example 33:Page 89

In [29]:
import math
from fractions import Fraction
def P(n,r):
    res=math.factorial(n)/math.factorial(n-r)
    return res
faces=6#Number of faces in a dice
dice=3#Number of dice that were rolled
print "Three dice were rolled."
print "Given: No two faces were the same."
print "Let A denote the event that there was an ace."
print "Let B  denote the event that no two faces were the same."
P_B=Fraction(P(faces,dice))/Fraction(pow(faces,dice)) # Sample space is pow(6,3) sice three dice are rolled.
P_A_intersection_B=Fraction(dice*P(faces-1,dice-1))/Fraction(pow(faces,dice))#Since A has occured, it cannot repeat again. So faces and dice are reduced by 1
P_A_given_B=Fraction(P_A_intersection_B)/Fraction(P_B) #By conditional probability
print "P(A|B)=",P_A_given_B
Three dice were rolled.
Given: No two faces were the same.
Let A denote the event that there was an ace.
Let B  denote the event that no two faces were the same.
P(A|B)= 1/2

Example 34:Page 90

In [30]:
from fractions import Fraction
print "We define events and probabilities as follows."
print "E:Introduction of computer education"
print "A1:Mr.X is selected as chairman"
print "A2:Mr.Y is selected as chairman"
print "A3:Mr.Z is selected as chairman"
P_A1=Fraction('4/9') # Probability of Mr.X being selected
P_A2=Fraction('2/9') # Probability of Mr.Y being selected
P_A3=Fraction('3/9') # Probability of Mr.Z being selected
P_E_given_A1=Fraction('3/10') # probability that there was computer education in the college when Mr.X is selected as a chairman
P_E_given_A2=Fraction('5/10') # probability that there was computer education in the college when Mr.Y is selected as a chairman
P_E_given_A3=Fraction('8/10') # probability that there was computer education in the college when Mr.Z is selected as a chairman
print "P(E)=P[(E INTERSECTION A1) UNION (E INTERSECTION A2) UNION (E INTERSECTION A3)]"
print "=P(E INTERSECTION A1) + P(E INTERSECTION A2) + P(E INTERSECTION A3)"
print "=P(A1)P(E|A1)+P(A2)P(E|A2)+P(A3)P(E|A3)" # Since by conditional probability, P(E|A1)=P(E intersection A1)/P(A1)
print "=",P_A1,"*",P_E_given_A1,"+",P_A2,"*",P_E_given_A2,"+",P_A3,"*",P_E_given_A3,"=",
P_E=(P_A1*P_E_given_A1)+(P_A2*P_E_given_A2)+(P_A3*P_E_given_A3) # P_E denotes the probability of computer education in the college
print P_E
print "So, the probability that there was computer education in the college is",P_E
We define events and probabilities as follows.
E:Introduction of computer education
A1:Mr.X is selected as chairman
A2:Mr.Y is selected as chairman
A3:Mr.Z is selected as chairman
P(E)=P[(E INTERSECTION A1) UNION (E INTERSECTION A2) UNION (E INTERSECTION A3)]
=P(E INTERSECTION A1) + P(E INTERSECTION A2) + P(E INTERSECTION A3)
=P(A1)P(E|A1)+P(A2)P(E|A2)+P(A3)P(E|A3)
= 4/9 * 3/10 + 2/9 * 1/2 + 1/3 * 4/5 = 23/45
So, the probability that there was computer education in the college is 23/45

Example 35:Page 91

In [31]:
print "A1: Event that the selected item was produced by machine M1"
print "A2: Event that the selected item was produced by machine M2"
print "A3: Event that the selected item was produced by machine M3"
print "E: Event that the selected item is defective"
P_A1=0.2 # The probability that an item selected at random from the entire batch was produced by machine M1
P_A2=0.3 # The probability that an item selected at random from the entire batch was produced by machine M2
P_A3=0.5 # The probability that an item selected at random from the entire batch was produced by machine M3
P_E_given_A1=0.01 # Probability that an item produced by machine M1 will be defective
P_E_given_A2=0.02 # Probability that an item produced by machine M2 will be defective
P_E_given_A3=0.03 # Probability that an item produced by machine M3 will be defective
print "We require to calculate the conditional probability P(A3|E)"
print "Using bayes' theorem"
P_A3_given_E=(P_A3*P_E_given_A3)/((P_A1*P_E_given_A1)+(P_A2*P_E_given_A2)+(P_A3*P_E_given_A3))
print "The probability that this item was produced by machine M3 is",round(P_A3_given_E,3)
 
A1: Event that the selected item was produced by machine M1
A2: Event that the selected item was produced by machine M2
A3: Event that the selected item was produced by machine M3
E: Event that the selected item is defective
We require to calculate the conditional probability P(A3|E)
Using bayes' theorem
The probability that this item was produced by machine M3 is 0.652

Example 36:Page 95

In [32]:
from fractions import Fraction
import math
from decimal import *
print "Estimating the likelihood that there will be a 1 hour examination when the professor is scheduled to get out of town"
print "Sample space={x1,x2,x3,x4}"
P_X1=Fraction('1/2')#X1 denotes professor out of town and exam given
P_X2=Fraction('1/16')#X2 denotes professor out of town and exam not given
P_X3=Fraction('3/16')#X3 denotes professor in town and exam given
P_X4=Fraction('1/4')#X4 denotes professor in town and exam not given
print "A denotes the event that exam is given and B denote the event that professor is out of town"
P_A=P_X1+P_X3#Probability of A
P_A_numerator=P_A.numerator#Separating numerator and denominators in all P(A)
P_A_denominator=P_A.denominator
P_A_given_B=P_X1/(P_X1+P_X2)#Probability of P(A|B)
P_A_given_B_numerator=P_A_given_B.numerator#Separating numerators and denominators in P(A|B)
P_A_given_B_denominator=P_A_given_B.denominator
print "Probability that the exam is given is ",P_A
print "Probability of exam given that the profession is out of town is ",P_A_given_B
log_P_A_numerator=round(math.log(P_A_numerator,2),2)#Logrithm of 11
log_P_A_denominator=math.log(P_A_denominator,2)#Logrithm of 16
neg_log_P_A=-log_P_A_numerator+log_P_A_denominator#Negative logrithm of P(A).SInce log(a/b)=lob(a)-log(b)
getcontext().prec=2#Decides the number of digits after decimal point
print "The needed information to determine that an examination will be given is ",neg_log_P_A,"bits"
log_P_A_given_B_numerator=math.log(P_A_given_B_numerator,2)#Logrithm of 
log_P_A_given_B_denominator=round(math.log(P_A_given_B_denominator,2),2)#Logrithm of 9
print "Information provided by the fact that the professor is out of town on the fact that examination is given is I(A,B)=",(-log_P_A_numerator+log_P_A_denominator+log_P_A_given_B_numerator-log_P_A_given_B_denominator),"bits"
print "C denotes the event that the professor is in town"
P_A_given_C=P_X3/(P_X3+P_X4)
print "Probability that the examination is given when the professor is in town is ",P_A_given_C
P_A_given_C_numerator=P_A_given_C.numerator#Separating numerators and denominators in P(A|C)
P_A_given_C_denominator=P_A_given_C.denominator
log_P_A_given_C_numerator=round(math.log(P_A_given_C_numerator,2),2)
log_P_A_given_C_denominator=math.log(P_A_given_C_denominator,2)
print "I(A,C)=",round((-log_P_A_numerator+log_P_A_denominator+log_P_A_given_C_numerator-log_P_A_given_C_denominator),2)
Estimating the likelihood that there will be a 1 hour examination when the professor is scheduled to get out of town
Sample space={x1,x2,x3,x4}
A denotes the event that exam is given and B denote the event that professor is out of town
Probability that the exam is given is  11/16
Probability of exam given that the profession is out of town is  8/9
The needed information to determine that an examination will be given is  0.54 bits
Information provided by the fact that the professor is out of town on the fact that examination is given is I(A,B)= 0.37 bits
C denotes the event that the professor is in town
Probability that the examination is given when the professor is in town is  3/7
I(A,C)= -0.69