---
title: "Rolling Estimates of CER Model Parameters"
author: "Eric Zivot"
date: "Friday, May 15, 2015"
output: slidy_presentation
---

## Set options, load packages and data

```{r}
options(digits=3, width=70)
library(IntroCompFinR)
library(PerformanceAnalytics)

```

```{r, echo=FALSE}
# get data silently
data(msftDailyPrices, sp500DailyPrices, sbuxDailyPrices)
msftPrices = to.monthly(msftDailyPrices, OHLC=FALSE)
sp500Prices = to.monthly(sp500DailyPrices, OHLC=FALSE)
sbuxPrices = to.monthly(sbuxDailyPrices, OHLC=FALSE)
# set sample to match book chapter
smpl = "1998-01::2012-05"
msftPrices = msftPrices[smpl]
sp500Prices = sp500Prices[smpl]
sbuxPrices = sbuxPrices[smpl]
# calculate returns
msftRetS = Return.calculate(msftPrices, method="simple")
sp500RetS = Return.calculate(sp500Prices, method="simple")
sbuxRetS = Return.calculate(sbuxPrices, method="simple")
msftRetS = msftRetS[-1]
sp500RetS = sp500RetS[-1]
sbuxRetS = sbuxRetS[-1]
msftRetC = log(1 + msftRetS)
sp500RetC = log(1 + sp500RetS)
sbuxRetC = log(1 + sbuxRetS)
# merged data set
cerRetS = merge(msftRetS, sbuxRetS, sp500RetS)
cerRetC = merge(msftRetC, sbuxRetC, sp500RetC)
colnames(cerRetS) = colnames(cerRetC) = c("MSFT", "SBUX", "SP500")
```

## zoo function <code>rollapply()</code>

Use the **zoo** function <code>rollapply()</code> to compute functions of data over rolling windows. 

```{r}
args(zoo:::rollapply.zoo)

```

The most important arguments are:

* <code>data</code>: zoo data
* <code>width</code>:  integer window width
* <code>FUN</code>:  function to be applied over the rolling windows
* <code>by</code>:  integer increment to move windows by
* <code>by.column</code>: logical, if <code>TRUE</code> apply <code>FUN</code> to each column of data; if <code>FALSE</code> apply <code>FUN</code> to all columns of data
* <code>align</code>:  character, what part of the window gets the time stamp

## Compute 24-month rolling means for SBUX

```{r}
roll.muhat = rollapply(cerRetC[,"SBUX"], width=24,
                       FUN=mean, align="right")
class(roll.muhat)
t(roll.muhat[1:5])
t(na.omit(roll.muhat)[1:5])
```

## Plot the rolling means with the return series

```{r}
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

## Compute 24-month rolling standard deviations for SBUX

```{r}
roll.sigmahat = rollapply(cerRetC[,"SBUX"],width=24,
                          FUN=sd, align="right")
t(na.omit(roll.sigmahat)[1:5])
```

```{r}
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 `r coredata(roll.sigmahat[as.yearmon("Jan 2000")]) ` in early 2000, dip to `r coredata(roll.sigmahat[as.yearmon("Jan 2005")]) ` in 2005, and then rise to `r coredata(roll.sigmahat[as.yearmon("Jan 2010")]) ` during the financial crisis. There is pretty clear evidence that the volatility of SBUX returns has changed over time.

## Show rolling means and volatilities on the same graph

```{r}
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"))
```

## 24-Month rolling estimates for MSFT

```{r}
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"))
```

* The rolling means stay pretty close to zero over the whole sample. 
* However, the rolling volatilities drop after the dot-com bust and only pick up a little bit during the financial crisis.

## 24-Month rolling estimates for SP500

```{r}
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"))
```

* The rolling means for the S&P500 index move around quite a bit. They dip negative around the dot-com bust, become slightly positive during the mid 2000s, dip negative again during the financial crisis, and become positive again afterwards.
* The rolling volatilities drop during the mid 2000s and then pick up dramatically during the financial crisis and fall a bit afterwards.

## 24-Month rolling correlations between SP500 and SBUX

First, compute function to compute pairwise correlation between two series:

```{r}
rhohat = function(x) {
  cor(x)[1,2]
}
```

Next, call <code>rollapply()</code> with the user-written function

```{r}
roll.rhohat = rollapply(cerRetC[,c("SP500","SBUX")],
                        width=24,FUN=rhohat, by.column=FALSE,
                        align="right")
t(na.omit(roll.rhohat)[1:5])
```

```{r}
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. 

## Summary of rolling estimation results

* Rolling window estimates indicate that $\mu$, $\sigma$ and $\rho_{ij}$ are typically not constant over time
    + volatilities and correlations tend to move more than means
* Assumption of covariance stationarity over long samples is suspect
