%% Initialize Model Estimation Using Presample Response Data
% Because an ARIMA model is a function of previous values, estimate requires presample data to initialize the model early in the sampling period. Although, estimate backcasts for presample data by default, you can specify required presample data instead. The P property of an arima model object specifies the required number of presample observations.
%
%% 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);
T = size(TT,1); % Total sample size

%
%% Create Model Template
% 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.
%
%% Partition Sample
% Create vectors of indices that partition the sample into presample and estimation sample periods, so that the presample occurs first and contains Mdl.P = 2 observations, and the estimation sample contains the remaining observations.

presample = 1:Mdl.P;
estsample = (Mdl.P + 1):T;

%
%% Estimate Model
% Fit an ARIMA(1,1,1) model to the estimation sample. Specify the presample responses.

EstMdl = estimate(Mdl,TT{estsample,"NYSE"},'Y0',TT{presample,"NYSE"});

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