This document shows some commands useful to graphically “verify” the central limit theorem in the case of a uniform random variable.

The following runif function returns 5 random numbers from the uniform distribution in the range [0, 1]:

runif(5)

The following command replicates 3 times the same runif operation:

(Three_Uniforms <- replicate(3, runif(5)))
[,1]       [,2]      [,3]
[1,] 0.02060323 0.04297248 0.1098457
[2,] 0.51377947 0.60048804 0.7811531
[3,] 0.70079159 0.47820034 0.5956885
[4,] 0.09655685 0.37621419 0.3623097
[5,] 0.96948797 0.50981652 0.1081516

Since we are interested in whatever linear combination of these three uniforms, we can consider their mean. Since each variable is in a column, the mean is computed row-wise:

rowMeans(Three_Uniforms)
[1] 0.05780712 0.63180687 0.59156014 0.27836025 0.52915202

We can combine the following commands in a unique “longer” call:

rowMeans(replicate(3, runif(5)))
[1] 0.1951196 0.5269952 0.2666821 0.6649161 0.1958799

NOTE!! The numbers are random, that’s why you obtain different values between the two previous calls.

Let us now generalize to the case of 30 Uniform r.v.’s. We now use 1000 numbers (instead of 5), so to have a better approximation:

num_variables <- 30
num_replications <- 1000
row_wise_means <- rowMeans(replicate(num_variables, runif(num_replications)))

Following the histogram of the mean of the Uniforms. This mean approximately follows a Normal Distribution, as you can see from the superimposed red line. In order to draw such a line, I chose proper values for the mean and the standard deviation… How did I choose them?

library(MASS) # needed for the truehist function
x <- seq(min(row_wise_means), max(row_wise_means), length.out = 200)
truehist(row_wise_means)
lines(x, dnorm(x, mean = 0.5, sd = 1/(sqrt(12)*sqrt(30))), col = 'red', lwd = 1.5)

You are invited to change the original distribution you sample from and/or the number of variables, in order to check the effects on the convergence.

LS0tCnRpdGxlOiAiQSBncmFwaGljYWwgZXhwbG9yYXRpb24gb2YgdGhlIGNlbnRyYWwgbGltaXQgdGhlb3JlbSIKYXV0aG9yOiAiRG9tZW5pY28gVmlzdG9jY28iCmRhdGU6ICJCUyBDbGFzcyAtIExlY3R1cmUgb2YgMTAtMTEvMTAvMjAxNiIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQoKVGhpcyBkb2N1bWVudC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