Fitting GARCH Volatility to Index Returns in MATLAB
Summary
The document explains how to estimate and plot conditional volatility for historical equity indexes in MATLAB. The example retrieves index price histories, converts prices to returns, fits a GARCH(1,1) model, and stores the estimated conditional standard deviations alongside the return series for plotting. Its main practical lesson is that the model should be fitted to returns rather than raw index price levels.
The accepted response points to MATLAB's older garchfit function when available and mentions the MFE toolbox as an alternative. It also describes using a zero constant mean, which corresponds to fitting volatility to residuals after a mean model. The answer notes that MATLAB's Econometrics Toolbox has since changed, so the legacy function may not be appropriate in current versions. The example focuses on implementation and visualization; it does not discuss diagnostics, parameter uncertainty, model comparison, or whether GARCH(1,1) is suitable for a particular index.
Key ideas
- Transform index prices into returns before fitting a GARCH volatility model.
- A GARCH fit can provide conditional volatility estimates that can be plotted alongside returns.
- The example uses legacy MATLAB functions, whose availability and interfaces may differ in newer releases.
- A zero-mean specification treats returns as having a constant mean of zero, or models residuals from a mean process.
Tags
Full text
# How do I model GARCH(1,1) volatility for historical indexes in Matlab?
# How do I model GARCH(1,1) volatility for historical indexes in Matlab?
I'm currently working with historical index data from Yahoo Finance and would like to plot the GARCH(1,1) volatility of these indexes. I'm working with the Datafeed and Finance Tollboxes in Matlab right now, and I'm able to get the data and plot the indexes. However I'm having some difficulty understanding the following methodology to get the GARCH sigmas.
```
clf;
clear all;
%close all;
format short;
t = cputime;
Connect = yahoo;
dataFTSE=fetch(Connect,'^FTSE','Jan 1 1990',today, 'd');
dataN225=fetch(Connect,'^N225','Jan 1 1990',today, 'd');
dataGSPC=fetch(Connect,'^GSPC','Jan 1 1990',today, 'd');
close(Connect);
tsFTSE=fints(dataFTSE(:,1),dataFTSE(:,end),'FTSE100','d','FTSE100');
tsN225=fints(dataN225(:,1),dataN225(:,end),'NiKKEI225','d','NiKKEI225');
tsGSPC=fints(dataGSPC(:,1),dataGSPC(:,end),'SP500','d','SP500');
subplot 311;
plot(tsFTSE)
xlabel('Time (date)')
ylabel('Adjusted Close price ($)')
subplot 312;
plot(tsGSPC)
xlabel('Time (date)')
ylabel('Adjusted Close price ($)')
subplot 313;
plot(tsN225)
xlabel('Time (date)')
ylabel('Adjusted Close price ($)')
yt = get(gca,'YTick');
set(gca,'YTickLabel', sprintf('%.0f|',yt))
e = cputime - t
```
From then on I get the indexes in financial objects, where the prices are in cell arrays. What I think needs to happen is to fit the GARCH(1,1) model like so:
```
ugarch(U,1,1)
```
where U is a vector with just the prices of the index? I don't have a lot of experience with Matlab's data structures so any info or references will be greatly appreciated. The reason I don't want to use the R script is to have some uniformity of plots in my thesis.
--EDIT-- I'm appending some more code which I think produces the plot I was after. It should be relatively easy to vectorize the index inputs and produce different plots.
```
dataGSPCret = [0.0 price2ret(dataGSPC(:,end))'];
retGSPC=fints(dataGSPC(:,1),dataGSPCret','retSP500','d',...
'retSP500');
[coeff3, errors3, LLF3, innovations3, sigmas3] = ...
garchfit(dataGSPCret);
sigmaGSPC = fints(dataGSPC(:,1),sigmas3','retSP500',...
'd','retSP500');
```
And then plot the GARCH variance over the daily returns.
```
figure(2);
subplot 311; hold on;
plot(retFTSE); plot(sigmaFTSE); hold off;
subplot 312; hold on;
plot(retN225); plot(sigmaN225); hold off;
subplot 313; hold on;
plot(retGSPC); plot(sigmaGSPC); hold off;
```
## Answer by John (score 2, accepted)
https://quant.stackexchange.com/a/3747
You would want to use garchfit if you have it. If you don't have access to that you could use the MFE toolbox.
Anyway, as for the inputs, it could be a vector with a constant mean of zero. This would be like fitting an AR(p) model to the prices and then estimating the Garch parameters on the residuals.
EDIT: Matlab has updated the Econometrics toolbox in recent years so that one would no longer use the garchfit function. These are instructions on how to convert the older garchfit code into the current code.Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.