1 Introduction

Obtain monthly data for the closing price of S&P 500 index, YAHOO/INDEX_GSPC/CLOSE, closing price of IBM stock, NYSE_IBM/CLOSE, and for the annualized 3 month Treasury bill rate, FRED/TB3MS.

  1. Construct the time series for monthly returns for S&P 500, IBM stock,and the 3-month Treasury bill.

  2. Estimate a simple OLS relating excess returns for IBM and excess returns for S&P 500 to obtain the coefficient ?? and \(\beta\) of \(\alpha\) security characteristic line.

  3. Estimate a CAPM model with time-varying coefficients with time varying coefficient ??t and ??t using Kalman filter. Create a plot showing how the smoothed states ??t and ??t changed over time.

  4. Compare \(\alpha\) and \(\beta\) from (b) with ??t and ??t from (c).

2 Import Data

library(Quandl)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library("vars")
## Warning: package 'vars' was built under R version 3.3.3
## Loading required package: MASS
## Loading required package: strucchange
## Warning: package 'strucchange' was built under R version 3.3.3
## Loading required package: sandwich
## Warning: package 'sandwich' was built under R version 3.3.3
## Loading required package: urca
## Loading required package: lmtest
## Warning: package 'lmtest' was built under R version 3.3.3
library("urca")
library("gdata")
## Warning: package 'gdata' was built under R version 3.3.3
## gdata: Unable to locate valid perl interpreter
## gdata: 
## gdata: read.xls() will be unable to read Excel XLS and XLSX files
## gdata: unless the 'perl=' argument is used to specify the location
## gdata: of a valid perl intrpreter.
## gdata: 
## gdata: (To avoid display of this message in the future, please
## gdata: ensure perl is installed and available on the executable
## gdata: search path.)
## gdata: Unable to load perl libaries needed by read.xls()
## gdata: to support 'XLX' (Excel 97-2004) files.
## 
## gdata: Unable to load perl libaries needed by read.xls()
## gdata: to support 'XLSX' (Excel 2007+) files.
## 
## gdata: Run the function 'installXLSXsupport()'
## gdata: to automatically download and install the perl
## gdata: libaries needed to support Excel XLS and XLSX formats.
## 
## Attaching package: 'gdata'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following object is masked from 'package:stats':
## 
##     nobs
## The following object is masked from 'package:utils':
## 
##     object.size
## The following object is masked from 'package:base':
## 
##     startsWith
library("stargazer")
## 
## Please cite as:
##  Hlavac, Marek (2015). stargazer: Well-Formatted Regression and Summary Statistics Tables.
##  R package version 5.2. http://CRAN.R-project.org/package=stargazer
library("KFAS")
## Warning: package 'KFAS' was built under R version 3.3.3

(a) Construct the time series

SP500 <- Quandl("YAHOO/INDEX_GSPC/CLOSE", collapse = "monthly", type = 'zoo')
IBM <- Quandl("GOOG/NYSE_IBM/CLOSE", collapse = "monthly", type = 'zoo')
Tbill <- Quandl("FRED/TB3MS", type = 'zoo')
rsp500 <- 100*diff(log(SP500), differences = 1)
rIBM <- 100*diff(log(IBM), differences = 1)
rTbill <- Tbill / 12
par(mfrow=c(3,2))
plot(log(SP500), main=expression("log_SP500"))
plot(rsp500, main=expression("100*differentiated log_SP500"))

plot(IBM, main=expression("log_IBM"))

plot(rIBM, main=expression("100*differentiated log_IBM"))

plot(Tbill, main=expression("Tbill"))

plot(rTbill, main=expression("100*differentiated log_Tbill"))

rsp5002 <- window(rsp500, start = 1981 + 3/12, end = 2017 + 3/12)
rIBM2 <- window(rIBM, start = 1981 + 3/12, end = 2017 + 3/12)
rTbill2 <- window(rTbill, start = 1981 + 3/12, end = 2017 + 3/12)

(b) Estimate a simple OLS relating excess returns for IBM and excess returns for S&P 500

excessIBM <- rIBM2 - rTbill2
excessSP500 <- rsp5002 - rTbill2

sOLS <- lm(excessIBM  ~ excessSP500)
plot(excessIBM, excessSP500, main="Simple OLS")
abline(lm(excessSP500~excessIBM))

summary(sOLS)
## 
## Call:
## lm(formula = excessIBM ~ excessSP500)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -31.2343  -3.1235  -0.1644   3.4677  24.5022 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -0.09957    0.29559  -0.337    0.736    
## excessSP500  0.92509    0.06804  13.597   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.134 on 431 degrees of freedom
## Multiple R-squared:  0.3002, Adjusted R-squared:  0.2986 
## F-statistic: 184.9 on 1 and 431 DF,  p-value: < 2.2e-16

(c) Estimate a CAPM model with time-varying coefficients with time varying coefficient \(\alpha_{t}\) and \(\beta_{t}\) using Kalman filter. Create a plot showing how the smoothed states \(\alpha_{t}\) and \(\beta_{t}\) changed over time.

excesssp500ts <- as.ts(excessSP500)
excessIBMts <- as.ts(excessIBM)

T <- length(excesssp500ts)
Zt <- array(rbind(rep(1,T), excesssp500ts), dim=c(1,2,T))
Ht <- matrix(NA)
Tt <- diag(2)
Rt <- diag(2)
Qt <- matrix(c(NA,0,0,NA), 2,2)

 
a1 <- matrix(c(0, 1), 2, 1)
P1 <- matrix(0, 2, 2)
P1inf <- diag(2)
 
y.SS <- SSModel(excessIBMts ~ -1 + SSMcustom(Z=Zt, T=Tt, R=Rt, Q=Qt, a1=a1, P1=P1, P1inf=P1inf), H=Ht)
y.SS.ML <- fitSSM(y.SS, inits = c(0.001,0.001,0.001), method="BFGS")
y.SS.KFS <- KFS(y.SS.ML$model)

alpha.KFS <- ts( cbind(y.SS.KFS$a[,1], y.SS.KFS$alphahat[,1]), start=c(1981,4), frequency=12)
beta.KFS <- ts( cbind(y.SS.KFS$a[,2], y.SS.KFS$alphahat[,2]), start=c(1981,4), frequency=12)
par(mfrow=c(2,1), mar=c(4,4,2,1))
 
plot.ts(alpha.KFS, plot.type="single", xlab="",ylab="Alpha", col=c("blue","red"), lwd=2)
legend("topright", c("filtered","smoothed"), col=c("blue","red"), lwd=2, cex=0.7, bty="n")
plot.ts(beta.KFS, plot.type="single", xlab="",ylab="Beta", col=c("blue","red"), lwd=2)
legend("topright", c("filtered","smoothed"), col=c("blue","red"), lwd=2, cex=0.7, bty="n")

par(mfrow=c(2,1), mar=c(4,4,2,1))
 
plot.ts(alpha.KFS[,2], plot.type="single", xlab="",ylab="Alpha", col="red", lwd=2)
legend("topright", "smoothed", col="red", lwd=2, cex=0.7, bty="n")
plot.ts(beta.KFS[,2], plot.type="single", xlab="",ylab="Beta", col="red", lwd=2)
legend("topright", "smoothed", col="red", lwd=2, cex=0.7, bty="n")

(d) Compare \(\alpha\) and \(\beta\)

mean(sOLS$coefficients[1])
## [1] -0.09956639
mean(sOLS$coefficients[2])
## [1] 0.9250887
mean(y.SS.KFS$a[, 1])
## [1] -0.06942577
mean(y.SS.KFS$a[, 2])
## [1] 0.9198105
mean(y.SS.KFS$alphahat[, 1])
## [1] -0.07471126
mean(y.SS.KFS$alphahat[, 2])
## [1] 0.9228255