Please be easy on me since quant finance is not my strength.
I have the following Python code that models volatility under GARCH(1,1) for the S&P500:
import numpy as np
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import scipy.optimize as spop
ticker = '^GSPC'
start = '2015-12-31'
end = '2021-06-25'
prices = yf.download(ticker, start, end)['Close']
returns = np.array(prices)[1:]/np.array(prices)[:-1] - 1
mean = np.average(returns)
var = np.std(returns)**2
def garch_mle(params):
mu = params[0]
omega = params[1]
alpha = params[2]
beta = params[3]
long_run = (omega/(1 - alpha - beta))**(1/2)
resid = returns - mu
realised = abs(resid)
conditional = np.zeros(len(returns))
conditional[0] = long_run
for t in range(1,len(returns)):
conditional[t] = (omega + alpha*resid[t-1]**2 + beta*conditional[t-1]**2)**(1/2)
likelihood = 1/((2*np.pi)**(1/2)*conditional)*np.exp(-realised**2/(2*conditional**2))
log_likelihood = np.sum(np.log(likelihood))
return -log_likelihood
res = spop.minimize(garch_mle, [mean, var, 0, 0], method='Nelder-Mead')
params = res.x
mu = res.x[0]
omega = res.x[1]
alpha = res.x[2]
beta = res.x[3]
log_likelihood = -float(res.fun)
long_run = (omega/(1 - alpha - beta))**(1/2)
resid = returns - mu
realised = abs(resid)
conditional = np.zeros(len(returns))
conditional[0] = long_run
for t in range(1,len(returns)):
conditional[t] = (omega + alpha*resid[t-1]**2 + beta*conditional[t-1]**2)**(1/2)
And it produces these values, after optimization:
mu 0.000961
omega 4e-06
alpha 0.2604
beta 0.721
long-run volatility 0.0149
log-likelihood 4691.0025
How do I continue modeling volatility, under GARCH(1,1), using the parameters from above (aka I wouldn't have access to the initial raw data anymore) and for the following new daily closes on the S&P500?
new_data = {
'2021-06-25': 4280.700195,
'2021-06-28': 4290.609863,
'2021-06-29': 4291.799805,
'2021-06-30': 4297.500000,
'2021-07-01': 4319.939941,
'2021-07-02': 4352.339844
}
Thanks!
resid
) and then run the last part of the code that uses the estimated parameters, resid and the former conditional to give you a new GARCH(1,1) volatility estimate. $\endgroup$