This document demonstrate Central Limit Theorem using simulation to generate random number from exponential distribution and examine its mean. Comparison of mean and variance between simulated data and theoretical values.
We will be taking average of 40 numbers from exponential
distribution with rate = 0.2. We will be doing a total of
1000 simulations.
Firstly, random number will be generated from exponential
distribution using R function rexp.
set.seed(555)
rate <- 0.2
nsim <-1000
group_size = 40
#generate random number from exp dist
sim_dat <- matrix(
rexp(n = nsim*group_size, rate = 0.2),
nrow = nsim,
ncol = group_size
)
randomly generated number stored in matrix sim_dat
from Central Limit Theorem we expected the sample mean to be normally distributed, centered at true mean \(\mu = \frac {1}{\lambda} ;\lambda(rate) = 0.2\) with variance equal population variance \(Var( \bar {x} ) = \frac {Var(x)}{n}= \frac{1}{n\lambda^2}\).
theo_mean <- 1/rate
theo_var <- 1/rate^2
# https://leanpub.com/LittleInferenceBook/read#leanpub-auto-the-sample-variance
var_samplemeans_theo<- theo_var/group_size
# sample stats
samplemeans <- apply(sim_dat, 1, FUN = mean)
mean_samplemeans <- mean(samplemeans)
var_samplemeans <- var(samplemeans)
dff <- c(
list(theoretical_mean = theo_mean,
simulated_mean = mean_samplemeans),
list(theoretical_variance = var_samplemeans_theo,
simulated_variance = var_samplemeans ))
dff
## $theoretical_mean
## [1] 5
##
## $simulated_mean
## [1] 4.991502
##
## $theoretical_variance
## [1] 0.625
##
## $simulated_variance
## [1] 0.6194613
The simulated mean is pretty close to the theoretical mean. The simulated variance is also close to the theoretical value. Let’s visualize the distribution.
Let’s look at the histogram of the sample means. CLT tells us that
this should be a normal distribution. Let’s overlay a normal curve using
dnorm on the density histogram to highlight that.
# hist
hist(samplemeans,breaks = 40,prob = TRUE)
rug(samplemeans)
abline(v = mean_samplemeans, col = "red", lwd = 3)
# overlay normal curve
# code from: https://r-charts.com/distribution/histogram-curves/
x2 <- seq(min(samplemeans), max(samplemeans), length = 40)
fun <- dnorm(x2, mean = theo_mean, sd = sqrt(var_samplemeans_theo))
lines(x2, fun, col = 2, lwd = 2)
The vertical red line shows theoretical mean of the distribution. The normal curve fit the histogram nicely, CLT works!