# 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}{c}1\\ 0.5\\ -0.5\end{array}\right]+\left[\begin{array}{ccc}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}{ccc}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}{c}0.5\\ -0.2\\ 0\end{array}\right]t+\text{L}{\epsilon }_{t}.$

${\epsilon }_{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)
results = struct with fields:
Description: "AR-Stationary 3-Dimensional VAR(2) Model with Linear Time Trend"
SampleSize: 98
NumEstimatedParameters: 24
LogLikelihood: -396.0032
AIC: 840.0063
BIC: 902.0456
Table: [24x4 table]
Covariance: [3x3 double]
Correlation: [3x3 double]

Lhat = chol(EstMdl.Covariance,'lower')
Lhat = 3×3

0.9632         0         0
-0.0926    0.9555         0
0.0337   -0.0190    0.8755

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

1
0
0
0
0
0
0
0
0
0
⋮

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.