Discussion 7 Ch. 5 # 18

library(knitr)
library(rmdformats)

## Global options
options(max.print="31")
opts_chunk$set(cache=TRUE,
               prompt=FALSE,
               tidy=TRUE,
               comment=NA,
               message=FALSE,
               warning=FALSE)
opts_knit$set(width=31)

library(ggplot2)

18

A baker blends 600 raisins and 400 chocolate chips into a dough mix and, from this, makes 500 cookies.

(a)

Find the probability that a randomly picked cookie will have no raisins.

Ans: Let X be the number of raisins on a randomly picked cookie.

\(\lambda = np = 600 \times \frac{1}{500}\)

P(X = 0) = \(\exp^{-\lambda}\)

raisins <- 600
choco_chips <- 400
cookies <- 500
lambda = raisins/cookies
# P (X = 0) meaning 0 raisins on a randomly chosen cookie
prob <- exp(1)^(-lambda)
prob
[1] 0.3011942
ggplot(transform(data.frame(raisins = c(0:10)), p = dpois(raisins, lambda)), aes(raisins, 
    p)) + geom_bar(stat = "identity") + scale_x_continuous(breaks = seq(0, 10, 1))

(b)

Find the probability that a randomly picked cookie will have exactly two chocolate chips.

Ans: Let X be the number of chocolate chips on a randomly picked cookie.

\(\lambda = np = 400 \times \frac{1}{500}\)

P(X = 2) = \(\frac{\lambda^{2}}{2!}e^{-\lambda}\)

\[ \begin{multline*} \begin{split} \end{split} \end{multline*} \]

lambda = choco_chips/cookies
# P (X = 2) meaning 2 chocolate chips on a randomly chosen cookie
prob <- lambda^2/2 * exp(1)^(-lambda)
prob
[1] 0.1437853
ggplot(transform(data.frame(Chocolate_Chips = c(0:10)), p = dpois(Chocolate_Chips, 
    lambda)), aes(Chocolate_Chips, p)) + geom_bar(stat = "identity") + scale_x_continuous(breaks = seq(0, 
    10, 1))

(c)

Find the probability that a randomly chosen cookie will have at least two bits (raisins or chips) in it.

Ans: Let X be the number of bits (chocolate chips or raisins combined) on a randomly picked cookie.

\(\lambda = np = 1000 \times \frac{1}{500}\)

\(P(X >= 2) = 1 - e^{-\lambda} - \lambda e^{-1}\)

lambda = (choco_chips + raisins)/cookies

prob <- 1 - exp(1)^(-lambda) - lambda * exp(1)^(-lambda)
prob
[1] 0.5939942
# To QA: 1 - dpois(0, lambda) - dpois(1, lambda)
ggplot(transform(data.frame(Bits = c(0:10)), p = dpois(Bits, lambda)), aes(Bits, 
    p)) + geom_bar(stat = "identity") + scale_x_continuous(breaks = seq(0, 10, 2)) + 
    scale_y_continuous(labels = scales::label_comma()) + annotate(geom = "text", 
    x = 0, y = 0.1353353, label = ".14", hjust = "left") + annotate(geom = "text", 
    x = 1, y = 0.2706706, label = ".27", hjust = "left")