Execute the following steps to estimate the CCC-GARCH model in Python.
- Import the libraries:
import pandas as pd
import yfinance as yf
from arch import arch_model
- Specify the risky assets and the time horizon:
RISKY_ASSETS = ['GOOG', 'MSFT', 'AAPL']
N = len(RISKY_ASSETS)
START_DATE = '2015-01-01'
END_DATE = '2018-12-31'
- Download data from Yahoo Finance:
df = yf.download(RISKY_ASSETS,
start=START_DATE,
end=END_DATE,
adjusted=True)
- Calculate daily returns:
returns = 100 * df['Adj Close'].pct_change().dropna()
returns.plot(subplots=True,
title=f'Stock returns: {START_DATE} - {END_DATE}');
This results in the plot shown below:
- Define lists for storing objects:
coeffs = []
cond_vol = []
std_resids = []
models = []
- Estimate the univariate GARCH models:
for asset in returns.columns:
model = arch_model(returns[asset], mean='Constant',
vol='GARCH', p=1, o=0,
q=1).fit(update_freq=0, disp='off')
coeffs.append(model.params)
cond_vol.append(model.conditional_volatility)
std_resids.append(model.resid / model.conditional_volatility)
models.append(model)
- Store the results in DataFrames:
coeffs_df = pd.DataFrame(coeffs, index=returns.columns)
cond_vol_df = pd.DataFrame(cond_vol).transpose()
.set_axis(returns.columns,
axis='columns',
inplace=False)
std_resids_df = pd.DataFrame(std_resids).transpose()
.set_axis(returns.columns,
axis='columns',
inplace=False)
The following image shows a table containing the estimated coefficients for each series:
- Calculate the constant conditional correlation matrix (R):
R = std_resids_df.transpose()
.dot(std_resids_df)
.div(len(std_resids_df))
- Calculate the one-step-ahead forecast of the conditional covariance matrix:
diag = []
D = np.zeros((N, N))
for model in models:
diag.append(model.forecast(horizon=1).variance.values[-1][0])
diag = np.sqrt(np.array(diag))
np.fill_diagonal(D, diag)
H = np.matmul(np.matmul(D, R.values), D)
The end result is:
array([[6.98457361, 3.26885359, 3.73865239],
[3.26885359, 6.15816116, 4.47315426],
[3.73865239, 4.47315426, 7.51632679]])
We can compare this matrix to the one obtained using a more complex DCC-GARCH model, which we cover in the next recipe.