Chapter 5.1 - Exercise 18

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

raisins <- 600
cookies <- 500
cchips <- 400

  1. Find the probability that a randomly picked cookie will have no raisins.
# expected average raisins per cookie
lambda <- raisins / cookies

# cookies with zero raisins
x <- 0

# using formula
px <- (lambda ^ x * exp(1) ^ -lambda) /  factorial(x)
px
## [1] 0.3011942
# check
dpois(x, lambda)
## [1] 0.3011942

  1. Find the probability that a randomly picked cookie will have exactly two chocolate chips.
# expected average number of chocolate chips per cookie
lambda <- cchips / cookies

# cookies with 2 chocolate chips
x <- 2

# using formula
px <- (lambda ^ x * exp(1) ^ -lambda) /  factorial(x)
px
## [1] 0.1437853
# check
dpois(x, lambda)
## [1] 0.1437853

  1. Find the probability that a randomly chosen cookie will have at least two bits (raisins or chips) in it.
# expected average number of goodies per cookie
lambda <- (raisins + cchips) / cookies

# cookies with 2 bits
x <- 2

# using formula
px <- (lambda ^ x * exp(1) ^ -lambda) /  factorial(x)
px
## [1] 0.2706706
# check
dpois(x, lambda)
## [1] 0.2706706

A look at the distribution of any bits

for (i in 0:10) {

  temp_df <- data.frame(bits = i, p = dpois(i, lambda))
  
  if (i == 0) {
    
    df <- temp_df
    
  } else {
    
    df <- bind_rows(df, temp_df)
    
  }

}

ggplot(df, aes(x = bits, y = p)) +
  geom_col() +
  scale_x_continuous(breaks = seq(0, i, 1)) +
  scale_y_continuous(breaks = seq(0, max(df$p), .05)) +
  theme_bw()