Copyright 2011, 2016, 2018 Jon Danielsson. This code is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. The GNU General Public License is available at: https://www.gnu.org/licenses/.

The original 2011 R code will not fully work on a recent R because there have been some changes to libraries. The latest version of the Matlab code only uses functions from Matlab toolboxes.

The GARCH functionality in the econometric toolbox in Matlab is trying to be too clever, but can't deliver and could well be buggy. If you want to try that, here are the docs (estimate). Besides, it can only do univariate GARCH and so can't be used in Chapter 3. Kevin Sheppard's MFE toolbox is much better, while not as user friendly, it is much better written and is certainly more comprehensive. It can be downloaded here and the documentation here is quite detailed.

Last updated 2011

```
library(tseries)
library(zoo)
p = zoo(read.csv('stocks.csv',header=TRUE,sep=','))
y = diff(log(p))*100 # calculate returns
y = y[,1:2] # consider first two stocks
y[,1] = y[,1]-mean(y[,1]) # subtract mean
y[,2] = y[,2]-mean(y[,2])
TT = length(y[,1])
```

Last updated June 2018

```
import numpy as np
p = np.loadtxt('stocks.csv',delimiter=',',skiprows=1)
p = p[:,[0,1]] # consider first two stocks
y = np.diff(np.log(p), n=1, axis=0)*100 # calculate returns
y[:,0] = y[:,0]-np.mean(y[:,0]) # subtract mean
y[:,1] = y[:,1]-np.mean(y[:,1])
T = len(y[:,0])
```

Last updated August 2016

```
## create a matrix to hold covariance matrix for each t
EWMA = matrix(nrow=TT,ncol=3)
lambda = 0.94
S = cov(y) # initial (t=1) covar matrix
EWMA[1,] = c(S)[c(1,4,2)] # extract var and covar
for (i in 2:TT){ # loop though sample
S = lambda*S+(1-lambda)*t(y[i-1]) %*% y[i-1]
EWMA[i,] = c(S)[c(1,4,2)] # convert matrix to vector
}
EWMArho = EWMA[,3]/sqrt(EWMA[,1]*EWMA[,2]) # calculate correlations
print(head(EWMArho))
print(tail(EWMArho))
```

Last updated June 2018

```
EWMA = np.full([T,3], np.nan)
lmbda = 0.94
S = np.cov(y, rowvar = False)
EWMA[0,] = S.flatten()[[0,3,1]]
for i in range(1,T):
S = lmbda * S + (1-lmbda) * np.transpose(np.asmatrix(y[i-1]))* np.asmatrix(y[i-1])
EWMA[i,] = [S[0,0], S[1,1], S[0,1]]
EWMArho = np.divide(EWMA[:,2], np.sqrt(np.multiply(EWMA[:,0],EWMA[:,1])))
print(EWMArho)
```

Last updated 2011

```
library(gogarch)
res = gogarch(y,formula = ~garch(1,1),garchlist = c(include.mean=FALSE))
OOrho = ccor(res)
## OOrho is a vector of correlations
```

Last updated June 2018

```
## Python does not have a proper OGARCH package at present
```

Last updated 2011

```
library(ccgarch)
## estimate univariate GARCH models to get starting values
f1 = garchFit(~ garch(1,1), data=y[,1],include.mean=FALSE)
f1 = f1@fit$coef
f2 = garchFit(~ garch(1,1), data=y[,2],include.mean=FALSE)
f2 = f2@fit$coef
## create vectors and matrices of starting values
a = c(f1[1], f2[1])
A = diag(c(f1[2],f2[2]))
B = diag(c(f1[3], f2[3]))
dccpara = c(0.2,0.6)
## estimate the model
dccresults=dcc.estimation(inia=a,iniA=A,iniB=B,ini.dcc=dccpara,dvar=y,model="diagonal")
## Parameter estimates and their robust standard errors in dcc.results$out
DCCrho = dccresults$DCC[,2]
```

Last updated June 2018

```
## Python does not have a proper DCC package at present
```

Last updated 2011

```
matplot(cbind(EWMArho,DCCrho,OOrho),type='l',las=1,lty=1:3,col=1:3,ylab="")
mtext("Correlations",side=2,line=0.3,at=1,las=1,cex=0.8)
legend(2100,0,c("EWMA","DCC","OO"),lty=1:3,col=1:3,bty="n",cex=0.7)
```

Last updated June 2018

```
## Python does not have a proper OGARCH/DCC package at present
```