# Code for doing Exercises #1 and #2 in Chapter 5. graphics.off() ############## # No. 1 ############## # please read data first data1=read.csv(file="c:/zcai/res-teach/econ6219/Bank-of-America.csv",header=T) data2=read.csv(file="c:/zcai/res-teach/econ6219/sp100.csv",header=T) rt=data1[,5] # returns for the individual stock rt=rev(rt) rt=diff(log(rt)) rmt=data2[,5] # returns for the market index rmt=rev(rmt) rmt=diff(log(rmt)) rt=rt[abs(rmt)<=0.1] # delect outliers rmt=rmt[abs(rmt)<=0.1] rmt=rmt[abs(rt)<=0.1] rt=rt[abs(rt)<=0.1] n=length(rt) # sample size fit1=lm(rt~rmt) # fit the market model print(summary(fit1)) beta=fit1$coefficients # obtain the estimated coefficients res_f=fit1$residuals # obtain the residuals std=sqrt(diag(vcov(fit1))) # obtain the standard errors t1=(beta[2]-1)/std[2] # t-test statistics for testing H_0: beta=1 p_value1=2*(1-pt(abs(t1),n-2)) # p-value for H_1: beta\=1 print(c("The p-value for (c) is", p_value1)) sse_f=sum(res_f^2) # SSE for the full model sse_r=sum((rt-rmt)^2) # SSE for the reduced model f1=(sse_r-sse_f)*(n-2)/(sse_f*2) # F-test statistic for testing H_0: alpha=0 & beta=1 p_value2=1-pf(f1,2,n-2) print(c("The p-value for (e) is", p_value2)) ################# # No. 2 ################# L=252 # set the window length beta1=rep(0,2*(n-L+1)) dim(beta1)=c(n-L+1,2) for(i in 1:(n-L+1)){ y=rt[i:(i+L-1)] x=rmt[i:(i+L-1)] fit2=lm(y~x) beta1[i,]=fit2$coefficients } win.graph() par(mfrow=c(2,2),mex=0.4,bg="light blue") plot(rmt,rt,xlab="S&P100",ylab="BoA") ts.plot(beta1[,1],main="Time series plot for alpha") abline(beta[1],0) ts.plot(beta1[,2],main="Time series plot for beta") abline(beta[2],0)