%% Create Fully Specified Multivariate Model Containing Regression Components
% This example shows how to include an unknown regression component in each submodel of the Markov-switching dynamic regression model in docid:econ_ug.mw_acc27d90-3ad5-46e5-a47b-406d5df15972.
% Consider adjusting the response variables with the exogenous variables x_{1t}, x_{2t}, and x_{3t} by including a regression component in each submodel as follows:
% \[{y_t} = \left\{ {\begin{array}{*{20}{l}}
% {\left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right] + \left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right]{x_{1t}} + {\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}} + \left[ {\begin{array}{*{20}{c}}
% 2&2\\
% { - 2}&{ - 2}
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {{x_{1t}}}\\
% {{x_{2t}}}
% \end{array}} \right] + {\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}} + \left[ {\begin{array}{*{20}{c}}
% 3&3&3\\
% { - 3}&{ - 3}&{ - 3}
% \end{array}} \right]\left[ {\begin{array}{*{20}{c}}
% {{x_{1t}}}\\
% {{x_{2t}}}\\
% {{x_{3t}}}
% \end{array}} \right] + {\varepsilon _{3t}}}&{;{s_t} = 3}.
% \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.
%
%% 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
    
% Regression coefficients (numSeries x numRegressors matrices)
Beta1 = [1;-1];           % 1 regressor
Beta2 = [2 2;-2 -2];      % 2 regressors
Beta3 = [3 3 3;-3 -3 -3]; % 3 regressors
    
% 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,'Beta',Beta1,...
    'Covariance',Sigma1);
mdl2 = varm('Constant',C2,'AR',AR2,'Beta',Beta2,...
    'Covariance',Sigma2);
mdl3 = varm('Constant',C3,'AR',AR3,'Beta',Beta3,...
    '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 AR order and regression coefficient size.

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

% Copyright 2019 The MathWorks, Inc.
%