Вы находитесь на странице: 1из 6

Simulation of Exponential Distribution

Christian Ibanez
August 28, 2018
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.

lambda <- 0.2


n <- 40
number_of_simulations <- 1000
set.seed(23)

Simulate a thousand exponential random variable with n = 40

simulated_exp <- replicate(number_of_simulations, rexp(n, lambda))


length(simulated_exp)

## [1] 40000

Get the mean of each 40 simulation

means_of_exp <- apply(simulated_exp, 2, mean)


length(means_of_exp)

## [1] 1000

Theoretical mean versus simulated mean

Theoretical mean/expected mean

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
theoretical_mean <- 1/lambda

Simulated mean

simulated_mean <- mean(means_of_exp)

Absolute difference between the 2

mean_difference <- abs(theoretical_mean - simulated_mean)

As we expected, the difference between the mean is small, but we can further decrease this mean difference if we increase the number of sample
size

mean_difference

## [1] 0.0142497

Theoretical Variance versus Simulated Variance

Theoretical Variance

theoretical_variance <- (1/lambda) ^ 2 /n

Simulated Variance

simulated_variance <- var(means_of_exp)

Absolute difference between the Variances

variance_difference <- abs(theoretical_variance - simulated_variance)

As we expected, the difference between the variance is small, but we can further decrease this variance difference if we increase the number of
sample size

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
variance_difference

## [1] 0.06121985

Theoretical SD vs Simulated SD

Theoretical SD

theoretical_SD <- 1/(lambda * sqrt(n))

Simulated SD

simulated_SD <- sd(means_of_exp)

Absolute difference between the SD

SD_difference <- abs(theoretical_SD - simulated_SD)

As we expected, the difference between the SD is small, but we can further decrease this SD difference if we increase the number of sample size

SD_difference

## [1] 0.03781446

Distribution

library("ggplot2")

plotdata <- data.frame(means_of_exp)


dist <- ggplot(plotdata, aes(x <- means_of_exp))

dist <- dist + geom_histogram(aes(y = ..density..), colour = "dark blue", fill = "grey")

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
dist <- dist +labs(title = "Distribution of means of n = 40 from a thousand simulations", x = "Mean of n = 40", y
= "Density")

dist <- dist + geom_vline(aes(xintercept = simulated_mean, colour = "simulated mean"))

dist <- dist + geom_vline(aes(xintercept = theoretical_mean, colour = "theoretical mean"))

#Show the distribution


dist

## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Confidence Interval

simulated_CI <- simulated_mean + c(-1,1) * 1.96 * simulated_SD / sqrt(n)

theoretical_CI <- theoretical_mean + c(-1, 1) * 1.96 * sqrt(theoretical_variance) / sqrt(theoretical_variance) /


sqrt(n)

Simulated Confidence Interval vs Theoretical Confidence Interval

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
simulated_CI

## [1] 4.757531 5.270969

theoretical_CI

## [1] 4.690097 5.309903

If you test a hypothesis with this confidence interval, the null hypothesis will be rejected

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

Вам также может понравиться