Eric Zivot
Friday, May 15, 2015
options(digits=3, width=70)
library(IntroCompFinR)
## 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(PerformanceAnalytics)
##
## Attaching package: 'PerformanceAnalytics'
##
## The following object is masked from 'package:graphics':
##
## legend
rollapply()
Use the zoo function rollapply()
to compute functions of data over rolling windows.
args(zoo:::rollapply.zoo)
## function (data, width, FUN, ..., by = 1, by.column = TRUE, fill = if (na.pad) NA,
## na.pad = FALSE, partial = FALSE, align = c("center", "left",
## "right"))
## NULL
The most important arguments are:
data
: zoo datawidth
: integer window widthFUN
: function to be applied over the rolling windowsby
: integer increment to move windows byby.column
: logical, if TRUE
apply FUN
to each column of data; if FALSE
apply FUN
to all columns of dataalign
: character, what part of the window gets the time stamproll.muhat = rollapply(cerRetC[,"SBUX"], width=24,
FUN=mean, align="right")
class(roll.muhat)
## [1] "xts" "zoo"
t(roll.muhat[1:5])
## Feb 1998 Mar 1998 Apr 1998 May 1998 Jun 1998
## SBUX NA NA NA NA NA
t(na.omit(roll.muhat)[1:5])
## Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000
## SBUX 0.0233 0.0239 0.0284 0.00951 0.0145
plot.zoo(merge(roll.muhat,cerRetC[,"SBUX"]), plot.type="single",
main="24 month rolling means for SBUX",ylab="returns",
lwd=c(2,2), col=c("blue","orange"))
abline(h=0)
legend(x="bottomright",legend=c("Rolling mean","Monthly returns"),
lwd=c(2,2), col=c("blue","orange"))
Notice how the rolling means become negative during the financial crisis. Here, the rolling means are not constant over the sample which suggests that the SBUX returns may not covariance stationary
roll.sigmahat = rollapply(cerRetC[,"SBUX"],width=24,
FUN=sd, align="right")
t(na.omit(roll.sigmahat)[1:5])
## Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000
## SBUX 0.172 0.172 0.176 0.196 0.197
plot.zoo(merge(roll.sigmahat,cerRetC[,"SBUX"]), plot.type="single",
main="24 month rolling SDs for SBUX", ylab="Returns",
lwd=c(2,2), col=c("blue","orange"))
abline(h=0)
legend(x="bottomright",legend=c("Rolling SD","Monthly returns"),
lwd=c(2,2), col=c("blue","orange"))
Here, the rolling volatility estimates start high, around 0.172 in early 2000, dip to 0.067 in 2005, and then rise to 0.137 during the financial crisis. There is pretty clear evidence that the volatility of SBUX returns has changed over time.
plot.zoo(merge(roll.muhat, roll.sigmahat, cerRetC[,"SBUX"]),
plot.type="single",
main="24 month rolling estimates: SBUX",ylab="returns",
lwd=2, col=c("blue","orange", "black"))
abline(h=0)
legend(x="bottomright",legend=c("Rolling mean","Rolling sd", "Monthly returns"),
lwd=2, col=c("blue","orange","black"))
roll.muhat = rollapply(cerRetC[,"MSFT"], width=24,
FUN=mean, align="right")
roll.sigmahat = rollapply(cerRetC[,"MSFT"], width=24,
FUN=sd, align="right")
plot.zoo(merge(roll.muhat, roll.sigmahat, cerRetC[,"MSFT"]),
plot.type="single",
main="24 month rolling estimates: MSFT",ylab="returns",
lwd=2, col=c("blue","orange", "black"))
abline(h=0)
legend(x="bottomright",legend=c("Rolling mean","Rolling sd", "Monthly returns"),
lwd=2, col=c("blue","orange","black"))
roll.muhat = rollapply(cerRetC[,"SP500"], width=24,
FUN=mean, align="right")
roll.sigmahat = rollapply(cerRetC[,"SP500"],width=24,
FUN=sd, align="right")
plot.zoo(merge(roll.muhat, roll.sigmahat, cerRetC[,"SP500"]),
plot.type="single",
main="24 month rolling estimates: SP500",ylab="returns",
lwd=2, col=c("blue","orange", "black"))
abline(h=0)
legend(x="bottomright",legend=c("Rolling mean","Rolling sd", "Monthly returns"),
lwd=2, col=c("blue","orange","black"))
First, compute function to compute pairwise correlation between two series:
rhohat = function(x) {
cor(x)[1,2]
}
Next, call rollapply()
with the user-written function
roll.rhohat = rollapply(cerRetC[,c("SP500","SBUX")],
width=24,FUN=rhohat, by.column=FALSE,
align="right")
t(na.omit(roll.rhohat)[1:5])
## Jan 2000 Feb 2000 Mar 2000 Apr 2000 May 2000
## x 0.482 0.463 0.495 0.511 0.492
plot.zoo(roll.rhohat, main="Rolling Correlation b/w SP500 and SBUX",
lwd=2, col="blue", ylab="rho.hat")
abline(h=0)
The correlations are not stable over the sample period. They start out near 0.5, drop to 0.3 between 2001 and 2004, become negative briefly in 2005 (why?), move back positive mear 0.5 in 2006, and then increase to 0.8 in 2011.