%% Estimate Time-Varying Diffuse State-Space Model
% This example shows how to create and estimate a diffuse state-space model
% containing time-varying parameters.
%
%%
% Suppose that an AR(2) and an MA(1) model comprise a latent process.
% 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}} = 0.7{x_{1,t - 1}} - 0.2{x_{1,t - 2}} + {u_{1,t}}\\
% {x_{2,t}} = {u_{2,t}} + 0.6{u_{2,t - 1}}.
% \end{array}$$
% 
% For the last 25 periods, the state equation is
%
% $${x_{1,t}} = 0.7{x_{1,t - 1}} - 0.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.
%
%%
% Generate a random series of 50 observations from $x_{1,t}$ and $x_{2,t}$,
% assuming that the series starts at 1.5 and 1, respectively.

% Copyright 2015 The MathWorks, Inc.

T = 50;
ARMdl = arima('AR',{0.7,-0.2},'Constant',0,'Variance',1);
MAMdl = arima('MA',0.6,'Constant',0,'Variance',1);
x0 = [1.5 1; 1.5 1];
rng(1);
x = [simulate(ARMdl,T,'Y0',x0(:,1)),...
    [simulate(MAMdl,T/2,'Y0',x0(:,2));nan(T/2,1)]];
%%
% The last 25 values for the simulated MA(1) data are missing.
%%
% The latent processes are measured using
%
% $${y_t} = 2\left( {{x_{1,t}} + {x_{2,t}}} \right) + {\varepsilon _t}$$
%
% for the first 25 periods, and 
% 
% $${y_t} = 2{x_{1,t}} + {\varepsilon _t}$$
%
% for the last 25 periods. $\varepsilon_t$ is Gaussian with mean 0 and
% standard deviation 1. 
%%
% Generate observations using the random, latent state process (|x|) and the
% observation equation.
y = 2*sum(x','omitnan')' + randn(T,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}{\rm for\;}t = 1,...,25,$$
%
% $$\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}{\rm for\;}t = 26,$$
%
% $$\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}{\rm for\;}t = 27,...,50.$$
%
%%
% 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 in a file named |diffuseAR2MAParamMap| on your
% MATLAB(R) path.
%%
% Create the state-space model by passing the function |diffuseAR2MAParamMap| as a
% function handle to |dssm|.
Mdl = dssm(@(params)diffuseAR2MAParamMap(params,T));
%%
% |dssm| implicitly defines the diffuse state-space model. Usually, you
% cannot verify diffuse state-space models that are implicitly created.
%%
% To estimate the parameters, pass the observed responses (|y|) to
% |estimate| . Specify positive initial values for the unknown parameters.
params0 = 0.1*ones(5,1);
EstMdl = estimate(Mdl,y,params0)
%%
% The estimated parameters are within one standard error of their true
% values, but the standard errors are quite high. Likelihood surfaces of
% state-space models might contain local maxima. Therefore, try several
% initial parameter values, or consider using |refine|.