knitr::opts_chunk$set( echo = TRUE,
message = TRUE,
warning = TRUE, fig.width = 6,
fig.height = 4,
dpi = 96
)
set.seed(123)
Part 1: Simulation of Exponential Distribution (CLT)
I will simulate the mean of 40 exponential random variables and show that the distribution of the mean is approximately normal, illustrating the Central Limit Theorem. lambda <- 0.2 # Rate parameter n <- 40 # Sample size sims <- 1000 # Number of simulations
sim_means <- replicate(sims, mean(rexp(n, lambda)))
sample_mean <- mean(sim_means) theoretical_mean <- 1 / lambda sample_var <- var(sim_means) theoretical_var <- (1 / lambda)^2 / n
list( sample_mean = sample_mean, theoretical_mean = theoretical_mean, sample_variance = sample_var, theoretical_variance = theoretical_var ) hist(sim_means, probability=TRUE, main=“Distribution of Sample Means (n=40)”, xlab=“Sample Mean”, col=“lightblue”, border=“white”) abline(v=sample_mean, col=“blue”, lwd=2) abline(v=theoretical_mean, col=“red”, lwd=2) legend(“topright”, legend=c(“Sample Mean”, “Theoretical Mean”), col=c(“blue”,“red”), lwd=2) qqnorm(sim_means) qqline(sim_means, col=“red”)
lambda <- 0.2 n <- 40 sims <- 1000
sim_means <- replicate(sims, mean(rexp(n, lambda)))
sample_mean <- mean(sim_means) theoretical_mean <- 1 / lambda sample_var <- var(sim_means) theoretical_var <- (1 / lambda)^2 / n
list( sample_mean = round(sample_mean, 3), theoretical_mean = theoretical_mean, sample_variance = round(sample_var, 3), theoretical_variance = theoretical_var ) hist(sim_means, probability=TRUE, main=“Distribution of Sample Means (n=40)”, xlab=“Sample Mean”, col=“lightblue”, border=“white”) abline(v=sample_mean, col=“blue”, lwd=2) abline(v=theoretical_mean, col=“red”, lwd=2) legend(“topright”, legend=c(“Sample Mean”,“Theoretical Mean”), col=c(“blue”,“red”), lwd=2)