#
#   The N(m=1000*0.45, sigma=1000*0.45*.55) is indeed a good
#   approximation of the Binomial(p=0.45,n=1000)
#
a <- rbinom(n=100000,prob=0.45,size=1000)
max(a)
hist(a,freq=TRUE)
hist(a)
hist(a,probability=TRUE)
hist(a,probability=TRUE,breaks=30,main="One million binomial observations")
curve(dnorm(x,mean=450,sd=sqrt(1000*.45*.55)),
      from=350,to=560,col="red",add=TRUE)
#
#   Finding the maximum amount of overbooking, if we want probability of
#   excess people <= 0.01. (Problem 3, handout lectures February, 6 and 9).
#
#   Remember we had to solve
#
#     340.5 - 0.85n = 2.326348*sqrt(n)*sqrt(0.15*0.85)
#
#   We only have to pass uniroot the functions whose roots we want and an
#   interval to search in:
#
uniroot( f= function(n) { 340.5 - 0.85*n - 2.326348*sqrt(0.15*0.85)*sqrt(n) },
         interval=c(340,400))
#
#   We can also solve by trial and error as well (see handout February, 9).
#
ratio <- function(n) { (340.5 - 0.85*n) / sqrt(0.15*0.85*n) }
#
#   We want the largest possible n such that the function is not below 2.3263
#
ratio(350)
ratio(370)
ratio(380)
ratio(381)
ratio(382)
