%% Specify Initial Parameter Values for Optimization
% Fit an ARIMA(1,1,1) model to the daily close of the NYSE Composite Index. Specify initial parameter values obtained from an analysis of a pilot sample.
%
%% Load Data
% Load the US equity index data set Data_EquityIdx.

load Data_EquityIdx

% The table DataTable includes the time series variable NYSE, which contains daily NYSE composite closing prices from January 1990 through December 1995.
% Convert the table to a timetable.

dt = datetime(dates,'ConvertFrom','datenum','Format','yyyy-MM-dd');
TT = table2timetable(DataTable,'RowTimes',dt);

%
%% Fit Model to Pilot Sample
% Suppose that an ARIMA(1,1,1) model is appropriate to model NYSE composite series during the sample period.
% Create an ARIMA(1,1,1) model template for estimation.

Mdl = arima(1,1,1);

% Mdl is a partially specified arima model object.
% Treat the first two years as a pilot sample for obtaining initial parameter values when fitting the model to the remaining three years of data. Fit the model to the pilot sample.

endPilot = datetime(1991,12,31);
pilottr = timerange(TT.Time(1),endPilot,'days');

EstMdl0 = estimate(Mdl,TT{pilottr,"NYSE"},'Display','off');

% EstMdl0 is a fully specified, estimated arima model object.
%
%% Estimate Model
% Fit an ARIMA(1,1,1) model to the estimation sample.  Specify the estimated parameters from the pilot sample fit as initial values for optimization.

esttr = timerange(endPilot + days(1),TT.Time(end),'days');

c0 = EstMdl0.Constant;
ar0 = EstMdl0.AR;
ma0 = EstMdl0.MA;
var0 = EstMdl0.Variance;

EstMdl = estimate(Mdl,TT{esttr,"NYSE"},'Constant0',c0,'AR0',ar0,...
   'MA0',ma0,'Variance0',var0);

% EstMdl is a fully specified, estimated arima model object.
% Copyright 2015 The MathWorks, Inc.