%% Explicitly Create and Display Time-Varying Diffuse State-Space Model
% From periods 1 through 50, the state model is a diffuse AR(2) and a stationary MA(1) model, and the observation model is the sum of the two states. From periods 51 through 100, the state model includes the first AR(2) model only. Symbolically, the state-space model is, for periods 1 through 50,
% \begin{array}{c}\left[
% {\begin{array}{*{20}{c}}{{x_{1t}}}\\{{x_{2t}}}\\{{x_{3t}}}\\{{x_{4t}}}\end{array}}
% \right] = \left[ {\begin{array}{*{20}{c}}{{\phi _1}}&{{\phi
% _2}}&0&0\\1&0&0&0\\0&0&0&\theta \\0&0&0&0\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}}{{\sigma
% _1}}&0&0&0\\0&0&0&0\\0&0&1&{\rm{0}}\\0&0&1&0\end{array}} \right]\left[
% {\begin{array}{*{20}{c}}{{u_{1t}}}\\{{u_{2t}}}\\{{u_{3t}}}\\{{u_{4t}}}\end{array}}
% \right]\\{y_t} = {a_1}\left( {{x_{1t}} + {x_{3t}}} \right) + {\sigma
% _2}{\varepsilon _t}\end{array},
% for period 51,
% \begin{array}{c}
% \left[ {\begin{array}{*{20}{c}}
% {{x_{1t}}}\\
% {{x_{2t}}}
% \end{array}} \right] = \left[ {\begin{array}{*{20}{c}}
% {{\phi _1}}&{{\phi _2}}&0&0\\
% 1&0&0&0
% \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}}
% {{\sigma _1}}&0&0&0\\
% 0&0&0&0
% \end{array}} \right]\left[
% {\begin{array}{*{20}{c}}{{u_{1t}}}\\{{u_{2t}}}\\{{u_{3t}}}\\{{u_{4t}}}\end{array}}
% \right]\\
% {y_t} = {a_2}{x_{1t}} + {\sigma _3}{\varepsilon _t}
% \end{array}
% and for periods 52 through 100,
% \begin{array}{c}
% \left[ {\begin{array}{*{20}{c}}
% {{x_{1t}}}\\
% {{x_{2t}}}
% \end{array}} \right] = \left[ {\begin{array}{*{20}{c}}
% {{\phi _1}}&{{\phi _2}}\\
% 1&0
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {{x_{1,t - 1}}}\\
% {{x_{2,t - 1}}}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% {{\sigma _1}}&0\\
% 0&0
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {{u_{1t}}}\\
% {{u_{2t}}}
% \end{array}} \right]\\
% {y_t} = {a_2}{x_{1t}} + {\sigma _3}{\varepsilon _t.}
% \end{array}
% Specify the state-transition coefficient matrix.

A1 = {[NaN NaN 0 0; 1 0 0 0; 0 0 0 NaN; 0 0 0 0]};
A2 = {[NaN NaN 0 0; 1 0 0 0]};
A3 = {[NaN NaN; 1 0]};
A = [repmat(A1,50,1);A2;repmat(A3,49,1)];

%
% Specify the state-disturbance-loading coefficient matrix.

B1 = {[NaN 0 0 0; 0 0 0 0; 0 0 1 0; 0 0 1 0]};
B2 = {[NaN 0 0 0; 0 0 0 0]};
B3 = {[NaN 0; 0 0]};
B = [repmat(B1,50,1);B2;repmat(B3,49,1)];

%
% Specify the measurement-sensitivity coefficient matrix.

C1 = {[NaN 0 NaN 0]};
C3 = {[NaN 0]};
C = [repmat(C1,50,1);repmat(C3,50,1)];

%
% Specify the observation-disturbance coefficient matrix.

D1 = {NaN};
D3 = {NaN};
D = [repmat(D1,50,1);repmat(D3,50,1)];

%
% Create the diffuse state-space model. Specify that the initial state distributions are diffuse for the states composing the AR model and stationary for those composing the MA model.

StateType = [2; 2; 0; 0];

Mdl = dssm(A,B,C,D,'StateType',StateType);

% Mdl is an dssm model object.
% The model is large and contains a different set of parameters for each period. The software displays state and observation equations for the first 10 and last 10 periods. You can choose which periods to display the equations for using the 'Period' name-value pair argument.
%
% Display the diffuse state-space model, and use 'Period' display the state and observation equations for the 50th, 51st, and 52nd periods.

disp(Mdl,'Period',50)
disp(Mdl,'Period',51)
disp(Mdl,'Period',52)

% The software attributes a different set of coefficients for each period. You might experience numerical issues when you estimate such models. To reuse parameters among groups of periods, consider creating a parameter-to-matrix mapping function.
% Copyright 2012 The MathWorks, Inc.