Aim of this analysis is to illustrate via simulation and associated explanatory text the properties of the distribution of the mean of 40 exponentials (lambda=0.2). Analysis shows:
-where the distribution is centered at and compare it to the theoretical center of the distribution,
-how variable it is and compare it to the theoretical variance of the distribution,
-that the distribution is approximately normal.
First a simulation is set up and a thousand simulated averages of 40 exponentials are made.
lambda=0.2
n=40
mns=NULL
set.seed(1)
for (i in 1 : 1000) {mns[i]= mean(rexp(n, lambda))}
mns=as.data.frame(mns)
First a simulation mean is compared to theoretical mean.
#theoretical mean
1/0.2
## [1] 5
#simulation mean
mean(mns$mns)
## [1] 4.99
Secondly simulation variance is compared to theoretical variance. Also simulation and theoretical standard deviations are compared.
#theoretical standard deviation
1/lambda/sqrt(n)
## [1] 0.7906
#simulation standard deviation
sd(mns$mns)
## [1] 0.7817
#theoretical variance
1/(lambda*lambda)/40
## [1] 0.625
#simulation variance
var(mns$mns)
## [1] 0.6111
As seen from previous analysis that simulation mean is 4.99 and theoretical mean is 5 and they are are approximately same. Also simulation variance is 0.6111 (and standard deviation 0.7817) and this is approximately same with theoretical variance 0.625 (and standard deviation 0.7906).
Third it is seen that exponential distribution is approximately normal. For that a density plot is created where a simulated exponential distributon is compared to normal distibution. Also theoretical mean is marked on the plot (which is approximately same for simulated distribution).
library(ggplot2)
ggplot(mns, aes(x=mns))+
geom_histogram(aes(y=..density..),binwidth=0.2, color="blue", fill="white")+
geom_vline(xintercept=5, color="red", linetype="dashed", size=1)+
#create a normal distribution density plot with theoretical mean and sd
stat_function(fun = dnorm, arg = list(mean = 5, sd = sd(mns$mns)))+
xlab("x")+
ylab("Density")+
ggtitle("Distribution of 1000 simulated averages of 40 exponentials
\n vs normal distribution")+
theme_minimal()