#Foundation and Application of Econometrics, Spring 2015, Pepared by Dr. I-Ming Chiu
#Accompany 368_Handout04.pdf
#The First Theroem (pp. 2) to State the Distribution of Sample Average
rm(list=ls())
x = rep(0,500)
set.seed(12345)
for (i in 1:500){
x[i] = mean(rnorm(10, 0, 2)) #X~N(0, 4)
}
mean(x);var(x) #mean(x) should be close to zero and variance should be close to 4/10 (i.e., sigmasquare/n)
hist(x,freq=F,ylim=c(0,0.7))
y = seq(-3,3,0.1)
lines(y, dnorm(y,mean(x),sd(x)),col="red",lty=2,lwd=2)
#Weak Law of Large Numbers (pp. 2) ... Consisteny of an estimator
# (a) Coin example
rm(list=ls())
x = rep(0, 100)
w = rep(0,100)
for (i in 1:100){
x = sample(c(0,1), 100, replace=T)
w[i] = sum(x[1:i])/i
}
plot(w, type="b")
abline(h = 0.5) # Sn/n converges to 0.5, abline: add a straight line to the existing diagram; h means a horizonal line, v means a vertical line, etc (use ?abline to explore more).
#(b) Dice example
rm(list=ls())
x = rep(0, 150)
w = rep(0,150)
for (i in 1:150){
x = sample(1:6, 150, replace=T)
w[i] = length(which(x[1:i]==6))/i
}
plot(w, type="b", cex = 0.6)
abline(h = 1/6) # Sn/n converges to 1/6; throw a dice "many" times, we expect to obsevre "6" (or any other five numbers) 1/6 of the time.
*For the above examples, you can always increase the number of trials, szie n, to observe a better convergence outcome.
#Central Limit Theorem (pp.3)
rm(list=ls())
par(mfrow=c(2,2))
x = 0:10
plot(x,dbinom(x,10,0.2),main="Binomial Distribution",type="h",ylab="density")
xbar_3=rep(0,500)
for (i in 1:500) {xbar_3[i]=mean(rbinom(3,10,0.2))}
hist(xbar_3,prob=TRUE,breaks=12)
xbar_10=rep(0,500)
for (i in 1:500) {xbar_10[i]=mean(rbinom(10,10,0.2))}
hist(xbar_10,prob=TRUE,breaks=12)
xbar_30=rep(0,500)
for (i in 1:500) {xbar_30[i]=mean(rbinom(30,10,0.2))}
hist(xbar_30,prob=TRUE,breaks=12)