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)