%% IRF of Autoregressive Model
% This example shows how to plot the IRF of an AR model by using impulse and armairf. Also, the example shows how changes to the innovation variance affect the IRF.
% The equation of an AR(p) model is
% {y_t} = c  + \phi {(L)^{ - 1}}{\varepsilon _t},
% where \phi(L) is the p-degree AR lag operator polynomial (1 - {\phi _1}L -  \ldots  - {\phi _p}{L^p}).
% An AR process is stationary when the AR lag operator polynomial is stable, which means all its roots lie outside the unit circle. In this case, the infinite-degree inverse polynomial \psi (L) = \phi {(L)^{ - 1}} has absolutely summable coefficients, and the IRF decays to zero.
%
%% Plot IRF Using impulse
% Create an AR(2) model with coefficients \phi_1 = 0.5 and \phi_2 = -0.75, a model constant of 0.5, and an innovation variance of 1.

ar = [0.5 -0.75];
Mdl = arima('Constant',0.5,'AR',ar,'Variance',1);

% Plot the IRF of the AR(2) model for 31 periods, from periods 0 through 30.

numObs = 31;
impulse(Mdl,numObs)

% The IRF decays in a sinusoidal pattern.
% Increase the constant to 100, and then plot the IRF of the adjusted AR(2) model.

Mdl.Constant = 100;
impulse(Mdl,numObs)

% Because deterministic components are not present in the IRF, it is unaffected by the increased constant.
% Decrease the innovation variance to 1e-5, and then plot the IRF of the adjusted AR(2) model.

Mdl.Variance = 1e-5;
impulse(Mdl,numObs)

% Because impulse always applies a unit shock to the innovation of the system, the IRF is unaffected by the decreased innovation variance.
%
%% Plot IRF Using armairf
% Plot the IRF of the original AR(2) model by passing ar as the AR coefficients (first input). Specify an empty array for the MA polynomial coefficients (second input). Specify 31 periods.

armairf(ar,[],'NumObs',numObs)

%  Plot the IRF specifying an innovation variance of 1e-5.

armairf(ar,[],'NumObs',numObs,'InnovCov',1e-5);

% Because armairf applies a one-standard-deviation innovation shock to the system, the scale of the IRF is smaller in this case.
% Copyright 2021 The MathWorks, Inc.