%% Create Fully Specified Model Containing Regression Component
% Consider including regression components for exogenous variables in each submodel of the threshold-switching dynamic regression model in docid:econ_ug#mw_70ce5798-44a9-42ef-82c6-6af648bd6494.
% Create logistic threshold transitions at mid-levels 2 and 8, with rates 3.5 and 1.5 respectively. Label the corresponding states "Low", "Med", and "Hi".

t = [2 8];
r = [3.5 1.5];
stateNames = ["Low" "Med" "High"];
tt = threshold(t,Type="logistic",Rates=[3.5 1.5],StateNames=stateNames)

% Assume the following VARX models describe the response processes of the system:
% State 1: {y_t} = \left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right] +
% \left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right]x_{1,t} +
% {\varepsilon _{1,t}}, where {\varepsilon _{1,t}}\sim{\rm N}\left( {\left[ {\begin{array}{*{20}{c}}
% 0\\
% 0
% \end{array}} \right],\left[ {\begin{array}{*{20}{c}}
% 1&{ - 0.1}\\
% { - 0.1}&1
% \end{array}} \right]} \right).
% State 2: {y_t} = \left[ {\begin{array}{*{20}{c}}
% 2\\
% { -2}
% \end{array}} \right]
% +
% \left[ {\begin{array}{*{20}{c}}
% 2 & 2\\
% { - 2}&-2
% \end{array}} \right]x_{2,t}
% + \left[ {\begin{array}{*{20}{c}}
% 0.5 & 0.1\\
% 0.5 & 0.5
% \end{array}} \right] y_{t-1}+ {\varepsilon _{2,t}}, where {\varepsilon _{2,t}}\sim{\rm N}\left( {\left[ {\begin{array}{*{20}{c}}
% 0\\
% 0
% \end{array}} \right],\left[ {\begin{array}{*{20}{c}}
% 2&{ - 0.2}\\
% { - 0.2}&2
% \end{array}} \right]} \right).
% State 3: {y_t} = \left[ {\begin{array}{*{20}{c}}
% 3\\
% { -3}
% \end{array}} \right]
% +
% \left[ {\begin{array}{*{20}{c}}
% 3 & 3 & 3\\
% { - 3}&-3&-3
% \end{array}} \right]x_{3,t}
% + \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 _{3,t}}, where {\varepsilon _{3,t}}\sim{\rm N}\left( {\left[ {\begin{array}{*{20}{c}}
% 0\\
% 0
% \end{array}} \right],\left[ {\begin{array}{*{20}{c}}
% 3&{ - 0.3}\\
% { - 0.3}&3
% \end{array}} \right]} \right).
% x_{1,t} represents a single exogenous variable, x_{2,t} represents two exogenous variables, and x_{3,t} represents three exogenous variables. Store the submodels in a vector.

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

% 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

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

% 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];

%VARX 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);

mdl = [mdl1; mdl2; mdl3];

% Create an LSTAR model from the switching mechanism tt and the state-specific submodels mdl. Label the series Y1 and Y2.

Mdl = tsVAR(tt,mdl,SeriesNames=["Y1" "Y2"])
Mdl.Submodels(1)
Mdl.Submodels(2)
Mdl.Submodels(3)

%
% Copyright 2020 The MathWorks, Inc.