## Create Multivariate Markov-Switching Dynamic Regression Models

These examples show how to create fully and partially specified, multivariate Markov-switching dynamic regression models by using the `msVAR` function. For an overview, see Creating Markov-Switching Dynamic Regression Models.

If you plan to fit a model to data, you must create a partially and fully specified model. The partially specified model contains unknown parameter values to be estimated, and the fully specified model contains parameter values that the `estimate` function uses to initiate the expectation-maximization algorithm.

If you do not plan to fit a model to data by using `estimate`, you must create a fully specified model for all other `msVAR` object functions.

### Create Fully Specified Multivariate Model

This example shows how to create a fully specified, three-state Markov-switching dynamic regression model.

Consider the response processes ${\mathit{y}}_{1\mathit{t}}$ and ${\mathit{y}}_{2\mathit{t}}$ that switch between three states, governed by the latent process ${\mathit{s}}_{\mathit{t}}$ with this observed transition matrix:

`$P=\left[\begin{array}{ccc}10& 1& 1\\ 1& 10& 1\\ 1& 1& 10\end{array}\right].$`

That is, a researcher observed state 1 persist for 10 time steps, state 1 transition to state 2 once, state 1 transition to state 3 once, and so on.

Suppose that ${y}_{t}={\left[\begin{array}{cc}{y}_{1t}& {y}_{2t}\end{array}\right]}^{\prime }$ is a VAR model in each state:

`${y}_{t}=\left\{\begin{array}{ll}\left[\begin{array}{c}1\\ -1\end{array}\right]+{\epsilon }_{1t}& ;{s}_{t}=1\\ \left[\begin{array}{c}2\\ -2\end{array}\right]+\left[\begin{array}{cc}0.5& 0.1\\ 0.5& 0.5\end{array}\right]{y}_{t-1}+{\epsilon }_{2t}& ;{s}_{t}=2\\ \left[\begin{array}{c}3\\ -3\end{array}\right]+\left[\begin{array}{cc}0.25& 0\\ 0& 0\end{array}\right]{y}_{t-1}+\left[\begin{array}{cc}0& 0\\ 0.25& 0\end{array}\right]{y}_{t-2}+{\epsilon }_{3t}& ;{s}_{t}=3,\end{array}$`

where, for j = 1,2,3, ${\epsilon }_{\mathrm{jt}}$ is Gaussian with mean 0 and covariance matrix

`${\Sigma }_{j}=j\left[\begin{array}{cc}1& -0.1\\ -0.1& 1\end{array}\right].$`

Describe Switching Mechanism

Create a three-state discrete-time Markov chain model that describes the regime switching mechanism by passing $\mathit{P}$ to `mc`.

```P = [10 1 1; 1 10 1; 1 1 10]; mc = dtmc(P)```
```mc = dtmc with properties: P: [3x3 double] StateNames: ["1" "2" "3"] NumStates: 3 ```
`mc.P`
```ans = 3×3 0.8333 0.0833 0.0833 0.0833 0.8333 0.0833 0.0833 0.0833 0.8333 ```

`mc` is a `dtmc` object. `dtmc` normalizes `P` so that each row sums to 1 (each row is a valid conditional distribution).

Describe State-Specific Dynamic Regression Submodels

For each regime, create a VAR model for ${\mathit{y}}_{\mathit{t}}$ by using `varm`. Specify all parameter values.

```% 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` through `mdl3` are fully specified `varm` objects.

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

`mdl = [mdl1; mdl2; mdl3];`

Create Markov-Switching Dynamic Regression Model

Create the Markov-switching dynamic regression model that describes the dynamic behavior of ${\mathit{s}}_{\mathit{t}}$ and ${\mathit{y}}_{\mathit{t}}$.

`Mdl = msVAR(mc,mdl)`
```Mdl = msVAR with properties: NumStates: 3 NumSeries: 2 StateNames: ["1" "2" "3"] SeriesNames: ["1" "2"] Switch: [1x1 dtmc] Submodels: [3x1 varm] ```

`Mdl` is a fully specified `msVAR` object representing a multivariate, three-state Markov-switching dynamic regression model.

The state-specific submodels are fully specified 2-D VAR models, each varying by parameter values.

`Mdl.Submodels(1)`
```ans = varm with properties: Description: "2-Dimensional VAR(0) Model" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 0 Constant: [1 -1]' AR: {} Trend: [2×1 vector of zeros] Beta: [2×0 matrix] Covariance: [2×2 matrix] ```
`Mdl.Submodels(2)`
```ans = varm with properties: Description: "AR-Stationary 2-Dimensional VAR(1) Model" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 1 Constant: [2 -2]' AR: {2×2 matrix} at lag  Trend: [2×1 vector of zeros] Beta: [2×0 matrix] Covariance: [2×2 matrix] ```
`Mdl.Submodels(3)`
```ans = varm with properties: Description: "AR-Stationary 2-Dimensional VAR(2) Model" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 2 Constant: [3 -3]' AR: {2×2 matrices} at lags [1 2] Trend: [2×1 vector of zeros] Beta: [2×0 matrix] Covariance: [2×2 matrix] ```

### Create Fully Specified Multivariate Model Containing Regression Components

This example shows how to include an unknown regression component in each submodel of the Markov-switching dynamic regression model in Create Fully Specified Multivariate Model.

Consider adjusting the response variables with the exogenous variables ${\mathit{x}}_{1\mathit{t}}$, ${\mathit{x}}_{2\mathit{t}}$, and ${\mathit{x}}_{3\mathit{t}}$ by including a regression component in each submodel as follows:

`${y}_{t}=\left\{\begin{array}{ll}\left[\begin{array}{c}1\\ -1\end{array}\right]+\left[\begin{array}{c}1\\ -1\end{array}\right]{x}_{1t}+{\epsilon }_{1t}& ;{s}_{t}=1\\ \left[\begin{array}{c}2\\ -2\end{array}\right]+\left[\begin{array}{cc}0.5& 0.1\\ 0.5& 0.5\end{array}\right]{y}_{t-1}+\left[\begin{array}{cc}2& 2\\ -2& -2\end{array}\right]\left[\begin{array}{c}{x}_{1t}\\ {x}_{2t}\end{array}\right]+{\epsilon }_{2t}& ;{s}_{t}=2\\ \left[\begin{array}{c}3\\ -3\end{array}\right]+\left[\begin{array}{cc}0.25& 0\\ 0& 0\end{array}\right]{y}_{t-1}+\left[\begin{array}{cc}0& 0\\ 0.25& 0\end{array}\right]{y}_{t-2}+\left[\begin{array}{ccc}3& 3& 3\\ -3& -3& -3\end{array}\right]\left[\begin{array}{c}{x}_{1t}\\ {x}_{2t}\\ {x}_{3t}\end{array}\right]+{\epsilon }_{3t}& ;{s}_{t}=3.\end{array}$`

Describe Switching Mechanism

Create a three-state discrete-time Markov chain model that describes the regime switching mechanism by passing $\mathit{P}$ to `mc`.

```P = [10 1 1; 1 10 1; 1 1 10]; mc = dtmc(P)```
```mc = dtmc with properties: P: [3x3 double] StateNames: ["1" "2" "3"] NumStates: 3 ```
`mc.P`
```ans = 3×3 0.8333 0.0833 0.0833 0.0833 0.8333 0.0833 0.0833 0.0833 0.8333 ```

`mc` is a `dtmc` object.

Describe State-Specific Dynamic Regression Submodels

For each regime, create a VAR model for ${\mathit{y}}_{\mathit{t}}$ by using `varm`. Specify all parameter values.

```% 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 % Regression coefficients (numSeries x numRegressors matrices) Beta1 = [1;-1]; % 1 regressor Beta2 = [2 2;-2 -2]; % 2 regressors Beta3 = [3 3 3;-3 -3 -3]; % 3 regressors % 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,Beta=Beta1,Covariance=Sigma1); mdl2 = varm(Constant=C2,AR=AR2,Beta=Beta2,Covariance=Sigma2); mdl3 = varm(Constant=C3,AR=AR3,Beta=Beta3,Covariance=Sigma3);```

`mdl1` through `mdl3` are fully specified `varm` objects.

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

`mdl = [mdl1; mdl2; mdl3];`

Create Markov-Switching Dynamic Regression Model

Create the Markov-switching dynamic regression model that describes the dynamic behavior of ${\mathit{s}}_{\mathit{t}}$ and ${\mathit{y}}_{\mathit{t}}$.

`Mdl = msVAR(mc,mdl)`
```Mdl = msVAR with properties: NumStates: 3 NumSeries: 2 StateNames: ["1" "2" "3"] SeriesNames: ["1" "2"] Switch: [1x1 dtmc] Submodels: [3x1 varm] ```

`Mdl` is a fully specified `msVAR` object representing a multivariate, three-state Markov-switching dynamic regression model.

The state-specific submodels are fully specified 2-D VAR models, each varying by AR order and regression coefficient size.

`Mdl.Submodels(1)`
```ans = varm with properties: Description: "2-Dimensional VARX(0) Model with 1 Predictor" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 0 Constant: [1 -1]' AR: {} Trend: [2×1 vector of zeros] Beta: [2×1 matrix] Covariance: [2×2 matrix] ```
`Mdl.Submodels(2)`
```ans = varm with properties: Description: "AR-Stationary 2-Dimensional VARX(1) Model with 2 Predictors" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 1 Constant: [2 -2]' AR: {2×2 matrix} at lag  Trend: [2×1 vector of zeros] Beta: [2×2 matrix] Covariance: [2×2 matrix] ```
`Mdl.Submodels(3)`
```ans = varm with properties: Description: "AR-Stationary 2-Dimensional VARX(2) Model with 3 Predictors" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 2 Constant: [3 -3]' AR: {2×2 matrices} at lags [1 2] Trend: [2×1 vector of zeros] Beta: [2×3 matrix] Covariance: [2×2 matrix] ```

### 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}{cc}{y}_{1t}& {y}_{2t}\end{array}\right]}^{\prime }$ that switches between three states, governed by the latent process ${\mathit{s}}_{\mathit{t}}$ with an unknown transition matrix. Suppose ${\mathit{x}}_{1\mathit{t}}$, ${\mathit{x}}_{2\mathit{t}}$, and ${\mathit{x}}_{3\mathit{t}}$ are exogenous variables of interest.

Suppose that during:

• State 1, ${\mathit{y}}_{\mathit{t}}$ is a VAR(0) model containing a regression component for ${\mathit{x}}_{1\mathit{t}}$

• State 2, ${\mathit{y}}_{\mathit{t}}$ is a VAR(1) model containing a regression component for ${\mathit{x}}_{1\mathit{t}}$ and ${\mathit{x}}_{2\mathit{t}}$

• State 3, ${\mathit{y}}_{\mathit{t}}$ is a VAR(2) model containing a regression component for ${\mathit{x}}_{1\mathit{t}}$, ${\mathit{x}}_{2\mathit{t}}$, and ${\mathit{x}}_{3\mathit{t}}$

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 `NaN`s, and pass it to `dtmc` to create the chain. Label the states.

```P = NaN(3); mc = dtmc(P); mc.P```
```ans = 3×3 NaN NaN NaN NaN NaN NaN NaN NaN NaN ```

`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 `NaN` values 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 their 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 ${\mathit{s}}_{\mathit{t}}$ and ${\mathit{y}}_{\mathit{t}}$.

`Mdl = msVAR(mc,mdl)`
```Mdl = msVAR with properties: NumStates: 3 NumSeries: 2 StateNames: ["1" "2" "3"] SeriesNames: ["1" "2"] Switch: [1x1 dtmc] Submodels: [3x1 varm] ```

`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)`
```ans = varm with properties: Description: "2-Dimensional VARX(1) Model with 2 Predictors" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 1 Constant: [2×1 vector of NaNs] AR: {2×2 matrix of NaNs} at lag  Trend: [2×1 vector of zeros] Beta: [2×2 matrix of NaNs] Covariance: [2×2 matrix of NaNs] ```
`Mdl.Submodels(2)`
```ans = varm with properties: Description: "2-Dimensional VARX(1) Model with 2 Predictors" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 1 Constant: [2×1 vector of NaNs] AR: {2×2 matrix of NaNs} at lag  Trend: [2×1 vector of zeros] Beta: [2×2 matrix of NaNs] Covariance: [2×2 matrix of NaNs] ```
`Mdl.Submodels(3)`
```ans = varm with properties: Description: "2-Dimensional VARX(2) Model with 3 Predictors" SeriesNames: "Y1" "Y2" NumSeries: 2 P: 2 Constant: [2×1 vector of NaNs] AR: {2×2 matrices of NaNs} at lags [1 2] Trend: [2×1 vector of zeros] Beta: [2×3 matrix of NaNs] Covariance: [2×2 matrix of NaNs] ```