# RE: [R] Using R to illustrate the Central Limit Theorem

From: Ted Harding <Ted.Harding_at_nessie.mcc.ac.uk>
Date: Fri 13 May 2005 - 23:00:00 EST

On 13-May-05 Bliese, Paul D LTC USAMH wrote:
> Interesting thread. The graphics are great, the only thing that
> might be worth doing for teaching purposes would be to illustrate
> the original distribution that is being averaged 1000 times.
>
> Below is one option based on Bill Venables code. Note that to do
>
> N <- 10000
> for(k in 2:20) {
> graphics.off()
> par(mfrow = c(2,2), pty = "s")
> hist(((runif(k))-0.5)*sqrt(12*k),main="Example Distribution 1")
> hist(((runif(k))-0.5)*sqrt(12*k),main="Example Distribution 2")
> m <- replicate(N, (mean(runif(k))-0.5)*sqrt(12*k))
> hist(m, breaks = "FD", xlim = c(-4,4), main = k,
> prob = TRUE, ylim = c(0,0.5), col = "lemonchiffon")
> pu <- par("usr")[1:2]
> x <- seq(pu, pu, len = 500)
> lines(x, dnorm(x), col = "red")
> qqnorm(m, ylim = c(-4,4), xlim = c(-4,4), pch = ".", col = "blue")
> abline(0, 1, col = "red")
> Sys.sleep(3)
> }
>
> By the way, I should probably know this but what is the logic of
> the "sqrt(12*k)" part of the example? Obviously as k increases
> the mean will approach .5 in a uniform distribution, so
> runif(k)-.5 will be close to zero, and sqrt(12*k) increases as
> k increases. Why 12, though?

The reason is indeed simple! In demonstrating the convergence of the distribution of mean(k X's) to a Normal distribution, the reference (i.e. the limiting distribution) is N(0,1), which has mean 0 and variance 1. Therefore, in comparing the distribution of mean(k X's) with N(0,1) it needs to be standardised to itself have mean 0 and variance 1. As you've already spotted, you standardise for the mean by subtracting 0.5; to standardise for the variance you need to divide by sqrt(variance(mean(k X's))).

This is sqrt(variance(X)/k). Finally (and this is where the "12" comes in), the variance of an X uniformly distributed on (0,1) is 1/12 (left as an exercise for the reader ... ). Hence 12*k.

Best wishes,
Ted.

E-Mail: (Ted Harding) <Ted.Harding@nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861
```Date: 13-May-05                                       Time: 13:43:54
------------------------------ XFMail ------------------------------

______________________________________________
```
R-help@stat.math.ethz.ch mailing list