The Law of Large Numbers

A verification of the Law of Large Numbers

Matteo Grazzini

The exponential distribution and the Law of Large Numbers

The exponential distribution is a function of the form \(f(x) = \lambda e^{-\lambda x} (\lambda > 0).\)

It is known from theory that the expected value for the exponential distribution is \(1/\lambda\) for any \(\lambda > 0\).

The Law of Large Numbers states that the average of a sufficiently large number of samples from any distribution converges towards the expected value of the distribution.

The Shiny application

The Shiny application designed for this project accepts a value for \(\lambda\) as input and, after verification that \(\lambda\) is numeric and strictly positive, simulates 50000 samples extracted from the exponential distribution.

The samples are stored in a 1000x50 matrix and averaged by row then by column.

The expected value for the distribution and the average of the samples are calculated and compared for verification.

An example for \(\lambda=2\)

Results from the application vs. code:

img1
set.seed(3)
simulation <- matrix(rexp(50000, rate = 2), 1000, 50)
simMean <- apply(simulation, 1 , mean)
mean(simMean)
## [1] 0.4997669

An example for \(\lambda=2\)

The distribution of the average of samples by row is as follows (average in red):

hist(simMean, breaks = 100, main = "Distribution of samples", xlab = "Sample",
     ylab = "Frequency")
abline(v = mean(simMean), col = "red")

plot of chunk unnamed-chunk-2