Author:Biman Rakshit Date:09/05/2020
Peer Graded Assignment: Statistical Inference Course Project
set.seed(415)
Instructions:
Simulation Exercise Instructions
Overview:
In this project you will investigate the exponential distribution in R and compare it with the Central Limit Theorem. The exponential distribution can be simulated in R with rexp(n, lambda) where lambda is the rate parameter. The mean of exponential distribution is 1/lambda and the standard deviation is also 1/lambda. Set lambda = 0.2 for all of the simulations. You will investigate the distribution of averages of 40 exponentials. Note that you will need to do a thousand simulations.
Question 1 : Show the sample mean and compare it to the theoretical mean distribution.
n <- 40
Simulations <- 1000
Lambda <- 0.2
SampleMean <- NULL
for(i in 1:Simulations) {
SampleMean <- c(SampleMean, mean(rexp(n, Lambda)))
}
mean(SampleMean)
[1] 5.022396
So, as we can see, compared to the theoretical mean distribution of 5 , our mean 5 is close.
Question 2: Show the sample is (via variance) and compare it to the thoretical variance of the distribtution.
The theoretical standard deviation of the distribution is also 1/lambda , which, for a lambda of 0.2 , equates to 5 . The variance is the square of the standard deviation, which is 25 .
Variance <- var(SampleMean)
Variance
[1] 0.6332544
0.6 is close to the theoretical distribution.
Show that the distribution is appoximately normal
hist(SampleMean, breaks = n, prob = T, col = "blue", xlab = "Means")
x <- seq(min(SampleMean), max(SampleMean), length = 100)
lines(x, dnorm(x, mean = 1/Lambda, sd = (1/Lambda/sqrt(n))), pch = 25, col = "green")

qqnorm(SampleMean)
qqline(SampleMean, col = "blue")

The distribution averages of 40 exponentials is very close to a normal distribution
LS0tDQp0aXRsZTogIlN0YXRpc3RpY2FsIEluZmVyZW5jZSBDb3Vyc2UgUHJvamVjdCINCm91dHB1dDoNCiAgaHRtbF9ub3RlYm9vazogZGVmYXVsdA0KICBwZGZfZG9jdW1lbnQ6IGRlZmF1bHQNCi0tLQ0KQXV0aG9yOkJpbWFuIFJha3NoaXQNCkRhdGU6MDkvMDUvMjAyMA0KDQpQZWVyIEdyYWRlZCBBc3NpZ25tZW50OiBTdGF0aXN0aWNhbCBJbmZlcmVuY2UgQ291cnNlIFByb2plY3QNCg0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