%% Forecast NASDAQ Composite Index
% Forecast the daily NASDAQ Composite Index over a 500-day horizon.
% Load the NASDAQ data included with the toolbox, and extract the first 1500 observations.

load Data_EquityIdx
nasdaq = DataTable.NASDAQ(1:1500);

%
% Fit an ARIMA(1,1,1) model to the data.

nasdaqModel = arima(1,1,1);
nasdaqFit = estimate(nasdaqModel,nasdaq);

%
% Forecast the Composite Index for 500 days using the fitted model. Use the observed data as presample data.

[Y,YMSE] = forecast(nasdaqFit,500,nasdaq);

%
% Plot the forecasts and 95% forecast intervals.

lower = Y - 1.96*sqrt(YMSE);
upper = Y + 1.96*sqrt(YMSE);

figure
plot(nasdaq,'Color',[.7,.7,.7]);
hold on
h1 = plot(1501:2000,lower,'r:','LineWidth',2);
plot(1501:2000,upper,'r:','LineWidth',2)
h2 = plot(1501:2000,Y,'k','LineWidth',2);
legend([h1 h2],'95% Interval','Forecast',...
	     'Location','NorthWest')
title('NASDAQ Composite Index Forecast')
hold off

% The process is nonstationary, so the widths of the forecast intervals grow with time.
% Copyright 2015 The MathWorks, Inc.