%% Compare Generalized and Orthogonalized Impulse Response Functions
% This example shows the differences between generalized and orthogonalized impulse response functions (IRFs) using the 3-D vector autoregression model containing the first two lags (VAR(2)) in docid:econ_ug.brz_lcd, p. 78. The variables in the model represent the quarterly rates of fixed investment, disposable income, and consumption expenditures of Germany. The estimated model is
% {y_t} = \left[ {\begin{array}{*{20}{c}}
% { - 0.017}\\
% {0.016}\\
% {0.013}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% { - 0.320}&{0.146}&{0.961}\\
% {0.044}&{ - 0.153}&{0.289}\\
% { - 0.002}&{0.225}&{ - 0.264}
% \end{array}} \right]{y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% { - 0.161}&{0.115}&{0.934}\\
% {0.050}&{0.019}&{ - 0.010}\\
% {0.034}&{0.355}&{ - 0.022}
% \end{array}} \right]{y_{t - 2}} + {\varepsilon _t},
% where y_t = \left[y_{1t}\;\;\;y_{2t}\;\;\;y_{3t}\right]' and \varepsilon_t = \left[\varepsilon_{1t}\;\;\;\varepsilon_{2t}\;\;\;\varepsilon_{3t}\right]'. The estimated covariance matrix of the innovations is
% \hat\Sigma = \left[ {\begin{array}{*{20}{c}}
% {21.30}&{0.72}&{1.23}\\
% {0.72}&{1.37}&{0.61}\\
% {1.23}&{0.61}&{0.89}
% \end{array}} \right]10^{-4}.
% The VAR(2) model contains a constant, but because the IRF is the derivative of y_t with respect to \varepsilon_t, the constant does not affect the IRF.
% Create a cell vector containing the autoregressive coefficient matrices, and create the innovations covariance matrix.

AR1 = [-0.320  0.146  0.961;
        0.044 -0.153  0.289;
       -0.002  0.225 -0.264];
AR2 = [-0.161 0.115  0.934;
        0.050 0.019 -0.010;
        0.034 0.355 -0.022];
ar0 = {AR1 AR2};

InnovCov = [21.30 0.72 1.23;
             0.72 1.37 0.61;
             1.23 0.61 0.89]*1e-4;

%
% Plot and compute the orthogonalized IRF in one armairf call by additionally returning the handle to the plotted graphics objects (the second output). Because no vector moving average (VMA) coefficients exist, specify an empty array ([]) for the second input argument.

[OrthoY,h] = armairf(ar0,[],'InnovCov',InnovCov);

% armairf returns separate figures, each containing IRFs of a variable in the system.  Within a figure, armairf plots three separate line plots for the response of the variable to shocks to the three variables in the system at time 0. The orthogonalized impulse responses seem to fade after nine periods.
% OrthoY is a 10-by-3-by-3 matrix of impulse responses. Each row corresponds to a time in the forecast horizon (0,...,9), each column corresponds to a variable receiving the shock at time 0, and each page corresponds to the IRF of a variable.
%
% Plot and compute the generalized IRF.

[GenY,h] = armairf(ar0,[],'InnovCov',InnovCov,'Method','generalized');

% The generalized impulse responses seem to fade after nine periods, and appear to behave similarly to the orthogonalized impulse responses.
% Display both sets of impulse responses.

for j = 1:3
    fprintf('Shock to Variable %d',j)
    table(squeeze(OrthoY(:,j,:)),squeeze(GenY(:,j,:)),'VariableNames',{'Orthogonalized',...
        'Generalized'})
end

% If armairf shocks the first variable, then the impulse responses of all variables are equivalent between methods. The second and third columns suggest that the generalized and orthogonalized impulse responses are generally different. However, if InnovCov is diagonal, then both methods produce the same impulse responses.
% Another difference between the two methods is that generalized impulse responses are invariant to the order of the variables, whereas orthogonalized impulse responses differ based on the variable order.
% Copyright 2015 The MathWorks, Inc.