Visualizing the Law of Large Numbers using #gganimate

The Law of Large Numbers

The law of large numbers is a theorem that describes the result of performing the same experiment a large number of times.According to this law, the average of the results obtained from a large number of trials should be close to the expected value, and will tend to be closer as more trials are performed.

$\bar{x_{n}} \to \mu$ as ${n} \to \infty$

Visualizing Law of Large Numbers using coin flips

Bias = 0.5

For a simple example, we will use an unbiased coin.This means that the probability of getting a head/tail is 0.5. As the number of coin tosses increases, the average proportion of heads/tails achieved would be close but not equal to 0.5.


n = 500
pHeads =0.5

flipSequence = sample( x=c(0,1),prob = c(1-pHeads,pHeads),size=n,replace=T)

r = cumsum(flipSequence)
n= 1:n
runProp = r/n

flip_data <- data.frame(run=1:500,prop=runProp)
g <- ggplot(flip_data,aes(x=run,y=prop,frame=run)) +geom_path(aes(cumulative=T))+xlim(1,500)+ylim(0.0,1.0)+geom_hline(yintercept = 0.5)+ggtitle("Running Proportion Heads of Unbiased Coin")+ylab("Proportion of Heads")+xlab("Flip Number")
gganimate(g,"attempt_cumulative.mp4",title_frame = F,interval=0.1)

The average proportion of heads eventually converge to 0.5. What about other biases? Before we start, let’s define a function to produce the simulation

flips_simulation <- function(n,pHeads,seed,name_of_file){

if (!require("ggplot2")) install.packages("ggplot2")

flipSequence = sample(x=c(0,1),prob=c(1-pHeads,pHeads),size=n,replace=T)
runProp = r/(1:n)
data_flip <- data.frame(index=1:length(runProp),runProp)

g <- ggplot(data_flip,aes(x=index,y=runProp,frame=index)) +geom_path(aes(cumulative=T))+xlim(1,n)+ylim(0.0,1.0)+geom_hline(yintercept = pHeads)+ggtitle(paste("Running Proportion Heads of Coin of Bias"),pHeads)+ylab("Proportion of Heads")+xlab("Flip Number")
gganimate(g,paste(name_of_file,".mp4",sep=""),title_frame = F,interval=0.1)


In the above function, the inputs are n(Number of Trials),pHeads(probability of head),seed(seed value for reproducibility) and name_of_file(name of your .mp4 file).Now let’s run this function for different values of bias and number of trials.

Bias = 0.6 , n=1000


Bias = 0.4,n=100


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s