%% Create Fully Specified Univariate TAR Model
% Create a two-state TAR model for a 1-D response process. Specify all parameter values (this example uses arbitrary values).
%% Create Switching Mechanism
% Create a discrete threshold transition at level 0. Label the regimes to reflect the state of the economy:
% When the threshold variable (currently unknown) is in \left(-\infty ,0\right), the economy is in a recession.
% When the threshold variable is in \left.\lbrack 0,\infty \right), the economy is expanding.

t = 0;
tt = threshold(t,StateNames=["Recession" "Expansion"])

% tt is a fully specified threshold object that describes the switching mechanism of the threshold-switching model.
%
%% Create State-Specific Models for Response Series
% Assume the following univariate models describe the response process of the system:
% Recession: y_t =-1+{0\ldotp 1y_{t-1} +\varepsilon }_{1,t} , where \varepsilon_{1,t} \sim N\left(0,1\right).
% Expansion: y_t ={-1+0\ldotp 3y_{t-1} +0\ldotp 2y_{t-2} +\varepsilon }_{2,t} , where \varepsilon_{2,t} \sim N\left(0,4\right).
% For each regime, use arima to create an AR model that describes the response process within the regime.

c1 = -1;
c2 = 1;
ar1 = 0.1;       
ar2 = [0.3 0.2]; 
v1 = 1;
v2 = 4;

mdl1 = arima(Constant=c1,AR=ar1,Variance=v1,...
    Description="Recession State Model")
mdl2 = arima(Constant=c2,AR=ar2,Variance=v2,...
    Description="Expansion State Model")

% mdl1 and mdl2 are fully specified arima objects.
% Store the submodels in a vector with order corresponding to the regimes in tt.StateNames.

mdl = [mdl1; mdl2];

%
%% Create Threshold-Switching Model
% Use tsVAR to create a TAR model from the switching mechanism tt and the state-specific submodels mdl.

Mdl = tsVAR(tt,mdl)
Mdl.Submodels(2)

% Mdl is a fully specified tsVAR object representing a univariate two-state TAR model. tsVAR stores specified arima submodels as varm objects.
% Because Mdl is fully specified, you can pass it to any tsVAR object function for further analysis (see Object Functions). Or, you can specify the threshold model parameters in Mdl.Switch as initial values for the estimation procedure (see estimate).
% Copyright 2021 The MathWorks, Inc.