MATH 3510 Probability Teaching

Law of Large Numbers and Simulations – MATH 3510

Comments on Law of Large Numbers and simulations

Simulation

Below you can find the code in R that generated the above animation

library(ggplot2)
library(gganimate)
library(gifski)

# Number of tosses
number_of_tosses <- 1000

outcome <- sample(c(0,1), size=number_of_tosses, replace = TRUE)

proportion_of_heads_at_each_time <- cumsum(outcome)/1:number_of_tosses

df <- data.frame(proportion_of_heads_at_each_time)

g <- ggplot( df, aes(x=1:number_of_tosses,y=proportion_of_heads_at_each_time)) + 
  labs(x="Number of tosses", y= "Proportion of heads") +
  ylim(c(0.1,0.8))+
  geom_hline(yintercept = 0.5, show.legend = TRUE, linetype = "dashed")+
  geom_line() +
  geom_point() +
  geom_text(aes(label=round(fracs,digits = 3)),hjust=-0.2, vjust=-0.2) +
  transition_reveal(1:steps) +
  ease_aes('sine-in-out') +
  theme_light()+
  theme(panel.border = element_blank())

animate(g, renderer = gifski_renderer(), duration = 15, width=800, height=600)

  anim_save(DIRECTORY_NAME_TO_SAVE_THE_FILE)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: