##Part 1: Simulation Exercises
#Overview :-
Executing simulations and data analysis to illustrate application of the central limit theorem, using R programming.
#Simulations :-
Simulating 1000 random samples of sample size forty random variables belonging to the exponential distribution - With a lambda rate of 0.2.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.2
lambda <- 0.2
thMean <- 1/lambda
n <- 40
sim_01 <- matrix(rexp(1000*n, lambda), nrow = 1000, ncol = 40)
simMean <- apply(sim_01, 1, mean)
meansimMean <- mean(simMean)
print(c( " Average of the means of sim_01 = ", meansimMean))
## [1] " Average of the means of sim_01 = " "4.96385469646634"
print(c("the inverse of lambda =", thMean))
## [1] "the inverse of lambda =" "5"
#Comparing Sample Means vs Theoretical Means
For Exponential Distribution the true mean is equal to the inverse of lambda. So with our rate of 0.2, our true mean should be equivalent to 5.
meansTest <- t.test(simMean, mu = thMean)
print(meansTest)
##
## One Sample t-test
##
## data: simMean
## t = -1.4491, df = 999, p-value = 0.1476
## alternative hypothesis: true mean is not equal to 5
## 95 percent confidence interval:
## 4.914908 5.012802
## sample estimates:
## mean of x
## 4.963855
Mean of x is roughly equivalent to the inverse of lambda.
#Comparing Sample Variance vs Theoretical Variance
The variance is equal to the inverse square of lambda.
thVar <- (1/lambda)^2
simVar <- apply(sim_01,1, var)
meansimVar <- mean(simVar)
print(c( " Average of the Variance of sim_01 = ", meansimVar))
## [1] " Average of the Variance of sim_01 = "
## [2] "24.7839626221555"
print(c("the inverse square of lambda =", thVar))
## [1] "the inverse square of lambda =" "25"
varTest <- t.test(simVar, mu = thVar)
print(varTest)
##
## One Sample t-test
##
## data: simVar
## t = -0.59836, df = 999, p-value = 0.5497
## alternative hypothesis: true mean is not equal to 25
## 95 percent confidence interval:
## 24.07546 25.49247
## sample estimates:
## mean of x
## 24.78396
Mean of x is roughly equivalent to the inverse square of lambda,
#Prove that the distribution is approximately normal.
Graph compares the simulation means distribution with the normal distribution The expected standard deviation is equal to (1/lambda)/sqrt(n)m And the variance is just the standard deviation squared
Esd <- (1/.2)/sqrt(n)
Evar <- Esd^2
fit <- seq(min(simMean), max(simMean), length=100)
std_fit <- dnorm(fit, mean=thMean, sd=Esd)
hist(simMean, breaks = n, prob=T, xlab = "means", ylab = "count", main = "Mean Density")
lines(fit, std_fit, pch=1, col="green", lty=3, lwd = 5)
abline(v = meansimMean, col="purple")