# 2014-02-24 # simple Illustration for # the Central Limit Theorem # # I start from a large sample # with uniform distribution # in range 0-1 # sample size n<-10000 # how many variables # should be averaged nsum<-1000 rhelp<-array(0,dim=c(n)) # LOOP OVER SUMMATION # runif() produced independent random # numbers between 0 and 1 for (i in 1:nsum){ x<-runif(n) rhelp<-rhelp+x } y<-rhelp/nsum ymean<-mean(y) ysd<-sd(y) print(paste("average of ",nsum," uniformly distributed variables")) print(paste("mean, sd. of the average:",round(ymean,4),round(ysd,4))) # plot of the histrogram # using 51 break points from 0-1 # using density as y-axis x<-1:51 x<-(x-1)/50 hist(y,breaks=x,prob=TRUE,main=paste("Central Limit Demo: nsum=",nsum)) # load the Gaussian distribution function source("scripts/myfunctions.R") yg<-fgauss(x,center=ymean,s=ysd) lines(x,yg,col=3,lwd=2)