Chapter 3. Multivariate Volatility Models (in Python/Julia)


Copyright 2011 - 2019 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/.


Listing 3.1/3.2: Download stock prices in Python
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])
		
Listing 3.1/3.2: Download stock prices in Julia
Last updated June 2018

using CSV;
p = CSV.read("stocks.csv",nullable=false);
y1 = diff(log.(p[:,1])).*100;              # consider first two stocks
y2 = diff(log.(p[:,2])).*100;              # convert prices to returns
y1 = y1-mean(y1);                          # subtract mean
y2 = y2-mean(y2);
y = hcat(y1,y2);                           # combine both series horizontally
T = size(y,1);                             # get the height of timeseries
		

Listing 3.3/3.4: EWMA in Python
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)
		
Listing 3.3/3.4: EWMA in Julia
Last updated June 2018

## create a matrix to hold covariance matrix for each t
EWMA = fill!(Array{Float64}(T,3), NaN)
lambda = 0.94
S = cov(y)                                       # initial (t=1) covar matrix
EWMA[1,:] = [S[1], S[4], S[2]]                   # extract var and covar
for i in range(2,T-1)                            # loop though the sample
    S = lambda*S+(1-lambda)*y[i-1,:]*(y[i-1,:])'
    EWMA[i,:] = [S[1], S[4], S[2]]               # convert matrix to vector
end
EWMArho = EWMA[:,3]./sqrt.(EWMA[:,1].*EWMA[:,2]) # calculate correlations
		

Listing 3.5/3.6: OGARCH in Python
Last updated June 2018

## Python does not have a proper OGARCH package at present
		
Listing 3.5/3.6: OGARCH in Julia
Last updated June 2018

## No OGARCH code available in Julia at present
		

Listing 3.7/3.8: DCC in Python
Last updated June 2018

## Python does not have a proper DCC package at present
		
Listing 3.7/3.8: DCC in Julia
Last updated June 2018

## No DCC code available in Julia at present
		

Listing 3.9/3.10: Correlation comparison in Python
Last updated June 2018

## Python does not have a proper OGARCH/DCC package at present
		
Listing 3.9/3.10: Correlation comparison in Julia
Last updated June 2018

## No OGARCH/DCC code available in Julia at present