Ex.14 A restaurant feeds 400 customers per day. On the average 20 percent of the customers order apple pie.
n <- 400 #number of customers
p <- 0.2 #probability for a customer to order apple pie
Based on Central Limit Theorem,
\(E[X]\) = \(np\)
\(sd(X)\) = \(\sqrt{\frac{p(1-p)}{n}}\)
expected.value <- n*p #expected value for Bernoulli Trails
sd <- sqrt(n*p*(1-p)) #standard deviation for Bernoulli Trails
percent <- 0.95
z_score <- round(qnorm(1 - (1 - percent)/2), 2) #compute the z-score for 95% confidence interval
lower_bound <- expected.value - sd * z_score #lower end of the condidence interval
upper_bound <- expected.value + sd * z_score #upper end of the confidence interval
c(lower_bound,upper_bound)
## [1] 64.32 95.68
\(E[X]\) = \((400)(0.2)\) = \(80\)
\(sd(X)\) = \(\sqrt{(400)(0.2)(0.8)}\) = \(8\)
The number of pieces of apple pie ordered on a given day such that we are 95% sure that the actual number will fall within 64 and 96.
\(SD=\sqrt{\frac{p(1-p)}{n}}\)
95% confidence interval: \((p-2\sqrt{\frac{p(1-p)}{n}} < \bar{p} < p+2\sqrt{\frac{p(1-p)}{n}})\) = \((p-2SD < \bar{p} < p+2SD)\) \(\Rightarrow\) \(SD = \frac{p - 19\%}{2} = \frac{21\%-p}{2}\)
\(SD=\sqrt{\frac{p(1-p)}{n}}\) \(\Rightarrow\) \(n = \frac{p(1-p)}{SD^2}\)
sd <- (p-0.19)/2
n <- (p*(1-p))/sd^2
Verify the result:
n #number of customers
## [1] 6400
expected.value <- n*p #expected value for Bernoulli Trails
sd <- sqrt(n*p*(1-p)) #standard deviation for Bernoulli Trails
lower_bound <- expected.value - sd * z_score #lower end of the condidence interval
upper_bound <- expected.value + sd * z_score #upper end of the confidence interval
c(lower_bound,upper_bound)
## [1] 1217.28 1342.72
c(round(lower_bound/n,2),round(upper_bound/n,2))
## [1] 0.19 0.21
It requires 6400 customers on the average, for the restaurant to be at least 95% sure that the number of customers ordering pie on that day falls in the 19% (1217 customers) to 21% (1343 customers) range.