%% Fit VAR Model to Simulated Data
% This example simulates data from an arbitrary 3-D VAR(2) model, and fits a VAR(2) model to the simulated data.
% Create the following VAR model.
% {y_t} = \left[ {\begin{array}{*{20}{c}}
% 1\\
% {0.5}\\
% { - 0.5}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% {0.3}&{ - 0.1}&{0.05}\\
% {0.1}&{0.2}&{0.1}\\
% { - 0.1}&{0.2}&{0.4}
% \end{array}} \right]{y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% {0.1}&{0.05}&{0.001}\\
% {0.001}&{0.1}&{0.01}\\
% { - 0.01}&{ - 0.01}&{0.2}
% \end{array}} \right]{y_{t - 2}} + \left[ {\begin{array}{*{20}{c}}
% {0.5}\\
% { - 0.2}\\
% 0
% \end{array}} \right]t + \texttt{L}{\varepsilon _t}.
% \varepsilon_t is a 3-D standard Gaussian random variable and L is the 3-by-3 identity matrix.

Constant = [1; 0.5; -0.5];
AR1 = [0.3 -0.1 0.05; 0.1 0.2 0.1; -0.1 0.2 0.4];
AR2 = [0.1 0.05 0.001; 0.001 0.1 0.01; -0.01 -0.01 0.2];
Trend = [0.5; -0.2; 0];
L = eye(3);
TrueMdl = varm('Constant',Constant,'AR',{AR1 AR2},'Trend',Trend,...
    'Covariance',L);

% TrueMdl is a fully specified varm model object, which means all parameters of its corresponding VAR(2) model are known.
%
% Generate a time series path of length 100 using simulate.

rng(1); % For reproducibility
numobs = 100;
Y = simulate(TrueMdl,numobs);

%
% Create a 3-D VAR(2) model template for estimation. Specify the presence of a linear trend component.

Mdl = varm(3,2);
Mdl.Trend = nan(Mdl.NumSeries,1);

% Mdl is a varm model object serving as a template for estimation.
%
% Fit the VAR model to the simulated data by calling estimate.

EstMdl = estimate(Mdl,Y);

% EstMdl is a fully specified, estimated varm model object. The estimation sample size is 98 because estimate requires 2 presample observations for initialization.
%
% Compare the estimated model with the true model.

results = summarize(EstMdl)
Lhat = chol(EstMdl.Covariance,'lower')

trueValues = [Constant; AR1(:); AR2(:); Trend];
compCoeff = abs(results.Table.Value - trueValues);
twoSE = compCoeff > 2*results.Table.StandardError

% The estimate of L, the innovations scale matrix, is fairly close in magnitude to the identity matrix. Only the estimated constant in the first response series is two standard errors further from its true value.
% Copyright 2012 The MathWorks, Inc.