This document shows a simulation where 40 elements of an exponential distribution are created, and then the mean of each sample is calculated; this process will be repeated 10,000 times. Finally an analysis is made regarding the mean and variance of the 10,000 means.
The first step of the process is to define the parameters of the distribution, also, the theoretical values of the mean and variance are calculated.
lambda=0.2
n=40
th.mean=1/lambda
th.var=(1/lambda)^2/n
set.seed(1234)
The next step is to create the simulation so we can gather the 10,000 means from the distribution.
means=NULL
for (i in 1:10000)
means=c(means,mean(rexp(n,lambda)))
Once that the simulation has finished, we can compare the theoretical mean with the simulation mean.
th.mean
## [1] 5
mean(means)
## [1] 5.005646
As we can see, the values are quite similar.
Also we do the same comparison with the values for the variance.
th.var
## [1] 0.625
var(means)
## [1] 0.6146077
In this case the values are almost the same as well.
Finally, a density graph is made from the simulated means with the purpose of showing that the distribution approaches a normal curve.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.4
means=data.frame(means)
ggplot(means,aes(x=means))+geom_histogram(aes(y=..density..),fill="steelblue1",col="gray40")+
geom_density(col="red",size=1)+geom_vline(xintercept = th.mean,col="black",size=1.5)+
labs(title="Density of Means", x="Mean", y="density")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.