Statistical Inference Course Assignment PART 01/02
Bruno Laget Merino
2023-10-10
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("[Link]","ggplot2",'dplyr',"knitr","xtable","reshape2","DT","formattable","gt"),
Basic Variables
Setting variables given in the assignment description: - Set the seed to a fixed number (100) for improved
reproduceability - Set the parameter λ of the exponential distribution to 0.2 - Set the number of samples n to
40 and the number of simulations to 1000
[Link](100)
lambda <- 0.2
n <- 40
sim_n <- 1000
Simulations
The object sim_exp_distribution holds a 1000x40 matrix containing 1000 simulations of 40 exponen-
tials. It replicates 1000 times an exponential defined, in rcode, by the function rexp(n,λ). 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 µn , the analytical mean A is given by
P1000
n=1 µn
1000
1
Meanwhile, the theoretical mean is given by
1 1
= =5
λ 0.2
analytical_mean <- mean(means_exponentials)
theoretical_mean <- 1/lambda
delta = percent(abs(analytical_mean - theoretical_mean) / theoretical_mean)
meanscomparison <- [Link](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 theoret
abline(v = theoretical_mean, col = "#00FFFF",lwd=3)
abline(v = analytical_mean, col = "#330066",lwd=2)
Exponential Function − Mean of 1k simulations vs theoretical mean
250
200
Frequency
150
100
50
0
3 4 5 6 7 8
mean
Sample Variance versus Theoretical Variance:
1 1
Given the theoretical standard deviation λ√ n
and variance ( λ√ n
)2 :
# st. deviation of the distribution
sd_dist <- sd(means_exponentials)
2
# 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 <- [Link](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) / va
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 µ, the Central Limit Theorem guarantees
that, for a sufficiently large number of samples n, there will be a new random variable X ∼ N (µ, √σ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 √σ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, distributi
lines(xfit, yfit, pch=16, lty=6, col="#330066",lwd=4)
qqnorm(means_exponentials, col = "#330066")
qqline(means_exponentials, col = "#FF6699",lwd=3)
Density of means, distribution vs theoretical Normal Q−Q Plot
8
0.5
7
0.4
Sample Quantiles
6
0.3
density
0.2
5
0.1
4
0.0
3 4 5 6 7 8 −3 −2 −1 0 1 2 3
means Theoretical Quantiles