Overview

This simulation will exemplify the Central Limit Theorem by comparing an exponential distribution and its theoretical values over a high number of simulations. ## Assignment Objectives 1. Show the sample mean and compare it to the theoretical mean of the distribution. 2. Show how variable the sample is (via variance) and compare it to the theoretical variance of the distribution. 3. Show that the distribution is approximately normal.

Simulations

R Environment Setup

invisible(lapply(c("data.table","ggplot2",'dplyr',"knitr","xtable","reshape2","DT","formattable","gt"), library, character.only = TRUE))

Basic Variables

Setting variables given in the assignment description: - Set the seed to a fixed number (100) for improved reproduceability - Set the parameter \(\lambda\) of the exponential distribution to 0.2 - Set the number of samples n to 40 and the number of simulations to 1000

set.seed(100)
lambda <- 0.2
n <- 40
sim_n <- 1000

Simulations

The object sim_exp_distribution holds a 1000x40 matrix containing 1000 simulations of 40 exponentials. It replicates 1000 times an exponential defined, in rcode, by the function rexp(n,\(\lambda\)). The object means_exponentials is a 1x1000 matrix containing the mean of each exponential.

sim_exp_distribution <- replicate(sim_n, rexp(n, lambda))
means_exponentials <- apply(sim_exp_distribution, 2, mean)

Sample Mean vs Theoretical Mean:

The analytical mean is given by the simple mean of all the simulated exponentials’ means, that is, for each exponential mean \(\mu_{n}\), the analytical mean A is given by \[ \frac{\sum_{n=1}^{1000}\mu_{n}}{1000} \] Meanwhile, the theoretical mean is given by \[ \frac{1}{\lambda} = \frac{1}{0.2} = 5 \]

analytical_mean <- mean(means_exponentials)
theoretical_mean <- 1/lambda
delta = percent(abs(analytical_mean - theoretical_mean) / theoretical_mean)
meanscomparison <- data.frame(Analytical_Mean = c(analytical_mean), 
                              Theoretical_Mean = c(theoretical_mean), Difference_pct = c(percent(delta)))
meanscomparison %>% gt() %>%
  tab_header(title = "Center of Distribution Comparison") %>%
  tab_style(style = list(cell_fill(color = "#330066"),cell_text(weight = "bold",color="white")),
    locations = cells_body(columns = Analytical_Mean))%>%
  tab_style(style = list(cell_fill(color = "#00FFFF"),cell_text(weight = "bold")), 
  locations = cells_body(columns = Theoretical_Mean))
Center of Distribution Comparison
Analytical_Mean Theoretical_Mean Difference_pct
4.999702 5 0.01%

The calculated difference amounts to less than 0,01% (thus negligible!), showing how close the analytical mean is to the theoretical mean. This is shown in the plot below, when the Theoretical Mean shown in teal is nearly overlapped by the Analytical Mean in a scale suitable to the whole distribution.

hist(means_exponentials, xlab = "mean", main = "Exponential Function - Mean of 1k simulations vs theoretical mean", col="#FF6699")
abline(v = theoretical_mean, col = "#00FFFF",lwd=3)
abline(v = analytical_mean, col = "#330066",lwd=2)

Sample Variance versus Theoretical Variance:

Given the theoretical standard deviation \(\frac{1}{\lambda\sqrt{n}}\) and variance \((\frac{1}{\lambda\sqrt{n}})^{2}\):

# st. deviation of the distribution
sd_dist <- sd(means_exponentials)
# variance of distribution
variance_dist <- sd_dist^2
# theoretical standard deviation
sd_theoretical <- (1/lambda)/sqrt(n)
# variance from analytical expression
variance_theoretical <- ((1/lambda)*(1/sqrt(n)))^2
variancecomparison <- data.frame(TYPE = c("DISTRIBUTION","THEORETICAL","DIFFERENCE %"),
  STANDARD_DEVIATION = c(sd_dist,sd_theoretical,percent(abs(sd_theoretical - sd_dist) / sd_theoretical)), 
  VARIANCE = c(variance_dist,variance_theoretical,percent(abs(variance_theoretical - variance_dist) / variance_theoretical)))
variancecomparison %>% gt() %>%
  tab_header(title = "Sample vs Theoretical Variance Comparison")
Sample vs Theoretical Variance Comparison
TYPE STANDARD_DEVIATION VARIANCE
DISTRIBUTION 0.80202508 0.64324422
THEORETICAL 0.79056942 0.62500000
DIFFERENCE % 0.01449039 0.02919076

As it has become clear, the true variance of the distribution 0.64 is quite close to the theoretical value 0.625.

Distribution:

To demonstrate how the distribution is approximately normal, the means histogram will be superimposed by the normal curve having the theoretical mean and standard deviation as parameters. The resulting image is a close fit between the two plots, ‘proving’ it’s approximately normal.

What the next two plots will demonstrate is that, given a sample mean \(\mu\), the Central Limit Theorem guarantees that, for a sufficiently large number of samples n, there will be a new random variable \(\overline{X} \sim N(\mu,\frac{\sigma}{\sqrt{n}})\), consisting of the sample means, and that it will be normally distributed with a mean equal to the original distribution, and standard deviation given by \(\frac{\sigma}{\sqrt{n}}\).

xfit <- seq(min(means_exponentials), max(means_exponentials), length=100)
yfit <- dnorm(xfit, mean=1/lambda, sd=(1/lambda/sqrt(n)))
hist(means_exponentials,breaks=10,prob=T,col="#FF6699",xlab = "means",main="Density of means, distribution vs theoretical",ylab="density")
lines(xfit, yfit, pch=16, lty=6, col="#330066",lwd=4)
qqnorm(means_exponentials, col = "#330066")
qqline(means_exponentials, col = "#FF6699",lwd=3)