%% Forecast a Process Using Simulations
% Forecast the daily NASDAQ Composite Index using Monte Carlo simulations.
% Load the NASDAQ data included with the toolbox. Extract the first 1500 observations for fitting.

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

%
% Specify, and then fit an ARIMA(1,1,1) model.

NasdaqModel = arima(1,1,1);
NasdaqFit = estimate(NasdaqModel,nasdaq);

%
% Simulate 1000 paths with 500 observations each. Use the observed data as presample data.

rng default;
Y = simulate(NasdaqFit,500,'NumPaths',1000,'Y0',nasdaq);

%
% Plot the simulation mean forecast and approximate 95% forecast intervals.

lower = prctile(Y,2.5,2);
upper = prctile(Y,97.5,2);
mn = mean(Y,2);

figure
plot(nasdaq,'Color',[.7,.7,.7])
hold on
h1 = plot(n+1:n+500,lower,'r:','LineWidth',2);
plot(n+1:n+500,upper,'r:','LineWidth',2)
h2 = plot(n+1:n+500,mn,'k','LineWidth',2);

legend([h1 h2],'95% Interval','Simulation Mean',...
			'Location','NorthWest')
title('NASDAQ Composite Index Forecast')
hold off

% Copyright 2015 The MathWorks, Inc.