%% Estimate ARMA Model
% Fit an ARMA(2,1) model to simulated data.
%
%% Simulate Data from Known Model
% Suppose that the data generating process (DGP) is
% {y_t} = 0.5{y_{t - 1}} - 0.3{y_{t - 2}} + {\varepsilon _t} +
% 0.2{\varepsilon _{t - 1}},
% where \varepsilon_{t} is a series of iid Gaussian random variables with mean 0 and variance 0.1.
% Create the ARMA(2,1) model representing the DGP.

DGP = arima('AR',{0.5,-0.3},'MA',0.2,...
    'Constant',0,'Variance',0.1)

% DGP is a fully specified arima model object.
% Simulate a random 500 observation path from the ARMA(2,1) model.

rng(5); % For reproducibility
T = 500;
y = simulate(DGP,T);

% y is a 500-by-1 column vector representing a simulated response path from the ARMA(2,1) model DGP.
%
%% Estimate Model
% Create an ARMA(2,1) model template for estimation.

Mdl = arima(2,0,1)

% Mdl is a partially specified arima model object. Only required, nonestimable parameters that determine the model structure are specified. NaN-valued properties, including \phi_1, \phi_2, \theta_1, c, and \sigma^2, are unknown model parameters to be estimated.
% Fit the ARMA(2,1) model to y.

EstMdl = estimate(Mdl,y)

% MATLAB® displays a table containing an estimation summary, which includes parameter estimates and inferences. For example, the Value column contains corresponding maximum-likelihood estimates, and the PValue column contains p-values for the asymptotic t-test of the null hypothesis that the corresponding parameter is 0.
% EstMdl is a fully specified, estimated arima model object; its estimates resemble the parameter values of the DGP.
% Copyright 2015 The MathWorks, Inc.