%% Create Known State-Space Model with Initial State Values
% Create a state-space model, where the state equation is an AR(2) model. The state disturbances are mean zero Gaussian random variables with standard deviation of 0.3. The observation equation is the difference between the current and previous state plus a mean zero Gaussian observation innovation with a standard deviation of 0.1. Symbolically, the state-space model is
% \left[
% {\begin{array}{*{20}{c}}{{x_{1,t}}}\\{{x_{2,t}}}\\{{x_{3,t}}}\end{array}}
% \right] = \left[
% {\begin{array}{*{20}{c}}0.6&0.2&0.5\\1&0&0\\0&0&1\end{array}}
% \right]\left[ {\begin{array}{*{20}{c}}{{x_{1,t - 1}}}\\{{x_{2,t -
% 1}}}\\{{x_{3,t - 1}}}\end{array}} \right] + \left[
% {\begin{array}{*{20}{c}}{0.3}\\ 0 \\ 0 \end{array}} \right]u_{1,t}
% {y_t} = \left[ {\begin{array}{*{20}{c}}1&-1&0\end{array}}
% \right]\left[
% {\begin{array}{*{20}{c}}{{x_{1,t}}}\\{{x_{2,t}}}\\{{x_{3,t}}}\end{array}}
% \right]+0.1\varepsilon_t.
% There are three states: x_{1,t} is the AR(2) process, x_{2,t} represents x_{1,t-1}, and x_{3,t} is the AR(2) model constant.
% Define the state-transition matrix.

A = [0.6 0.2 0.5; 1 0 0; 0 0 1];

%
% Define the state-disturbance-loading matrix.

B = [0.3; 0; 0];

%
% Define the measurement-sensitivity matrix.

C = [1 -1 0];

%
% Define the observation-innovation matrix.

D = 0.1;

%
% Use ssm to create the state-space model. Set the initial-state mean (Mean0) and covariance matrix (Cov0). Identify the type of initial state distributions (StateType) by noting the following:
% x_{1,t} is a stationary, AR(2) process.
% x_{2,t} is also a stationary, AR(2) process.
% x_{3,t} is the constant 1 for all periods.

Mean0 = [0; 0; 1];                                        % The mean of the AR(2)
varAR2 = 0.3*(1 - 0.2)/((1 + 0.2)*((1 - 0.2)^2 - 0.6^2)); % The variance of the AR(2)
Cov1AR2 = 0.6*0.3/((1 + 0.2)*((1 - 0.2)^2) - 0.6^2);      % The covariance of the AR(2)
Cov0 = zeros(3);
Cov0(1:2,1:2) = varAR2*eye(2) + Cov1AR2*flip(eye(2));
StateType = [0; 0; 1];
Mdl = ssm(A,B,C,D,'Mean0',Mean0,'Cov0',Cov0,'StateType',StateType)

% Mdl is an ssm model.
%
% You can display properties of Mdl using dot notation. For example, display the initial state covariance matrix.

Mdl.Cov0

% Copyright 2015 The MathWorks, Inc.