%% Simulate Responses Using filter
% Illustrate the relationship between simulate and filter by estimating a 4-dimensional VAR(2) model of the four response series in Johansen's Danish data set. Simulate a single path of responses using the fitted model and the historical data as initial values, and then filter a random set of Gaussian disturbances through the estimated model using the same presample responses.
% Load Johansen's Danish economic data.

load Data_JDanish

% For details on the variables, enter Description.
%
% Create a default 4-D VAR(2) model.

Mdl = varm(4,2);

%
% Estimate the VAR(2) model using the entire data set.

EstMdl = estimate(Mdl,Data);

% When reproducing the results of simulate and filter, it is important to take these actions.
% Set the same random number seed using rng.
% Specify the same presample response data using the 'Y0' name-value pair argument.
%
% Set the default random seed. Simulate 100 observations by passing the estimated model to simulate. Specify the entire data set as the presample.

rng default
YSim = simulate(EstMdl,100,'Y0',Data);

% YSim is a 100-by-4 matrix of simulated responses. Columns correspond to the columns of the variables in Data.
%
% Set the default random seed. Simulate 4 series of 100 observations from the standard Gaussian distribution.

rng default
Z = randn(100,4);

%
% Filter the Gaussian values through the estimated model. Specify the entire data set as the presample.

YFilter = filter(EstMdl,Z,'Y0',Data);

% YFilter is a 100-by-4 matrix of simulated responses. Columns correspond to the columns of the variables in the data Data. Before filtering the disturbances, filter scales Z by the lower triangular Cholesky factor of the model covariance in EstMdl.Covariance.
%
% Compare the resulting responses between filter and simulate.

(YSim - YFilter)'*(YSim - YFilter)

% The results are identical.
% Copyright 2012 The MathWorks, Inc.