Simulation of exponential distribution in R and comparisom with Central Limit Theorem (tgjoen)

This R program simulate 40 random exponentials with lambda 0.2 and records samples mean,variance and standard deviation (one example is shown below). These simulated statistics are compared with the theoretical values (mean = 5, var = 25 and sd = 5). Definition of vector variables for simulated means, variations and standard deviations:

set.seed(123)
sim_means = NULL
sim_var = NULL
sim_sd =NULL
Exponential<-rexp(40,.2)
hist(Exponential, breaks =20,col="grey",xlab="Random numbers from an exponential with n=40, lambda =0.2")

Run 1000 simulations of exponential distributions and sample all the means, variances and standard deviations into vector variables

for (i in 1:1000) sim_means = c(sim_means,mean(rexp(40,.2)))
for (i in 1:1000) sim_var = c(sim_var, var(rexp(40,.2)))
for (i in 1:1000) sim_sd = c(sim_sd, sd(rexp(40,.2)))
msn<-mean(sim_means,1)
vsn<-mean(sim_var)
sdn<-mean(sim_sd)
nor_means<-rnorm(1000,5,5)

Graphical representations of the distributions of the simulated statistics (means from simulation (left) compared to 1000 normally distributed numbers centered around 5 with sd=5 (right). Theoretical value av orange vertical line

par(mfrow=c(1,2))
hist(sim_means, breaks = 20,col ="green", prob=TRUE)
lines(density(sim_means),lwd=4)
abline(v=5, lwd=3,col="orange")
hist(nor_means, breaks=20,col="grey", prob=TRUE)
lines(density(nor_means),lwd=4)
abline(v=5, lwd=3,col="orange")

###Graphical representations of the distributions of the simulated statistics from simulation (variations left) and standard deviation (right). Theoretical value av orange vertical line

par(mfrow=c(1,2))
hist(sim_var, breaks = 20, col="blue")
abline(v=25, lwd=3,col="orange")
hist(sim_sd, breaks = 20, col="red")
abline(v=5, lwd=3,col="orange")

##The mean of the simulated mean , var and sd values are 4.98, 24.77 and 4.88, respectively. This is compared to the teoretical values 5, 25 and 5. The figures shows that simulated mean and standard deviation are quite close to normal distribution around theoretical value , whereas variation is more “off”.