%% Create Known Diffuse State-Space Model with Initial State Values
% Create a diffuse state-space model, where:
% The state x_{1,t} is a stationary AR(2) model with \phi_1 = 0.6, \phi_2 = 0.2, and a constant 0.5. The state disturbance is a mean zero Gaussian random variable with standard deviation 0.3.
% The state x_{4,t} is a random walk. The state disturbance is a mean zero Gaussian random variable with standard deviation 0.05.
% The observation y_{1,t} is the difference between the current and previous value in the AR(2) state, plus a mean 0 Gaussian observation innovation with standard deviation 0.1.
% The observation y_{2,t} is the random walk state plus a mean 0 Gaussian observation innovation with standard deviation 0.02.
% Symbolically, the state-space model is
% \left[
% {\begin{array}{*{20}{c}}{{x_{1,t}}}\\{{x_{2,t}}}\\{{x_{3,t}}}\\{{x_{4,t}}}\end{array}}
% \right] = \left[
% {\begin{array}{*{20}{c}}0.6&0.2&0.5&0\\1&0&0&0\\0&0&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}}}\\{{x_{4,t - 1}}}\end{array}} \right] + \left[
% {\begin{array}{*{20}{c}}{0.3} & 0 \\ 0 & 0\\ 0 & 0\\ 0 & 0.05 \end{array}}
% \right]\left[
% {\begin{array}{*{20}{c}}{{u_{1,t}}}\\{{u_{4,t}}}\end{array}}
% \right]
% \left[\begin{array}{*{20}{c}}y_{1,t}\\y_{2,t}\end{array}\right] =
% \left[ {\begin{array}{*{20}{c}}1&-1&0&0\\0&0&0&1\end{array}}
% \right]\left[
% {\begin{array}{*{20}{c}}{{x_{1,t}}}\\{{x_{2,t}}}\\{{x_{3,t}}}\\{{x_{4,t}}}\end{array}}
% \right]+\left[\begin{array}{*{20}{c}}0.1 & 0\\ 0 &
% 0.02\end{array}\right]\left[\begin{array}{*{20}{c}}\varepsilon_{1,t}\\\varepsilon_{2,t}\end{array}\right].
% The model has four states: x_{1,t} is the AR(2) process, x_{2,t} represents x_{1,t-1}, x_{3,t} is the AR(2) model constant, and x_{4,t} is the random walk.
% Define the state-transition matrix.

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

%
% Define the state-disturbance-loading matrix.

B = [0.3 0; 0 0; 0 0; 0 0.05];

%
% Define the measurement-sensitivity matrix.

C = [1 -1 0 0; 0 0 0 1];

%
% Define the observation-innovation matrix.

D = [0.1; 0.02];

%
% Use dssm to create the state-space model. 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.
% x_{4,t} is nonstationary.
% Set the initial state means (Mean0) to 0. The initial state mean for constant states must be 1.

Mean0 = [0; 0; 1; 0];     
StateType = [0; 0; 1; 2];
Mdl = dssm(A,B,C,D,'Mean0',Mean0,'StateType',StateType)

% Mdl is a dssm model object.  dssm sets the initial state:
% Covariance matrix for the stationary states to the asymptotic covariance of the AR(2) model
% Variance for constant states to 0
% Variance for diffuse states to Inf
%
% You can display or modify properties of Mdl using dot notation. For example, display the initial state covariance matrix.

Mdl.Cov0

%
% Reset the initial state means for the stationary states to their asymptotic values.

Mdl.Mean0(1:2) = 0.5/(1-0.2-0.6);
Mdl.Mean0

% Copyright 2015 The MathWorks, Inc.