%% Prepare Switching Mechanism of Threshold-Switching Model for Estimation
% The two main components of a threshold-switching dynamic regression (tsVAR) model are:
% Threshold transitions, which represent the switching mechanism between states. Mid-levels and transition rates are estimable.
% A collection of autoregressive models describing the dynamic system among states. Submodel coefficients and covariances are estimable.
% Before you create a threshold-switching model, you must specify its threshold transitions by using theshold. If you plan to fit a threshold-switching model to data, you can fully specify its threshold transitions if you know all mid-levels and transition rates. If you need to estimate some or all mid-levels and rates, you can enter NaN values as placeholders for unknown parameters. estimate treats all specified parameters as equality constraints during estimation. Regardless, to fit threshold transition parameters to data, you must specify a partially specified threshold object as the switching mechanism of a threshold-switching model.
%
%% Prepare All Estimable Parameters for Estimation
% Consider a smooth transition autoregressive model (STAR) that switches between three states (two thresholds) with an exponential transition function.
% Create the switching mechanism. Specify that all estimable parameters are unknown.

t1 = [NaN NaN]; % Two unknown mid-levels
r1 = [NaN NaN]; % Two unknown transition rates
tt1 = threshold(t1,Type="exponential",Rates=r1)

% tt is a partially specified threshold object to pass to tsVAR as the switching mechanism. The estimate function of tsVAR fits the two mid-levels and transition rates to the data with any unknown submodel parameters in the threshold-switching model.
%
%% Specify Equality Constraints
% Consider a smooth transition autoregressive model (STAR), which has a switching mechanism with the following qualities:
% The thresholds are at -1 and 1.
% The transition function is exponential.
% The transition rate between the first and second state is 0.5, but the rate between the second and third states is unknown.
% Create the switching mechanism.

t2 = [-1 1];
r2 = [0.5 NaN];
tt2 = threshold(t2,Type="exponential",Rates=r2)

% tt is a partially specified threshold object to pass to tsVAR as the switching mechanism. The estimate function of tsVAR does the following:
% Treat the mid-levels tt.Levels and the first transition rate tt.Rates(1) as equality constraints
% Fit the second transition rate tt.Rates(2) to the data with any unknown submodel parameters in the threshold-switching model
% Copyright 2021 The MathWorks, Inc.