%% Create Fully Specified Multivariate Model
% Create the following two, three-state threshold-switching dynamic regression model for a 2-D response process:
% A model with state-specific innovations distributions
% A model with a model-wide innovation covariance
% Specify all parameter values (this example uses arbitrary values).
%% Create Threshold Transitions
% 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)

% tt is a fully specified threshold object.
%% Specify State-Specific Innovations Covariance Matrices
% Assume the following VAR models describe the response processes of the system:
% State 1: {y_t} = \left[ {\begin{array}{*{20}{c}}
% 1\\
% { - 1}
% \end{array}} \right] + {\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}}
% 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}}
% 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).

% 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, mdl2, and mdl3 are fully specified varm objects.
% Store the submodels in a vector with order corresponding to the regimes in tt.StateNames.

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 is a fully specified tsVAR object representing a multivariate three-state LSTAR model. Because the Covariance property is empty ([]), the submodels have their own innovations covariance matrix.
%% Specify Model-Wide Innovations Covariance Matrix
% Suppose that the innovations covariance matrix is invariant across states, and has value \left[ {\begin{array}{*{20}{c}}
% 2&{ - 0.2}\\
% { - 0.2}&2
% \end{array}} \right].
% Create an LSTAR model like Mdl that has the model-wide innovations covariance matrix.

MdlCov = tsVAR(tt,mdl,SeriesNames=["Y1" "Y2"],Covariance=Sigma2)
MdlCov.Covariance

%  The Covariance property of MdlCov is nonempty, which means the innovations distribution of all submodels are equal.
% Copyright 2021 The MathWorks, Inc.