%% Convert VARMA Model to VMA Model
% This example converts the polynomials of a VARMA model to a pure MA polynomial of a VMA model.
% Specify the coefficient matrices of a VARMA(3,2) model.

A1 = [.2 -.1 0;.1 .2 .05;0 .1 .3];
A2 = [.3 0 0;.1 .4 .1;0 0 .2];
A3 = [.4 .1 -.1;.2 -.5 0;.05 .05 .2];
MA1 = .2*eye(3);
MA2 = [.3 .2 .1;.2 .4 0;.1 0 .5];

%
% Convert the VARMA model matrices to a VAR model representation.

ma = arma2ma({A1 A2 A3},{MA1 MA2});

% ma is a 1-by-47 cell vector of coefficient matrices of the corresponding VMA model.
%
% Determine whether the VMA representation is invertible by creating a lag operator polynomial for the corresponding matrices, and then passing it to isStable.

ma = [{eye(3)} ma]; % Include the lag 0 coefficient.
Mdl = LagOp(ma);
isMAInvertible = isStable(Mdl)

% isMAInvertible is 1, which indicates that the MA portion of the VARMA model is invertible.
%
% Represent the VMA model as a pure VAR model.

arLagOp = arma2ar([],Mdl);

% arLagOp is a LagOp autoregression polynomial object. It contains coefficients for lags 0 through 18.
%
% Represent the coefficients in difference equation notation. Extract the coefficients for lags greater than 0.

arLagOp = reflect(arLagOp);
ar = toCellArray(arLagOp);
ar = ar(2:end);

%
% Compare the matrices resulting from converting the coefficients from the VARMA model to the VMA model, then to the VAR model, to the coefficients resulting from converting the VARMA model directly to the VAR model.

ar1 = arma2ar({A1 A2 A3},{MA1 MA2});
compCoeff = cellfun(@(x,y)sum(sum((x - y)'*(x - y))),ar,ar1);
sum(compCoeff) > eps

% The differences between the matrices are negligible.
% Copyright 2012 The MathWorks, Inc.