The purpose of this exercise is to investigate the exponential distribution in R and compare it with the Central Limit Theorem. The exponential distribution can be simulated in R with rexp(n, lambda) where lambda is the rate parameter. The mean of exponential distribution is 1/lambda and the standard deviation is also 1/lambda. Lambda is set to 0.2 for all of the simulations. This simulation will investigate the distribution of averages of 40 exponentials for a thousand simulations.
lambda<-0.2
n<-40
sims<-1000
set.seed(42)
##Run the simulations
run<-replicate(sims, rexp(n,lambda))
means<-apply(run,2,mean)
##Plot the mean distribution
hist(means,breaks=50,xlim=c(2,9),main="Histogram of Means Distribution")
##Plot the theoretical mean
abline(v=mean(means),lwd=3,col="blue")
print(paste("The theoretical mean is",round(mean(means),3),sep=" "))
## [1] "The theoretical mean is 4.987"
##Compare the variance and standard deviation
print(paste("The theoretical standard deviation is",round((1/lambda)/sqrt(n),3)),sep=" ")
## [1] "The theoretical standard deviation is 0.791"
print(paste("The simulated standard deviation is",round(sd(means),3)),sep=" ")
## [1] "The simulated standard deviation is 0.797"
print(paste("The theoretical variance is",round(((1/lambda)/sqrt(n))^2,3)),sep=" ")
## [1] "The theoretical variance is 0.625"
print(paste("The simulated variance is",round(sd(means)^2,3)),sep=" ")
## [1] "The simulated variance is 0.634"
##Compare the simulated distribution curve to the normal curve
hist(means,breaks=50,xlim=c(2,9),main="Histogram of Means Distribution")
x.fit<-seq(min(means), max(means), length = 1000)
y.fit<-dnorm(x.fit, mean = 1/lambda, sd = 1/lambda/sqrt(n))
lines(x.fit, y.fit*125, lty=2,col="blue")
As seen in this exercise, the sample mean and the theoretical mean are extremely close to one another as are the theoretical vs. simulated standard deviation and variance. These simulations were done without replacement and with a set.seed of 42.