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. Set lambda = 0.2 for all of the simulations.
In this project, we are tasked two parts.
library(ggplot2)
set.seed(1)
lambda <- 0.2
n <- 40
num_sim <- 1000
#exp_data <- rexp(num_sim, lambda) #the exponential distribution
mean_data <- NULL
for (i in 1 : num_sim) mean_data= c(mean_data, mean(rexp(n, lambda))) #The mean of exponential distribution
1. Show the sample mean and compare it to the theoretical mean of the distribution.
The mean of the exponential distribution is 4.990025.
mean(mean_data)
## [1] 4.990025
The theoretical mean is 5.
1 / lambda
## [1] 5
2. Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution.
The sample variance of mean X is 0.6111165.
var(mean_data)
## [1] 0.6111165
The theoretical variance is 0.625.
(1 / lambda ^ 2) / n
## [1] 0.625
3. Show that the distribution is approximately normal.
g <- ggplot(data = data.frame(mean_data), aes(x=mean_data))
g + geom_histogram(aes(y = ..density..), bins = 40) +
stat_function(fun = dnorm, args = list(mean = 1 / lambda, sd = sqrt(1 / lambda ^2 / n))) +
xlab(expression(bar(X))) +
ggtitle("Comparison of sample distribution and theoretical")
The above figure shows the distribution of sample mean is matched with a normal distribution.