This project investigates the exponential distribution and its properties in R, comapring it to the Central Limit Theorem. The following sections illustrate via simulation the properties of the distribution of the mean of 40 exponentials.The target is to:
Show the sample mean and compare it to the theoretical mean of the distribution.
Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution.
Show that the distribution is approximately normal.
set.seed(105)
lam=0.2 #lambda
n=40
#initate means vector
ms=NULL
#loop 1000 times and calculate the mean each time for 40 exponentials with lambda=0.2 (mean=std=5)
for (i in 1 : 1000)
{ms = c(ms, mean(rexp(n,lam)))}
#theoretical mean
thmean<-1/lam
#sample mean
smean<-mean(ms)
The theoretical mean is 1/lambda= 5. On the other hand the sample mean for 40 exponentials with 1000 simulations=5.0073159. It is clear that the sample mean is close to the theoretical mean.
If we simulate lots of exponentials to investigate the Law of Large Numbers, we can see that the cumulative mean converges to the population mean. This indicates a good estimator as it is consistent.
set.seed(105)
means<-cumsum(rexp(1000,0.2))/(1:1000)
plot(means,type="l",xlab ="Number of Observations", ylab="Cumulative Mean",main = "Cumulative Mean from Exponential Distribution Simulation")
abline(h=5,col="red")
thvar<-(1/lam)^2/n
svar<-var(ms)
The theoretical variance is 1/(lambda^2*n)= 0.625. On the other hand the sample variance= 0.6659851. It is clear that the sample mean is close the the theoretical mean.
hist(ms,probability = T, col="gray", xlab="Means", main="Result of 1000 Simulations of 40 Exponentials")
curve(dnorm(x,mean(ms),sd(ms)),add=TRUE,col="red")
By simulating the exponential distribution we can see that the sample mean converges to the theoretcal mean as we increase the number of samples. The sample variance also is close to the theoretical value and the distribution of the means is close to a normal distribution.