%% Implicitly Create Time-Varying State-Space Model
%%
% This example shows how to create a time-varying, state-space model by
% passing a parameter-mapping function describing the model to |ssm| (i.e.,
% _implicitly_ create a state-space model). 
%%
% Suppose that from periods 1 through 10, the state model are stationary
% AR(2) and MA(1) models, respectively, and the observation model is the
% sum of the two states. From periods 11 through 20, the state model only
% includes the first AR(2) model. 
%%
% Symbolically, the models
% are:
%
% $$\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&1\\
% 0&1
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {{u_{1t}}}\\
% {{u_{2t}}}
% \end{array}} \right]\\
% {y_t} = {a_1}\left( {{x_{1t}} + {x_{3t}}} \right) + {\sigma _2}{\varepsilon _t}.
% \end{array}{\rm for\;}t = 1,...,10,$$
% 
% $$\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
% \end{array}} \right]{u_{1t}}\\
% {y_t} = {a_2}{x_{1t}} + {\sigma _3}{\varepsilon _t}.
% \end{array}{\rm for\;}t = 11,$$
%
% $$\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
% \end{array}} \right]{u_{1t}}\\
% {y_t} = {a_2}{x_{1t}} + {\sigma _3}{\varepsilon _t}.
% \end{array}{\rm for\;}t = 12,...,20.$$
% 
%%
% 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>timeVariantParamMap.m</include>
%
%%
% Save this code as a file named |timeVariantParamMap.m| on your MATLAB(R)
% path.
%%
% Create the state-space model by passing the function
% |timeVariantParamMap| as a function handle to |ssm|.

% Copyright 2015 The MathWorks, Inc.

Mdl = ssm(@timeVariantParamMap);
%%
% |ssm| implicitly creates the state-space model. Usually, you
% cannot verify implicitly created state-space models.
%%
% |Mdl| is an |ssm| model object containing unknown parameters.  You can
% estimate the unknown parameters by passing |Mdl| and response data to
% |estimate|.