In this project, we will explore the properties of the exponential distribution through simulations in R. The focus will be on the mean and variance of the distribution of averages of 40 exponentials. Additionally, we will compare the distribution of a large collection of random exponentials with the distribution of averages of 40 exponentials to observe the impact of the Central Limit Theorem.
In this section, we utilized the rexp(n, lambda)
function in R to simulate data from an exponential distribution. The
parameter n represents the number of random variables to generate, and
lambda is the rate parameter, governing the shape of the distribution.
For consistency, we set lambda to 0.2 for all simulations.
# R Code for Simulations
lambda <- 0.2
simulations <- matrix(rexp(40 * 1000, rate = lambda), nrow = 1000)
sample_means <- rowMeans(simulations)
# R Code for Sample Mean versus Theoretical Mean
theoretical_mean <- 1/lambda
hist(sample_means, main = "Distribution of Sample Means", xlab = "Sample Mean", col = "lightblue")
abline(v = theoretical_mean, col = "red", lwd = 2)
legend("topright", legend = c("Sample Mean", "Theoretical Mean"), col = c("black", "red"), lwd = 2)
# R Code for Sample Variance versus Theoretical Variance
theoretical_variance <- (1/lambda)^2
hist(apply(simulations, 1, var), main = "Distribution of Sample Variances", xlab = "Sample Variance", col = "lightgreen")
abline(v = theoretical_variance, col = "blue", lwd = 2)
legend("topright", legend = c("Sample Variance", "Theoretical Variance"), col = c("black", "blue"), lwd = 2)
# R Code for Distribution Comparison
par(mfrow = c(1, 2))
hist(runif(1000), main = "Random Uniform Distribution", col = "lightblue")
hist(sample_means, main = "Distribution of Sample Means", col = "lightgreen")