%% Create Partially Specified Multivariate Model Containing Regression Components For Estimation
% This example shows how to create a partially specified, three-state Markov-switching dynamic regression model to be fit to data.
% Consider the 2-D response process \[y_t = \left[ {\begin{array}{*{20}{c}}
% y_{1t}&y_{2t}
% \end{array}} \right]^\prime\] that switches between 3 states, governed by the latent process s_t with an unknown transition matrix. Suppose  x_{1t}, x_{2t}, and x_{3t} are exogenous variables of interest.
% Suppose that during:
% State 1, y_t is a VAR(0) model containing a regression component for x_{1t}
% State 2, y_t is a VAR(1) model containing a regression component for x_{1t} and x_{2t}
% State 3, y_t is a VAR(2) model containing a regression component for x_{1t}, x_{2t}, and x_{3t}
%
%% 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(3);
mc = dtmc(P);
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 varm is well suited for the quick creation of VAR model templates for estimation. That is, given the response  series dimensionality and VAR model order, all other parameters in the model are unknown and estimable.
% For each regime, create a VAR model template by specifying the response series dimensionality and AR polynomial order.

numSeries = 2;
mdl1 = varm(numSeries,0);
mdl2 = varm(numSeries,1);
mdl3 = varm(numSeries,2);

% mdl1, mdl2, and mdl3 are partially specified varm objects. By default, the Beta property is empty, which means the models do not contain a regression component. To include regression components for estimation, for each model, set Beta to a numSeries-by-numRegressors matrix of NaNs by using dot notation.

mdl1.Beta = NaN(numSeries,1);
mdl2.Beta = NaN(numSeries,2);
mdl3.Beta = NaN(numSeries,3);

% Store the submodels in a vector with order corresponding to the regimes in mc.StateNames.

mdl = [mdl2; mdl2; mdl3];

%
%% Create Markov-Switching Dynamic Regression Model
% Create the Markov-switching dynamic regression model that describes the dynamic behavior of s_t and y_t.

Mdl = msVAR(mc,mdl)

% Mdl is a partially specified msVAR object representing a multivariate, three-state Markov-switching dynamic regression model. To estimate the unknown parameter values of Mdl, pass Mdl, response and predictor data, and a fully specified Markov-switching model (which has the same structure as Mdl, but contains initial values for estimation) to estimate.
% The state-specific submodels are partially specified 2-D VAR models, each varying by AR order and regression coefficient size.

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

% Copyright 2019 The MathWorks, Inc.
%
%