Generate three random samples each of which has 200 observations from U(1,3).

1a)

x1<-runif(200,min=1,max=3)
x2<-runif(200,min=1,max=3)
x3<-runif(200,min=1,max=3)
#this script will produce random samples of a uniform distribution

hist(x1, prob=T, main="Sum of Uniform Distributions (U[1,3] w/ 200 observation) with Normal density superimposed")
sdx<-sd(x1); sdx
## [1] 0.5621672
mx<-mean(x1); mx
## [1] 2.022715
#Superimpose the normal distribution on top of the random sample of the uniform distribution
curve(dnorm(x, mean=mx, sd=sdx),add=T, col="red", lty=2)

The normal distribution is not a good fit for the histogram. The center of the normal distribution overestimates the probability of the outcomes, and the edges of the normal distribution underestimates the probability of the uniform distribution.

1b) Let’s perform the same comparison but with a sum of uniform distributions.

X <- x1+x2+x3
hist(X, prob=T, main="Sum of 3 Uniform Distributions (U[1,3] w/ 200 observation) with Normal density superimposed") 
sdX<-sd(X); sdx
## [1] 0.5621672
mX<-mean(X); mx
## [1] 2.022715
curve(dnorm(x, mean=mX, sd=sdX),add=T, col="red", lty=2)

This is very interesting and worthy of note, The sum of three uniform distributions is a much better fit for the normal distribution!

1c) Let’s do the same comparison, but this time with the sum of 6 samples and 10 samples.

Sample6<- matrix(nrow=6, ncol=200) #create an empty vector
for(i in 1:6){Sample6[i,]<-runif(200,min=1,max=3)} #save the uniform distribution into each row
SumSample6<-rep(0,200) #create an empty vector
SumSample6<-colSums(Sample6)
hist(SumSample6, prob=T, main="Sum of 6 Uniform Distributions (U[1,3] w/ 200 observation) with Normal density superimposed") 
sdX<-sd(SumSample6); sdX
## [1] 1.321886
mX<-mean(SumSample6); mX
## [1] 12.05627
curve(dnorm(x, mean=mX, sd=sdX),add=T, col="red", lty=2)

The normal distribution is becoming a better fit as we sum more distributions

Sample12<- matrix(nrow=12, ncol=200) #create an empty vector
for(i in 1:12){Sample12[i,]<-runif(200,min=1,max=3)} #save the uniform distribution into each row
SumSample12<-rep(0,200) #create an empty vector
SumSample12<-colSums(Sample12)
hist(SumSample12, prob=T, main="Sum of 12 Uniform Distributions (U[1,3] w/ 200 observation) with Normal density superimposed") 
sdX<-sd(SumSample12); sdX
## [1] 2.022592
mX<-mean(SumSample12); mX
## [1] 23.77174
curve(dnorm(x, mean=mX, sd=sdX),add=T, col="red", lty=2)

The last simulation is the best fit we have seen so far. This is an example of the central limit theorem.