9.1 Central Limit Theorem, Question 11

11 Write a computer program to simulate 10,000 Bernoulli trials with probability .3 for success on each trial. Have the program compute the 95 percent confidence interval for the probability of success based on the proportion of successes. Repeat the experiment 100 times and see how many times the true value of .3 is included within the confidence limits.

set.seed(28347859)
sim_results <- vector()
count_of_outside <- vector()
for(j in seq(1,1000,1)){
count <- 0 
for(i in seq(1,100,1)){
  
  n = 10000
  prob = .3
  q= 1-prob
  sdev <- sqrt(prob*q/n)
  lb <- prob- 2*sdev
  ub <- prob + 2*sdev
  rber<-rbern(n,prob)
  result = sum(rber)/n
  if(( result <= lb ) | (result >= ub)){
    
    count= count+1
  }
  sim_results[i]<-result
  
}
count_of_outside[j] <- count
}
out_of_bounds <- mean(count_of_outside)
max_times_ob <-max(count_of_outside)
min_times_ob <- min(count_of_outside)

From the simulation we can see that the average amount of times that 0.3 was not between 0.29083 and 0.30917 was 4.627 per cycle. The most amount of times that 0.3 was not inside the bounds in a cycle was 13, while the minimum amount was 0.

results_df <-as.data.frame(sim_results)
ggplot(results_df,aes(x=sim_results))+
  geom_density()+
  geom_vline(aes(xintercept=lb,color='red'))+
  geom_vline(aes(xintercept=ub,color='red'))+
  labs(title='Bernoulli Simulation')+ 
  theme(plot.title = element_text(hjust = 0.5))+
  geom_text(aes(x=round(lb,5), label=paste0("Lower CI Bound\n",round(lb,5),collapse = ''), y=40), colour="black", angle=90,check_overlap=T) +
  geom_text(aes(x=round(ub,5), label=paste0("Upper CI Bound\n",round(ub,5),collapse = ''), y=40), colour="black", angle=90, check_overlap=T)