%% IRF of Moving Average Model
% This example shows how to plot and return the IRF of a pure MA model by using impulse and armairf. The example also shows how to change the color of the plotted IRFs.
% The equation for an MA(q) model is
% {y_t} = \mu  + \theta (L){\varepsilon _t},
% where \theta(L) is a q-degree MA lag operator polynomial, (1 + {\theta _1}L +  \ldots  + {\theta _q}{L^q}).
% The IRF for an MA model is the sequence of MA coefficients 1,{\theta _1}, \ldots ,{\theta _q}.
%
%% Plot IRF Using impulse
% Create a zero-mean MA(3) model with coefficients \theta_1 = 0.8, \theta_2 = 0.5, and \theta_3 = -0.1, and an innovation variance of 1.

ma = [0.8 0.5 -0.1];
Mdl = arima('Constant',0,'MA',ma,'Variance',1);

% Mdl is a fully specified arima model object representing the MA(3) model.
% Plot the IRF of the MA(3) model.

impulse(Mdl)

% impulse returns a stem plot containing 1 at period 0, followed by the values of the MA coefficients at their lags.
% For an MA model, the impulse response function stops after q periods. In this example, the last nonzero coefficient is at lag q = 3.
% Return the IRF by calling impulse and specifying an output argument.

periods = (0:3)';
dm = impulse(Mdl);
IRF = table(periods,dm)

%
% To change aspects of the stem plot, you must set values of its properties. The stem plot handle is in the Children property of the plot axes handle.
% Extract the stem plot handle from the current axes handle.

h = gca;
hstem = h.Children;

% Change the color of the stem plot to red by using the RGB color value.

hstem.Color = [1 0 0]; 

%
%% Plot IRF Using armairf
% Plot the IRF of the MA(3) model by passing ma as the MA coefficients (second input). Specify an empty array for the AR polynomial coefficients (first input). Return the IRF and plot handle.

[dm,h] = armairf([],ma);
table(periods,dm)

% Unlike impulse, armairf returns a time series plot.
%
% Change the color of the plot line to red.

h.Color = [1 0 0];

% Copyright 2021 The MathWorks, Inc.