%% Forecast the Conditional Variance of NASDAQ Returns
% Forecast the conditional variance of the NASDAQ Composite Index returns over a 500-day horizon.
% Load the NASDAQ data included with the toolbox. Convert the index to returns.

load Data_EquityIdx
nasdaq = DataTable.NASDAQ;
r = price2ret(nasdaq);
T = length(r);

%
% Fit a GJR(1,1) model to the data.

Mdl = gjr(1,1);
EstMdl = estimate(Mdl,r);

%
% Forecast the conditional variance for 500 days using the fitted model. Use the observed returns as presample innovations for the forecast.

vF = forecast(EstMdl,500,r);

%
% Plot the forecasts, along with the conditional variances inferred from the data.

v = infer(EstMdl,r);

figure
plot(v,'Color',[.7,.7,.7])
hold on
plot(T+1:T+500,vF,'r','LineWidth',2)
xlim([1,T+500])
legend('Inferred','Forecast','Location','Northwest')
title('Conditional Variance Forecasts for NASDAQ Returns')
hold off

%
% Plot conditional variance forecasts for 1000 days.

vF1000 = forecast(EstMdl,1000,r);

figure
plot(vF1000,'r','LineWidth',2)
title('Conditional Variance Forecast Asymptote')

% The forecasts converge asymptotically to the unconditional variance of the GJR process.
% Copyright 2015 The MathWorks, Inc.