%% Specify Number of Periods
% Explicitly create the state-space model
% \begin{array}{*{20}{l}}
% {{x_{1,t}}} = 0.9x_{1,t-1} + 0.2u_{1,t}\\
% {{x_{2,t}}} = 0.1x_{1,t-1}-0.3x_{2,t-1} + u_{2,t}\\
% {{y_{1,t}}} = x_{1,t}  +\varepsilon_{1,t}\\
% {{y_{2,t}}} = x_{1,t} + x_{2,t} +\varepsilon_{2,t}.\\
% \end{array}}

A = [0.9 0; 0.1 -0.3];
B = [0.2 0; 0 1];
C = [1 0; 1 1];
D = eye(2);
Mdl = ssm(A,B,C,D,'StateType',[0 0]);

% Mdl is an ssm model object.
% Compute the temporal correlations of the measurement variables from lag 0 through 20.

numlags = 20;
Cyy = corr(Mdl,'NumLags',numlags);

% Cyy is a 21-by-2-by-2 array representing the 20-period temporal correlations of the measurement variables.
% Display Cyy(:,2,2), which is the model-implied autocorrelation of y_{2,t}.

acfy2 = Cyy(:,2,2)

% Generate a random path of measurements of length 200 from the model.

rng(1); % For reproducibility
Y = simulate(Mdl,200);

% Compute the sample autocorrelation function (ACF) of each variable for 2q0 lags.

sacfy1 = autocorr(Y(:,1),'NumLags',numlags);
sacfy2 = autocorr(Y(:,2),'NumLags',numlags);

% Visually compare the model-implied and sample ACF of each measurement variable.

acfy1 = Cyy(:,1,1);
plot([acfy1 sacfy1])
xticklabels(0:numlags)
ylabel("Autocorrelation")
xlabel("Lags")
legend(["ACF(y_{1,t})" "Sample ACF(y_{1,t})"])
title("ACF(y_{1,t})")
axis tight
plot([acfy2 sacfy2])
xticklabels(0:numlags)
ylabel("Autocorrelation")
xlabel("Lags")
legend(["ACF(y_{2,t})" "Sample ACF(y_{2,t})"])
title("ACF(y_{2,t})")
axis tight

%
% Copyright 2021 The MathWorks, Inc.
%