# Central Limit Theorem set.seed(532) #The CLT is a very powerful result that says the sample mean of # i.i.d. RVs is approximately normal if take large enough samples # Simulation using standardized RV for Bernoulli seq (p.378) # Should converge to standard normal RV p <- 1/2 cltsequence <- function(n) (mean(rbinom(n, 1, p))-p) * (2*sqrt(n)) n <- 1000 simlist <- replicate(10000, cltsequence(n)) hist(simlist, prob = TRUE,xlab="Z-score",main="Histogram of CLT simulation") curve(dnorm(x), -4, 4, add = TRUE) # Sum of 6 dice is approx normal (p. 383) nsim <- 100000 ndice <- 6 mu <- ndice*3.5 sigma <- sqrt(sum(((1:6)-3.5)^2)*1/6*ndice) simlist <- replicate(nsim, (sum(sample(1:6, ndice, rep=TRUE))-mu)/sigma) hist(simlist[abs(simlist)<4],breaks=seq(from=-4,to=4,by=.25),freq=FALSE,xlab="Z-score",main="Histogram of CLT simulation") curve(dnorm(x), -4, 4, add = TRUE) # Test whether our simulation of the normal distribution follows the # 68-95-99.7 rule for standard normal RV (mean 0, SD 1): sum(-1 <=simlist & simlist <= 1)/n # proportion within one SD sum(-2 <=simlist & simlist <= 2)/n # proportion within two SD sum(-3 <=simlist & simlist <= 3)/n # proportion within three SD