INTRODUCING 5 - days-a-week problem solving session for Math Olympiad and ISI Entrance. Learn More 

April 28, 2020

Central Limit Theorem by Simulation ( R Studio)

This post verifies central limit theorem with the help of simulation in R for distributions of Bernoulli, uniform and poisson.

Central Limit Theorem

Mathematicaly, in \(X_1, X_2, …, X_n\) are random samples taken from a popualaton with mean \(\mu\) and finte variance \(\sigma^2\) and \(\bar{X}\) is the sampe mean, then \(Z = \frac{\sqrt{n}(\bar{X}-\mu)}{\sigma} \to N(0,1) \).

Simulation

Pseudocode

N # Number of trials (population size)
n # Number of simulations
standardized_sample_mean = rep(0,n)
EX #Expectation
VarX #Variance
  for (i in 1:n){
    samp #Sample from any distribution
    sample_mean <- mean(samp) # Sample mean
    standardized_sample_mean[i] <- sqrt(N)*(sample_mean - EX)/sqrt(VarX)
#Standardized Sample Mean
  }
hist(standardized_sample_mean,prob=TRUE) #Histogram
qqnorm(standardized_sample_mean) #QQPlot

Bernoulli \(\frac{1}{2}\)

N <- 2000 # Number of trials (population size)
n <- 1000 # Number of simulations
standardized_sample_mean = rep(0,n)
EX <- 0.5
VarX <- 0.25
  for (i in 1:n){
    samp <- rbinom(1, size = N, prob = 0.05)
    sample_mean <- mean(samp) # sample mean
    standardized_sample_mean[i] <- sqrt(N)*(sample_mean - EX)/sqrt(VarX)
  }
par(mfrow=c(1,2))
hist(standardized_sample_mean,prob=TRUE)
qqnorm(standardized_sample_mean)

Uniform \((0,1)\)

N <- 2000 # Number of trials (population size)
n <- 1000 # Number of simulations
standardized_sample_mean = rep(0,n)
EX <- 0.5
VarX <- 0.25
  for (i in 1:n
){
    samp <- runif( N, 0, 1)
    sample_mean <- mean(samp) # sample mean
    standardized_sample_mean[i] <- sqrt(N)*(sample_mean - EX)/sqrt(VarX)
  }
par(mfrow=c(1,2))
hist(standardized_sample_mean,prob=TRUE)
qqnorm(standardized_sample_mean)

Poission(1)

N <- 2000 # Number of trials (population size)
n <- 1000 # Number of simulations
standardized_sample_mean = rep(0,n)
EX <- 1
VarX <- 1
  for (i in 1:n){
    samp <- rpois(N,1)
    sample_mean <- mean(samp) # sample mean
    standardized_sample_mean[i] <- sqrt(N)*(sample_mean - EX)/sqrt(VarX)
  }
par(mfrow=c(1,2))
hist(standardized_sample_mean,prob=TRUE)
qqnorm(standardized_sample_mean)
Central Limit Theorem by simulation graph

Exercise

Try for other distributions and mixtures and play around and verify yourself.

Stay Tuned! Stay Blessed!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Cheenta. Passion for Mathematics

Advanced Mathematical Science. Taught by olympians, researchers and true masters of the subject.
JOIN TRIAL
support@cheenta.com