Perform the simulation
no_sim <- 1000
lambda <- 0.2
n <- 40
set.seed(123)
sim_matrix <- matrix(rexp(no_sim *n, rate=lambda), no_sim, n)
sim_mean <- rowMeans(sim_matrix)
hist(sim_mean, col ="purple", main = "Histogram of simulation mean distribution", xlab = "Simulation mean")
simulation_mean <- mean(sim_mean)
theory_mean <- 1/lambda
The simulation mean is 5.0119113 while the theoretical mean with lambda (0.2) is 5. It is indicated that the simulation mean is very close to the theoretical mean. ##Variance Comparsion
simulation_variance <- var(sim_mean)
theory_variance <- (1/lambda)^2/n
simulation_sd <- sd(sim_mean)
theory_sd <- sqrt(theory_variance)
The simulation variance is 0.6088292 while the theoretical variance with lambda (0.2) is 0.625. It is indicated that the simulation variance is very close to the theoretical variance ## Distribution (Approximately normal)
plotdata <- data.frame(sim_mean);
histplot <- ggplot(plotdata, aes(x =sim_mean))
histplot <- histplot + geom_histogram(aes(y=..density..), colour="black", fill = "yellow");
histplot <- histplot + labs(title="Density of 40 Numbers from Exponential Distribution", x="Mean of 40 Selections", y="Density")
histplot <- histplot + geom_vline(xintercept=simulation_mean,size=1.0, color="black")
histplot <- histplot + geom_vline(xintercept=theory_mean,size=1.0, color="blue", linetype = "longdash")
histplot <- histplot + stat_function(fun=dnorm,args=list(mean=theory_mean, sd=theory_sd),color = "red", size = 1.0)
histplot <- histplot + stat_function(fun=dnorm,args=list(mean=simulation_mean, sd=simulation_sd),color = "green", size = 1.0)
histplot
#Compare Conf. Interval.
sim_conf_interval <- round (mean(sim_mean) + c(-1,1)*1.96*sd(sim_mean)/sqrt(n),3);
theory_conf_interval <- theory_mean + c(-1,1)*1.96*sqrt(theory_variance)/sqrt(n);