Here I am going 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. Here we take lambda equal to “0.2”.Now I go question wise.
We are taking 1000 samples and 40 as the size of the one sample.So that we will have 1000 mean one from the each sample.
#As we know that theoretical mean of exponential distribution is 1/lambada ie.1/0.2= 5.
theomean<-1/0.2
theomean
## [1] 5
sampsize<-40
numsamp<-1000
lambada<-0.2
First we see hoW “Exponential Dstribution”" looks like.I am taking here of size 40.In The Figure we can see the exponential nature of the histogram.
set.seed(11)
exp1<-rexp(1000,0.2)
hist(exp1,breaks =50,col="green",probability = T)
lines(density(exp1),lwd=3,col="blue")
Now we Take 1000 samples of size 40 and plot the distribution of means of each samples
set.seed(12)
means<-NULL
for(i in 1:numsamp){
means<-c(means,mean(rexp(sampsize,lambada)))
}
hist(means,breaks=50,col="blue",main = "Distribution of 1000 sample means")
Now we will compare the theoretical mean and sample mean of the Distribution
#Theoretical mean is theomean
theomean
## [1] 5
#Sample mean
hist(means,breaks=50,col="grey",main = "Distribution of 1000 sample means")
abline(v=mean(means),lwd=4,col="red")
text(7,40, paste("Actual mean = ", round(mean(means),4), "\n Theoretical mean = 5" ), col="red")
The theoretical standard distribution will be “1/lambda/sqrt(n)”
# theoretical standard deviation vs practical standard deviation
print (paste("Theoretical standard deviation: ", round( (1/lambada)/sqrt(sampsize) ,4), ", Practical standard deviation", round(sd(means) ,4) ) )
## [1] "Theoretical standard deviation: 0.7906 , Practical standard deviation 0.7741"
# the variance is
print (paste("Theoretical variance: ", (1/lambada)^2/sampsize, ", Practical variance", round(var(means) ,4) ) )
## [1] "Theoretical variance: 0.625 , Practical variance 0.5992"
So we can say that both are approximetly equal
We can see it from the histogram itself that it is nearly equal.For clearity I have used the density function.
hist(means,breaks=50,col="orange",main = "Distribution of 1000 sample means",probability = T)
lines(density(means), lwd=3, col="blue")