This study simulates exponential distribution and compare the mean of simulated samples’ means and variance to theoretical ones given by Central Limit Theorem.
The exponential distribution is simulated for 1000 times given the following information:
The 1,000 sample means and sample variances are saved seperately in two variables for future use.
# Setup the means and vars
sample_means = NULL
sample_vars = NULL
# information needed for exp distribution: sample size and lambda
sample_size <- 40
lambda <- 0.2
# number of simulation
number = 1000
# set seed
seed <- 1
set.seed(seed)
# simulation - mean
for (i in 1:number) {
sample_means = c(sample_means, mean(rexp(sample_size, lambda)))
}
# simulation - variance
set.seed(seed)
for (i in 1:number) {
sample_vars = c(sample_vars, var(rexp(sample_size, lambda)))
}
The mean of sample means and the theoretical mean are shown as the following:
sample_mean <- mean(sample_means)
sample_mean
## [1] 4.990025
theo_mean <- 1/lambda
theo_mean
## [1] 5
The plot of the distribution and the comparison is as the following:
# mean comparison
# histogram
hist(sample_means,
main = "Distribution of sample means",
xlab = "Sample means",
xlim = c(-1,11),
sub = "Comparison between the theoretical mean and mean of sample means")
# add lines to show sample mean and theoretical mean
sample_mean <- mean(sample_means)
abline(v = theo_mean, col = "blue", lty = 1)
abline(v = sample_mean, col = "red", lty = 2)
# explanation
text(x = 5, y = 150,
labels = paste("Mean of sample mean (MSM): ", round(sample_mean, 2),
"\nTheoretical mean (TM): ", 1/lambda),
col = "burlywood4",
lwd = 2)
# legend
legend("topright",
legend = c("MSM", "TM"),
lty = c(2, 1),
col = c("red", "blue"),
lwd = 2,
text.width = 2)
The mean of sample variance and the theoretical variance are shown as the following:
sample_var <- mean(sample_vars)
sample_var
## [1] 25.06459
theo_variance <- (1/lambda)^2
theo_variance
## [1] 25
The plot of the distribution and the comparison is as the following:
# variance comparison
hist(sample_vars,
main = "Variances of samples",
xlab = "Sample variance",
sub = "Comparison between the theoretical variance and mean of sample variance")
# add lines of mean to show the comparison
abline(v = sample_var, col = "red", lty = 2)
abline(v = theo_variance, col = "blue", lty = 1)
# explanation
text(50, y = 200,
labels = paste("Theoretical variance (TV):",
theo_variance,
"\nMean of sample variances (MSV):",
round(sample_var, 2)),
col = "burlywood4")
# legend
legend("topright",
legend = c("MSV", "TV"),
lty = c(2, 1),
col = c("red", "blue"),
lwd = 2,
text.width = 8)
As is shown in the following plots, the simulated distribution is appromixately normal.
# mean distribution
mean_distribution <- hist(sample_means,
main = "Distribution of sample means",
xlab = "Sample means",
xlim = c(-1,11),
sub = "Comparison between the theoretical mean and mean of sample means")
# add normal distribution curve
xfit<-seq(min(sample_means),max(sample_means),length=40)
yfit<-dnorm(xfit,mean=mean(sample_means),sd=sd(sample_means))
yfit <- yfit*diff(mean_distribution$mids[1:2])*length(sample_means)
lines(xfit, yfit, col="blue", lwd=2)
# variance distribution
var_distribution <- hist(sample_vars,
main = "Variances of samples",
xlab = "Sample variance",
sub = "Comparison between the theoretical variance and mean of sample variance")
# add normal distribution curve
xfit<-seq(min(sample_vars),max(sample_vars),length=40)
yfit<-dnorm(xfit,mean=mean(sample_vars),sd=sd(sample_vars))
yfit <- yfit*diff(var_distribution$mids[1:2])*length(sample_vars)
lines(xfit, yfit, col="blue", lwd=2)