%% Create Fully Specified Multivariate Model
% This example shows how to create a fully specified, three-state Markov-switching dynamic regression model.
% Consider the response processes y_{1t} and y_{2t} that switch between 3 states, governed by the latent process s_t with this observed transition matrix
% \[P = \left[ {\begin{array}{*{20}{c}}
% 10&1&1\\
% 1&10&1\\
% 1&1&10
% \end{array}} \right].\]
% That is, a researcher observed state 1 persist for 10 time steps, state 1 transition to state 2 once, state 1 transition to state 3 once, and so on.
% Suppose that \[y_t = \left[ {\begin{array}{*{20}{c}}
% y_{1t}&y_{2t}
% \end{array}} \right]^\prime\] is a VAR model in each state:
% \[{y_t} = \left\{ {\begin{array}{*{20}{l}}
% {\left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right] + {\varepsilon _{1t}}}&{;{s_t} = 1}\\
% {\left[ {\begin{array}{*{20}{c}}
% 2\\
% { - 2}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% {0.5}&{0.1}\\
% {0.5}&{0.5}
% \end{array}} \right]{y_{t - 1}} + {\varepsilon _{2t}}}&{;{s_t} = 2}\\
% {\left[ {\begin{array}{*{20}{c}}
% 3\\
% { - 3}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% {0.25}&0\\
% 0&0
% \end{array}} \right]{y_{t - 1}} + \left[ {\begin{array}{*{20}{c}}
% 0&0\\
% {0.25}&0
% \end{array}} \right]{y_{t - 2}} + {\varepsilon _{3t}}}&{;{s_t} = 3},
% \end{array}} \right.\]
% where, for j = 1,2,3, \varepsilon_{\textrm{jt}} is Gaussian with mean 0 and covariance matrix
% \[\Sigma_j=j\left[ {\begin{array}{*{20}{c}}
% 1&{ - 0.1}\\
% { - 0.1}&1
% \end{array}} \right].\]
%
%% Describe Switching Mechanism
% Create a three-state discrete-time Markov chain model that describes the regime switching mechanism by passing P to mc.

P = [10 1 1; 1 10 1; 1 1 10];
mc = dtmc(P)
mc.P

% mc is a dtmc object.  dtmc normalizes P so that each row sums to 1 (each row is a valid conditional distribution).
%
%% Describe State-Specific Dynamic Regression Submodels
% For each regime, create a VAR model for y_t by using varm. Specify all parameter values.

% Constants (numSeries x 1 vectors)
C1 = [1;-1];
C2 = [2;-2];
C3 = [3;-3];

% Autoregression coefficients (numSeries x numSeries matrices)
AR1 = {};                            % 0 lags
AR2 = {[0.5 0.1; 0.5 0.5]};          % 1 lag
AR3 = {[0.25 0; 0 0] [0 0; 0.25 0]}; % 2 lags

% Innovations covariances (numSeries x numSeries matrices)
Sigma1 = [1 -0.1; -0.1 1];
Sigma2 = [2 -0.2; -0.2 2];
Sigma3 = [3 -0.3; -0.3 3];

% VAR Submodels
mdl1 = varm('Constant',C1,'AR',AR1,'Covariance',Sigma1);
mdl2 = varm('Constant',C2,'AR',AR2,'Covariance',Sigma2);
mdl3 = varm('Constant',C3,'AR',AR3,'Covariance',Sigma3);

% mdl1 through mdl3 are fully specified varm objects.
% Store the submodels in a vector with order corresponding to the regimes in mc.StateNames.

mdl = [mdl1; mdl2; mdl3];

%
%% Create Markov-Switching Dynamic Regression Model
% Create the Markov-switching dynamic regression model that describes the dynamic behavior of s_t and y_t.

Mdl = msVAR(mc,mdl)

% Mdl is a fully specified msVAR object representing a multivariate, three-state Markov-switching dynamic regression model.
% The state-specific submodels are fully specified 2-D VAR models, each varying by parameter values.

Mdl.Submodels(1)
Mdl.Submodels(2)
Mdl.Submodels(3)

% Copyright 2019 The MathWorks, Inc.
%
%