Chapter 3. Multivariate Volatility Models


Copyright 2016 Jon Danielsson. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
The R code from 2011 runs unmodified, this just updates the end date. The Matlab code would not run, so these are new functions. The GARCH functionality in the econometric toolbox in Matlab can only do univatiate GARCH. Kevin Sheppard's MFE toolbox is well written and is certainly comprehensive. Its whats used below. It can be downloaded here and the documentation here is quite comprehensive.
Listing 3.1: Download stock prices in R
Last edited: August 2016

library("tseries")      
library(zoo)
p1 = get.hist.quote(instrument = "msft",start = "2000-01-01",end = "2016-08-30",quote="AdjClose") 
p2 = get.hist.quote(instrument = "ibm",  start = "2000-01-01",end = "2016-08-30",quote="AdjClose") 
p = cbind(p1,p2)            
y = diff(log(p))*100        
y[,1] = y[,1]-mean(y[,1])    
y[,2] = y[,2]-mean(y[,2])
TT = length(y[,1])
		
Listing 3.2: Download stock prices in Matlab
Last edited: August 2016

stocks =hist_stock_data('01012000','30082016','msft','ibm')
p1 = stocks(1).AdjClose(end:-1:1);  
p2 = stocks(2).AdjClose(end:-1:1);
p = [p1 p2];          
y = diff(log(p))*100;  
y(:,1)=y(:,1)-mean(y(:,1));    
y(:,2)=y(:,2)-mean(y(:,2));
T = length(y);
		

Listing 3.3: EWMA in R
Last edited: AUgut 2016

EWMA = matrix(nrow=TT,ncol=3)  
lambda = 0.94
S = cov(y)            
EWMA[1,] = c(S)[c(1,4,2)]  
for (i in 2:TT){    
  S = lambda * S  + (1-lambda) * t(y[i-1]) %*% y[i-1]  
  EWMA[i,] = c(S)[c(1,4,2)]  
}
EWMArho = EWMA[,3]/sqrt(EWMA[,1]*EWMA[,2])  
		
Listing 3.4: EWMA in Matlab
Last edited: 2011

EWMA = nan(T,3);  
lambda = 0.94
S = cov(y)  
EWMA(1,:) = S([1,4,2]);  
for i = 2:T  
  S = lambda * S  + (1-lambda) * y(i,:)' * y(i,:)  
  EWMA(i,:) = S([1,4,2]);  
end
EWMArho = EWMA(:,3) ./ sqrt(EWMA(:,1) .* EWMA(:,2))  
		

Listing 3.5: OGARCH in R
Last edited: 2011

library(gogarch)
res = gogarch(y,formula = ~garch(1,1),garchlist = c(include.mean=FALSE))
OOrho = ccor(res)    
		
Listing 3.6: OGARCH in Matlab
Last edited: August 2016

[par, Ht] = o_mvgarch(y,2, 1,1,1);
Ht = reshape(Ht,4,T)';  
OOrho = Ht(:,3) ./ sqrt(Ht(:,1) .* Ht(:,4));  
		

Listing 3.7: DCC in R
Last edited: 2011

library(ccgarch)
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
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) 
dccresults = dcc.estimation(inia=a, iniA=A, iniB=B, ini.dcc=dccpara,dvar=y, model="diagonal")
DCCrho = dccresults$DCC[,2]
		
Listing 3.8: DCC in Matlab
Last edited: August 2016

[p, lik, Ht] = dcc(y,1,1,1,1)
Ht = reshape(Ht,4,T)';
DCCrho = Ht(:,3) ./ sqrt(Ht(:,1) .* Ht(:,4));  
		

Listing 3.9: Correlation comparison in R
Last edited: 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)
		
Listing 3.10: Correlation comparison in Matlab
Last edited: 2011

plot([EWMArho,OOrho,DCCrho])
legend('EWMA','DCC','OOrho','Location','SouthWest')