This is a graded in-class assignment with peer review. **One submission per group on paper.** Do a clean work, your style will be evaluated too. Take a snapshot of your work after peer review. Check the details of peer review guidelines on Bilgi Learn.

The local coffee shop has three kinds of coffee, Latte, Cappuccino and Macchiato. A customer orders Cappuccino with probability 0.6, Latte 0.25 and Macchiato 0.15.

What is the probability that at least three customers among first 10 customers order Cappuccino or Macchiato?

\(P(L)=0.25\)

\(P(M \cup C)=0.6 + 0.15 = 0.75\)

Say \(P(A)\) is “at least three customers among first 10 customers order Cappuccino or Macchiato”. \(1-P(A)\) means “two or fewer of the first 10 customers order C or M”.

\(P(0)=(1-0.75)^{10}\),\(P(1)=P(M \cup C)^{1}*P(L)^9*\binom{10}{1}\),\(P(2)=P(M \cup C)^{2}*P(L)^8*\binom{10}{2} = 1-P(A)\)

\(P(A) = 1 - P(0) - P(1) - P(2)\)

```
#Say probability of ordering Latte is prL
prL=0.25
#Probability of ordering Cappuccino or Macchiato is prCM
prCM = 0.6 + 0.15
#At least three means 3 to 10 customers ordered Cappuccino or Macchiato
#with probability prCM
#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 Cappuccino or Macchiato
p_0 = prL^10
#p_1 is exactly one of the customers order Cappuccino or Macchiato
p_1 = prCM^1*prL^9*choose(10,1)
#p_2 is exactly two of the customers order Cappuccino or Macchiato
p_2 = prCM^2*prL^8*choose(10,2)
1-p_0-p_1-p_2
```

`## [1] 0.9995842`

What is the probability that the first Latte is ordered by the fourth customer or before?

Probability of 1st order being latte is \(P(L_1) = 0.25\)

Probability of 1st order being not latte and 2nd order latte is \(P(L_2 \cap L_1') = 0.75*0.25\)

Probability of 1st and 2nd order being not latte and 3rd order latte is \(P(L_3 \cap L_1' \cap L_2') = 0.75*0.75*0.25\)

Probability of first 3 orders being not latte and 4th order latte is \(P(L_4 \cap L_1' \cap L_2' \cap L_3') = 0.75^3*0.25\)

\(P(A) = P(L_1) + P(L_2 \cap L_1') + P(L_3 \cap L_1' \cap L_2') + P(L_4 \cap L_1' \cap L_2' \cap L_3')\)

```
#Probability of having the first order a Latte is 0.25
#Probability of not having the first order a Latte but on the second one is 0.75*0.25
#Probability of not having the first two order a Latte but on the third one is 0.75*0.75*0.25
#Probability of not having the first three order a Latte but on 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`

The first 5 customers get a free cookie each day. What is the probability that at least 2 cookies are given to customers who order Macchiato?

Same logic as (a). \(P(M) = 0.15\). Suppose X is macchiato customers getting at least 2 cookies out of first 5 and \(P(i)\) is getting exactly \(i\) cookies. Then we should calculate \(P(X) = P(2) + P(3) + P(4) + P(5)\) or \(P(X) = 1 - P(0) - P(1)\).

\[P(X) = 1 - 0.85^5 - \binom{5}{1}(0.15)^1(0.85)^4\]

```
#The logic is the same as a.
1 - (0.85)^5 - choose(5,1)*(0.85)^4*0.15
```

`## [1] 0.16479`

- If any type of coffee runs out, the remaining coffee types will be preferred proportionally (e.g. if Macchiato runs out Latte’s probability will be 0.25/0.85). Suppose, the coffee shop has only 1 cup of Latte left. What is the probability that exactly 1 out of the first 3 customers will order Cappuccino?

Suppose there are no Latte customers in the first 3 customers. Then the probabilities do not change. Consider this event as \(B_1\). Possible desired combinations are MCM, CMM, MMC

\[P(B_1) = \binom{3}{1}(0.6)(0.15)^2\]

Now suppose first customer ordered latte (\(B_2\)). The probability is 0.25 and there are two possible desired combinations LMC or LCM.

\[P(B_2) = 0.25*\binom{2}{1}(0.6/0.75)(0.15/0.75)\]

Suppose second customer ordered latte (\(B_3\)). Possible desired outcomes are MLC or CLM.

\[P(B_3) = 0.6*0.25*(0.15/0.75) + 0.15*0.25*(0.6/0.75)\]

Now suppose third customer ordered latte (\(B_4\)). It is similar to \(B_2\) but this time since latte is ordered at the end, it will not change the prior probabilities. Possible desired combinations are MCL, CML.

\[P(B_4) = 0.6*0.15*0.25 + 0.15*0.6*0.25 \]

```
#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 1 Cappuccino order out of 3 orders if no customer orders Latte
pB1 = choose(3,1)*(0.6)*(0.4)^2
#If the 1st customer orders Latte
pB2=0.25*(2*(0.15/0.75)*(0.6/0.75))
#If the 2nd customer orders latte
pB3=0.6*0.25*(0.15/0.75) + 0.15*0.25*(0.6/0.75)
#If the 3rd customer orders latte
pB4 = 0.6*0.15*0.25 + 0.15*0.6*0.25
pB1 + pB2 + pB3 + pB4
```

`## [1] 0.473`

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

\[P(I) = (1-(1-0.75)*(1-0.25)*(1-0.3))\] \[P(II) = (1-(1-0.5)*(1-0.4))\] \[P(III) = (1-(1-0.6)*(1-0.7))\] \[P(S_1) = P(I)*P(II)*P(III)\] \[P(S_2) = 0.75*0.9\]

\[P(S^\prime) = (1-P(S_1))*(1-P(S_2))\]

```
#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
#For serial nodes you should multiply the probabilities
#Probability of passing subsystem two
p_s2 = 0.75*0.9
#Probability of not passing the whole system
(1-p_s1)*(1-p_s2)
```

`## [1] 0.1510762`

A machine produces 25 items, 20 of which is non-defective. The items are randomly selected without replacement. The 7th selected item is found to be non-defective. What is the probability that this is the 2nd non-defective one?

```
So, to paraphrase we need to find exatly one non-defective item in the first 6 items. Since we fix the 7th item we are left with 19 non-defective and 24 total items. The order could be NDDDDD, DNDDDD, DDNDDD etc. It is a combination problem. Denote the probability it with P(A).
$$P(A) = \binom{6}{1}*(19/24)*(5/23)*(4/22)*(3/21)*(2/20)*(1/19)$$
```

`choose(6,1)*(19/24)*(5/23)*(4/22)*(3/21)*(2/20)*(1/19)`

`## [1] 0.0001411632`

A dice player rolls two dice.

- He wins if the sum is either 7 or 11.
- He loses if the sum is 2, 3 or 12.
- He repeats the roll if the sum is 4, 5, 6, 8, 9 or 10
- Then repeats the roll until the initial sum is repeated, then wins.
- Loses if the sum is 7

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

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

\[\begin{align*} P(Lose) =& 6/36 + 2/36 + 1/36 + 2/45 + 25/396 + 25/396 + 2/45 + 1/36\\ =& 0.5070707 \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_lose_2 = p_dice["2"]
p_lose_3 = p_dice["3"]
p_lose_12 = p_dice["12"]
p_lose_4 = p_dice["4"]*(p_dice["7"]*(1/(1-(1-p_dice["7"]-p_dice["4"]))))
p_lose_5 = p_dice["5"]*(p_dice["7"]*(1/(1-(1-p_dice["7"]-p_dice["5"]))))
p_lose_6 = p_dice["6"]*(p_dice["7"]*(1/(1-(1-p_dice["7"]-p_dice["6"]))))
p_lose_8 = p_dice["8"]*(p_dice["7"]*(1/(1-(1-p_dice["7"]-p_dice["8"]))))
p_lose_9 = p_dice["9"]*(p_dice["7"]*(1/(1-(1-p_dice["7"]-p_dice["9"]))))
p_lose_10 = p_dice["10"]*(p_dice["7"]*(1/(1-(1-p_dice["7"]-p_dice["10"]))))
p_lose = p_lose_2 + p_lose_3 + p_lose_12 + p_lose_4 + p_lose_5 + p_lose_6 + p_lose_8 + p_lose_9 + p_lose_10
print(p_lose)
```

```
## 2
## 0.5070707
```

In a classroom of 30 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 30 days from the year, so all the days will be different.
#Ordering is important since different permutations can be valid.
n_select = choose(365,30)*factorial(30)
#Then find the number of ways 30 days can be chosen with repetition possible
#It is multiplication rule, like tossing 1,2,3,.. coins
n_mult = 365^30
#Probability is the proportion
n_select/n_mult
```

`## [1] 0.2936838`

```
#or just use
1-pbirthday(30)
```

`## [1] 0.2936838`