This is a graded in-class assignment. Show all your work in R Markdown files. Submit compiled Word files only.

Question 1

The local coffee shop has three kinds of coffee, Turkish, espresso and filter coffee. A customer orders Turkish coffee with probability 0.4, espresso 0.25 and filter coffee 0.35.

  1. What is the probability that at least three customers among first 10 customers order espresso or filter coffee?
#Say probability of ordering turkish coffee is pa
pa=0.4
#Probability of ordering espresso or filter coffee is pb
pb = 0.25 + 0.35
#At least three means 3 to 10 customers ordered espresso or filter coffee
#with probability p2
#Though if we can calculate 0 to 2 customers and remove it
#from the total probability (which is 1) it will be the same.
#p_0 is none of the customers order filter coffee or espresso
p_0 = pa^10
#p_1 is exactly one of the customers order filter coffee or espresso
p_1 = pb^1*pa^9*choose(10,1)
#p_2 is exactly two of the customers order filter coffee or espresso
p_2 = pb^2*pa^8*choose(10,2)
1-p_0-p_1-p_2
## [1] 0.9877054
  1. What is the probability that the first espresso is ordered by the fourth customer or before?
#Probability of having the first order an espresso is 0.25
#Probability of not having the first order an espresso but not the second one is 0.75*0.25
#Probability of not having the first two order an espresso but not the third one is 0.75*0.75*0.25
#Probability of not having the first three order an espresso but not the fourth one is (0.75)^3*0.25
0.25 + (0.75)*0.25 + (0.75)^2*0.25 + (0.75)^3*0.25
## [1] 0.6835938
  1. The first 7 customers get a free cookie each day. What is the probability that at least three cookies are given to customers who order filter coffee?
#The logic is the same as a.
1 - (0.65)^7 - 7*(0.65)^6*0.35 - choose(7,2)*(0.65)^5*(0.35)^2
## [1] 0.4677167
  1. If any type of coffee runs out, the remaining coffee types will be preferred proportionally (e.g. if espresso runs out Turkish coffee’s probability will be 0.4/(0.75)). Suppose, the coffee shop has only 1 cup of espresso left. What is the probability that 3 out of the first 5 customers will order filter coffee?
#Tip: Probabilities are rescaled after espresso is ordered
# and rescaling factor is the same (1/0.75) for all other probabilities.
# Its order is not important, we should calculate how many rescaling should be done.

#Probability of having 3 out of 5 filter coffee if no customer orders espresso
p0 = choose(5,3)*(0.35)^3*0.4^2

#Probability of having 3 out of 5 filter coffee if the 1st customer orders espresso
#Assume (T)urkish , (E)spresso, (F)ilter
#There are four combinations ETFFF, EFTFF, EFFTF, EFFFT
p1=4*(0.35)^3*0.4*0.25/(0.75)^(5-1)

#Probability of having 3 out of 5 filter coffee if the 2nd customer orders espresso
#There are four combinations TEFFF, FETFF, FEFTF, FEFFT
p2=4*(0.35)^3*0.4*0.25/(0.75)^(5-2)

#Probability of having 3 out of 5 filter coffee if the 3rd customer orders espresso
p3=4*(0.35)^3*0.4*0.25/(0.75)^(5-3)

#Probability of having 3 out of 5 filter coffee if the 4th customer orders espresso
p4=4*(0.35)^3*0.4*0.25/(0.75)^(5-4)

#Probability of having 3 out of 5 filter coffee if the 5th customer orders espresso
p5=4*(0.35)^3*0.4*0.25/(0.75)^(5-5)

#Total probability of the event
p0 + p1 + p2 + p3 + p4 + p5
## [1] 0.2339599

Question 2

Consider the system above. Suppose the system works if either subsystem 1 or subsystem 2 works. Calculate the probability of the system working?

#For serial nodes you should multiply the probabilities
#Probability of passing subsystem two
p_s2 = 0.75*0.9
#For parallel nodes you should calculate the probability of not passing
#through any node and subtract it from 1.

#Probability of passing subsystem 1 - I
p_s1_1 = (1 - (1-0.75)*(1-0.25)*(1-0.3))
#Probability of passing subsystem 1 - II
p_s1_2 = (1-(1-0.5)*(1-0.4))
#Probability of passing subsystem 1 - III
p_s1_3 = (1-(1-0.6)*(1-0.7))
#Probability of passing subsystem 1
p_s1 = p_s1_1*p_s1_2*p_s1_3
#Probability of passing the whole system
1 - (1-p_s1)*(1-p_s2)
## [1] 0.8489237

Question 3

A machine produces 15 items, 12 of which is non-defective. The items are randomly selected without replacement. The sixth selected item is found to be non-defective. What is the probability that this is the third non-defective one?

#First five should be any combination of two (N)on-defective items and
#three (D)efective items for the sixth item to be the third non-defective item.
#A valid outcome would be NNDDD
#Probability of that outcome is
po=(12/15)*(11/14)*(3/13)*(2/12)*(1/11)
#Other valid outcomes all have the same probabilities
#number of possible valid outcomes is the combination
choose(5,2)*po
## [1] 0.02197802

Question 4

A dice player rolls two dice.

What is \(P(Win)\)? (Hint: \(\sum_{i=0}^\infty a^i = \dfrac{1}{1-a}\) if \(0 < a < 1\))

\[\begin{align} P(Win) =& P({Sum}_1 = 7) + P({Sum}_1 = 11) + P(Win,{Sum_1}=4) + P(Win,{Sum_1}=5) + P(Win,{Sum_1}=6) + \\ & P(Win,{Sum_1}=8) + P(Win,{Sum_1}=9) + P(Win,{Sum_1}=10) \\~\\ P({Sum} = 7) =& P(1,6) + P(2,5) + P(3,4) + P(4,3) + P(5,2) + P(6,1) = 6/36 = 1/6 \\ P({Sum} = 11) =& P(5,6) + P(6,5) = 2/36 = 1/18 \\~\\ P({Win},{Sum}_1 = 4) =& P({Sum}_1 = 4)*P(Win | {Sum}_1 = 4)\\ P({Win}|{Sum}_1 = 4) =& P({Sum}_2 = 4) + P({Sum}_2 \neq 4,7)*P(Win | {Sum}_2 \neq 4,7)\\ P({Win} | {Sum}_2 \neq 4,7) =& P({Sum}_3 = 4) + P({Sum}_3 \neq 4,7)*P(Win | {Sum}_3 \neq 4,7)\\ P({Win} | {Sum}_i \neq 4,7) =& P({Sum}_{i+1} = 4) + P({Sum}_{i+1} \neq 4,7)*P(Win | {Sum}_{i+1} \neq 4,7)\\~\\ P({Sum}_1 = 4) =& P(1,3) + P(2,2) + P(3,1) = 3/36 = 1/12 \\ P({Sum}_1 \neq 4,7) =& 1 - 3/36 - 6/36 = 27/36 = 3/4 \\ P({Win}|{Sum}_1 = 4) =& 1/12 + 3/4*(1/12 + 3/4*(1/12 + \dots)) \\ P({Win}|{Sum}_1 = 4) =& 1/12*(1 + 3/4 + (3/4)^2 + (3/4)^3 + \dots)\\ P({Win}|{Sum}_1 = 4) =& 1/12*(1/(1-3/4)) = 1/3\\ P({Win},{Sum}_1 = 4) =& 1/12*1/3 = 1/36 \\ \end{align}\]

Similarly for 5,6,8,9,10

\[\begin{align} P(Win) =& 6/36 + 2/36 + 1/36 + 2/45 + 25/396 + 25/396 + 2/45 + 1/36 =& 0.4929293 \end{align}\]
#First let's calculate probability of sums
#Following code gives a probability table of sums
p_dice = table(expand.grid(1:6,1:6)[,1]+expand.grid(1:6,1:6)[,2])/36
p_dice
## 
##          2          3          4          5          6          7 
## 0.02777778 0.05555556 0.08333333 0.11111111 0.13888889 0.16666667 
##          8          9         10         11         12 
## 0.13888889 0.11111111 0.08333333 0.05555556 0.02777778
p_win_7 = p_dice["7"]
p_win_11 = p_dice["11"]
p_win_4 = p_dice["4"]*(p_dice["4"]*(1/(p_dice["4"]+p_dice["7"])))
p_win_5 = p_dice["5"]*(p_dice["5"]*(1/(p_dice["5"]+p_dice["7"])))
p_win_6 = p_dice["6"]*(p_dice["6"]*(1/(p_dice["6"]+p_dice["7"])))
p_win_8 = p_dice["8"]*(p_dice["8"]*(1/(p_dice["8"]+p_dice["7"])))
p_win_9 = p_dice["9"]*(p_dice["9"]*(1/(p_dice["9"]+p_dice["7"])))
p_win_10 = p_dice["10"]*(p_dice["10"]*(1/(p_dice["10"]+p_dice["7"])))
p_win = p_win_7 + p_win_11 + p_win_4 + p_win_5 + p_win_6 +
        p_win_8 + p_win_9  + p_win_10
print(p_win)
##         7 
## 0.4929293

Question 5

In a classroom of 22 students, what is the probability that none of them are born on the same day of the year? (ignore February 29)

#First find the number of selecting 22 days from the year. So all the days will be different
n_select = choose(365,22)*factorial(22)
#Then find the number of ways 22 days can be chosen with repetition possible
#It is multiplication rule, like tossing 1,2,3,.. coins
n_mult = 365^22
#Probability is the proportion
n_select/n_mult
## [1] 0.5243047
#or just use
1-pbirthday(22)
## [1] 0.5243047