Simulation Exercise

Doris Chen
2023-01-27

Overview

This is the Statistical Inference Course Project Part 1. In this project, we will run one thousand simulations to investigate the distribution of averages of 40 exponentials and compare it with the Central Limit Theorem.

Simulations

First, we run a thousand simulations of the averages of 40 exponentials, get a population (exp) of 40,000 data and the sample mean (mns).

exp <- NULL
mns <- NULL

for (i in 1:1000) {
        exp <- c(exp, rexp(40, rate=0.2))
        mns <- c(mns, mean(rexp(40, rate=0.2)))
}

Sample Mean versus Theoretical Mean

  1. Sample Mean:
mean(mns)
## [1] 4.999254
  1. Theoretical mean (population mean):
mean(exp)
## [1] 4.987313

The sample mean and the theoretical mean are very close. Just as the Law of Large Numbers states, the average of the sample mean limits to what its estimating, the population mean. see the plot below.

hist(mns, breaks=10, main="Sample Mean Distribution")
abline(v=mean(mns), lwd=4, col="red")
abline(v=mean(exp), lwd=4, col="yellow")
legend("topright", lty=1, lwd=4, col=c("red", "yellow"), legend=c("Sample Mean", "Theoretical Mean"), bty="n", cex=0.6)

We mark the sample mean and the theoretical mean in the plot, and the two lines overlapped.

Sample Variance versus Theoretical Variance

  1. Sample Variance:
var(mns)
## [1] 0.6402444
  1. Theoretical Variance:
var(exp)/40
## [1] 0.6277953

Theoretically, the variance of the sample mean is the population variance divided by n. As it shows above, the sample variance and the theoretical variance are pretty close.

We plot the two standard deviation lines on the sample mean distribution as following:

hist(mns, breaks=10, main="Sample Mean Distribution")
abline(v=mean(mns), lwd=4, col="red")
abline(v=mean(mns)+sd(mns), lwd=4, col="blue")
abline(v=mean(mns)+sqrt(var(exp)/40), lwd=4, col="yellow")
legend("topright", lty=1, lwd=4, col=c("red", "blue", "yellow"), legend=c("Mean", "Sample SD", "Theoretical SD"), bty="n", cex=0.6, pt.cex = 0.6)

The sample standard deviation line and the theoretical standard deviation line are almost overlapped too.

Distribution

From the “Sample Mean Distribution” histograms above, we can tell that the sample mean distribution looks approximately normal.

Let’s check the summary of the sample mean data.

summary(mns)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   2.747   4.422   4.962   4.999   5.522   7.790

The median value and the mean value are different but very close, which means the distribution is approximately normal.

We make a new histogram of the sample mean with a normal curve over it:

x <- seq(min(mns), max(mns), length=1000)   # X-axis grid
fun <- dnorm(x, mean=mean(mns), sd=sd(mns))  # Normal curve
hist(mns, prob=TRUE, ylim=c(0, max(fun)), main="Sample Mean Distribution with Normal Curve")
lines(x, fun, col="brown", lwd=2)

As we can see from the plot, the brown line looks approximately a bell curve.