# Demonstration of the law of large numbers
# it shows how the empirical probability to throw a six with a dice approximates the theoretical probability of 1/6 with increasing trials
# for more R-scripts: www.random-stuff.de
pdf("law_of_large_numbers.pdf", width=10, height=6)
par(bty="n", mar=c(6.3, 7, 3, 3), mgp=c(4,1,0))
theory <- 1/6
y <- c()
empirical <- c()
x <- c(1:5000)
for(i in x) {
dice <- sample(1:6,1)
if(dice == 6) { y <- c(y,1)
} else { y <- c(y,0) }
empirical <- c(empirical, sum(y)/length(y))
}
plot(x, empirical, type="l", xlab=c("number of trials n in an independent experiment"), ylab = expression(paste("relative frequency ", H(A) == n[A]/n)), ylim=c(0,0.3), xlim=c(0,length(x)), lwd=1)
box(which = "inner", lty = "solid")
abline(a= theory, b=0, col="red")
#quartz.save("law_of_large_numbers.pdf", type="pdf") # for Mac PCs
dev.off()
print(paste("Plot was saved in:", getwd()))