This project investigates the Exponential distribution in R and is compared with the Central Limit Theorem. The Exponential Distribution in R is simulated using the function rexp(n,lambda), where n is the number of exponentials and lambda is the rate.The mean aswel as Standard Deviation is 1/lambda.
The average of 40 exponentials over 1000 simulations is calculated and the Sample mean and Variance is compared to their respective Theoretical values. The distribution is also shown to be approximately Normal.
# Setting the values
set.seed(2000)
nosim<-1000 # Number of Simulations
n<-40 # Number of Exponentials
lambda<-0.2 # Value of lambda
# Calculating mean value of the 1000 simulations
meansimul<-as.numeric(lapply(1:nosim,function(i){mean(rexp(n,lambda))}))
#Calculating the mean of the entire distribution
sample_mean<-mean(meansimul)
#Calculating Theoretical Mean of the distribution
theoretical_mean<-1/lambda
values<-c(sample_mean,theoretical_mean)
values
## [1] 5.02941 5.00000
The sample mean is 5.02941 which is very close to the theorectical mean value which is 5 .
sample_sd<-sd(meansimul)
theo_sd<-(1/lambda)/sqrt(n)
sample_var<-sample_sd^2
theo_var<-theo_sd^2
val<-c(sample_var,theo_var)
val
## [1] 0.6168082 0.6250000
The value of sample variance 0.6168 is close to the theorectical variance value of 0.625.Thus the simulated and the actual Variance are close to each other.
# Plot to show the comparison of Sample mean and Theoretical mean.
#Scaling the values of mean
means<-scale(meansimul)
#plot histogram and density of scaled means
hist(means,freq=FALSE,col="light grey",border="grey",
main="Histogram for Sample Mean",ylim=c(0,0.4),
xlab="Mean of each Simulation")
lines(density(means))
#overlay the standard normal curve in blue for comparison
curve(dnorm(x,0,1), -3, 3, col='blue', add=T)
abline(v=mean(means),col='red',lwd=2)
The black curve is the one that fits the Simulated Mean Distribution and the blue curve is the Normal Distribution curve. It can be said that the simulated distribution is approximately normally distributed centred at the Red vertical line.
# The quantile-quantile plot
qqnorm(means)
qqline(means,col=2)