Developing Data Product Project

Demonstration of the Central Limit Theorem

Chan Ching Chuen

2023-10-11

Demonstration of the Central Limit Theorem

The app is a demonstration of the Central Limit Theorem, using the uniform distribution. To recap, let \(X_1, X_2, ... X_n\) denote a random sample of n independent observations from a population with mean \(\mu\) and variance \(\sigma^2\). Then the sample mean \(\bar{X_n}\), has a distribution that converges to normal distribution with mean equals \(\mu\) and variance \(\sigma^2/n\).

In this demonstration we use the uniform distribution, and let user to play with different sample size \(n\), and simulate this a number of times, say 1000, and display the histograms for the simulations of the \(\bar{X_n}\) comparing against the theoretical normal distribution predicted by the Central Limit Theorem.

For an explanation of how the Central Limit Theorem works, see https://en.wikipedia.org/wiki/Central_limit_theorem.

Inputs and R codes

The user is going to input 4 paramters: the sample size \(n\) in the previous slide, and the number of simulations \(N\) that controls how many sample means to simulate. The uniform distribution is defined by 2 parameters \(b\) and \(a\) which define the upper and lower bound respectively.

nsamp <- 30
nsims <- 1000
b <- 1
a <- 0
#simulate sample means
gen_xn <- replicate(nsims, mean(runif(nsamp, min=a, max=b)))
head(gen_xn)
## [1] 0.5188705 0.5272075 0.4674096 0.5707356 0.4122941 0.4298820
#generate the corresponding standard normal curve
theo_mean= (b+a)/2
theo_std= (b-a)/sqrt(12)/sqrt(nsamp)
normx <- seq(min(gen_xn),max(gen_xn),(b-a)/1000)
normy <- dnorm(normx, mean=theo_mean, sd=theo_std)

Slide with Plot

A sample plot using parameters in the previous page is shown below, showing how the simulation approximates a normal distribution:

Future Improvements

For future developments, we can consider