%% Create Partially Specified Univariate Model for Estimation
% This example shows how to create a Markov-switching dynamic regression model containing unknown parameter values to be fit to data.
% Suppose that an economy switches between two regimes, an expansion and a recession, and suppose that the transition probabilities are unknown.
% Suppose that y_t is a univariate response process representing an economic measurement that can suggest which state the economy experiences during a period. During an expansion, y_t is this AR(2) model, and, during a recession, y_t is an AR(1) model. State-specific submodel coefficients and innovations variance are unknown.
%
%% Describe Switching Mechanism
% A discrete-time Markov chain represents the switching mechanism, and a right stochastic matrix describes the chain.  Because the transition probabilities are unknown, create a matrix of NaNs, and pass it to dtmc to create the chain. Label the states.

P = NaN(2);
mc = dtmc(P,'StateNames',["Expansion" "Recession"])
mc.P

% mc is a partially specified dtmc object. The transition matrix mc.P is completely unknown and estimable.
%
%% Describe State-Specific Dynamic Regression Submodels
% The shorthand syntax of arima is well suited for the quick creation of AR model templates for estimation. That is, given the AR model order, all other parameters in the model are unknown and estimable.
% For each regime, create an AR model template by specifying the AR polynomial order, and zero-order differencing and moving average polynomials. Describe each model by using dot notation.

mdl1 = arima(1,0,0);
mdl1.Description = "Expansion State"
mdl2 = arima(2,0,0);
mdl2.Description = "Recession State"

% mdl1 and mdl2 are partially specified arima objects. NaN-valued properties correspond to unknown, estimable parameters.
% Store the submodels in a vector with order corresponding to the regimes in mc.StateNames.

mdl = [mdl1; mdl2];

%
%% Create Markov-Switching Dynamic Regression Model
% Create the Markov-switching dynamic regression model that describes the dynamic behavior of the economy with respect to y_t.

Mdl = msVAR(mc,mdl)

% Mdl is a partially specified msVAR object representing a univariate two-state Markov-switching dynamic regression model.

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

% msVAR converts the arima object submodels to 1-D varm object equivalents.
% Mdl is prepared for estimation; pass it, a fully specified model containing initial values for optimization, and data to estimate.
% Copyright 2019 The MathWorks, Inc.