%% Implicitly Create Time-Varying Diffuse State-Space Model
% This example shows how to create a diffuse state-space model in which one
% of the state variables drops out of the model after a certain period.
%%
% Suppose that a latent process comprises an AR(2) and an MA(1) model.
% There are 50 periods, and the MA(1) process drops out of the model for
% the final 25 periods.  Consequently, the state equation for the first 25
% periods is
%
% $$\begin{array}{l}
% {x_{1,t}} = \phi_1{x_{1,t - 1}} - \phi_2{x_{1,t - 2}} + {u_{1,t}}\\
% {x_{2,t}} = {u_{2,t}} + \theta_1{u_{2,t - 1}},
% \end{array}$$
%
% and for the last 25 periods, it is 
%
% $${x_{1,t}} = \phi_1{x_{1,t - 1}} - \phi_2{x_{1,t - 2}} + {u_{1,t}},$$
%
% where $u_{1,t}$ and $u_{2,t}$ are Gaussian with mean 0 and standard
% deviation 1.
%%
% The latent processes are measured using
%
% $${y_t} = a\left( {{x_{1,t}} + {x_{2,t}}} \right) + {\varepsilon _t},$$
%
% for the first 25 periods, and
%
% $${y_t} = b{x_{1,t}} + {\varepsilon _t}$$
%
% for the last 25 periods, where $\varepsilon_t$ is Gaussian with mean 0
% and standard deviation 1.
%%
% Together, the latent process and observation equations make up a
% state-space model.  If the coefficients are unknown
% parameters, the state-space model is
% 
% $$\begin{array}{l}
% \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}}
% {{\phi _1}}&{{\phi _2}}&0&0\\
% 1&0&0&0\\
% 0&0&0&{{\theta _1}}\\
% 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}}
% 1&0\\
% 0&0\\
% 0&1\\
% 0&1
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {{u_{1,t}}}\\
% {{u_{2,t}}}
% \end{array}} \right]\\
% {y_t} = a({x_{1,t}} + {x_{3,t}}) + {\varepsilon _t}
% \end{array}$$
%
% for the first 25 periods,
%
% $$\begin{array}{l}
% \left[ {\begin{array}{*{20}{c}}
% {{x_{1,t}}}\\
% {{x_{2,t}}}
% \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}}
% 1\\
% 0
% \end{array}} \right]{u_{1,t}}\\
% {y_t} = b{x_{1,t}} + {\varepsilon _t}
% \end{array}$$
%
% for period 26, and
%
% $$\begin{array}{l}
% \left[ {\begin{array}{*{20}{c}}
% {{x_{1,t}}}\\
% {{x_{2,t}}}
% \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}}
% 1\\
% 0
% \end{array}} \right]{u_{1,t}}\\
% {y_t} = b{x_{1,t}} + {\varepsilon _t}
% \end{array}$$
%
% for the last 24 periods.
%%
% Write a function that specifies how the parameters in |params| map to the
% state-space model matrices, the initial state values, and the type of
% state.
%
% <include>diffuseAR2MAParamMap.m</include>
%
%%
% Save this code as a file named |diffuseAR2MAParamMap| on your MATLAB(R)
% path.
%%
% Create the diffuse state-space model by passing the function
% |diffuseAR2MAParamMap| as a function handle to |dssm|.  This example uses
% 50 observations.

% Copyright 2015 The MathWorks, Inc.

T = 50;
Mdl = dssm(@(params)diffuseAR2MAParamMap(params,T));
%%
% |dssm| implicitly creates the diffuse state-space model. Usually, you
% cannot verify diffuse state-space models that are implicitly created.
%%
% |dssm| contains unknown parameters.  You can simulate data and then
% estimate parameters using |estimate|.
