## Create Univariate Markov-Switching Dynamic Regression Models

These examples show how to create fully and partially specified, univariate 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 Univariate Model

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

Suppose that an economy switches between two regimes: an expansion and a recession. If the economy is in an expansion, the probability that the expansion persists in the next time step is 0.9, and the probability that it switches to a recession is 0.1. If the economy is in a recession, the probability that the recession persists in the next time step is 0.7, and the probability that it switches to an expansion is 0.3.

Suppose that ${\mathit{y}}_{\mathit{t}}$ is a univariate response process representing an economic measurement that can suggest which state the economy experiences during a period. During an expansion, ${\mathit{y}}_{\mathit{t}}$ is this AR(2) model:

`${y}_{t}=5+0.3{y}_{t-1}+0.2{y}_{t-2}+{\epsilon }_{1t},$`

where ${\epsilon }_{1\mathit{t}}$ is an iid Gaussian process with mean 0 and variance 2. During a recession, ${\mathit{y}}_{\mathit{t}}$ is this AR(1) model:

`${y}_{t}=-5+0.1{y}_{t-1}+{\epsilon }_{2t},$`

where ${\epsilon }_{2\mathit{t}}$ is an iid Gaussian process with mean 0 and variance 1.

Describe Switching Mechanism

The right-stochastic transition matrix describing the regime switching probabilities is

`$P=\left[\begin{array}{cc}0.9& 0.1\\ 0.3& 0.7\end{array}\right].$`

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

```P = [0.9 0.1; 0.3 0.7]; mc = dtmc(P,StateNames=["Expansion" "Recession"])```
```mc = dtmc with properties: P: [2x2 double] StateNames: ["Expansion" "Recession"] NumStates: 2 ```

`mc` is a `dtmc` object.

Describe State-Specific Dynamic Regression Submodels

For each regime, create an AR model for ${\mathit{y}}_{\mathit{t}}$ by using `arima`. Specify all parameter values, and enter a description for the models.

```% Constants C1 = 5; C2 = -5; % AR coefficients AR1 = [0.3 0.2]; % 2 lags AR2 = 0.1; % 1 lags % Innovations variances v1 = 2; v2 = 1; % Submodels: mdl1 = arima(Constant=C1,AR=AR1,Variance=v1, ... Description="Expansion State")```
```mdl1 = arima with properties: Description: "Expansion State" SeriesName: "Y" Distribution: Name = "Gaussian" P: 2 D: 0 Q: 0 Constant: 5 AR: {0.3 0.2} at lags [1 2] SAR: {} MA: {} SMA: {} Seasonality: 0 Beta: [1×0] Variance: 2 ARIMA(2,0,0) Model (Gaussian Distribution) ```
```mdl2 = arima(Constant=C2,AR=AR2,Variance=v2, ... Description="Recession State")```
```mdl2 = arima with properties: Description: "Recession State" SeriesName: "Y" Distribution: Name = "Gaussian" P: 1 D: 0 Q: 0 Constant: -5 AR: {0.1} at lag  SAR: {} MA: {} SMA: {} Seasonality: 0 Beta: [1×0] Variance: 1 ARIMA(1,0,0) Model (Gaussian Distribution) ```

`mdl1` and `mdl2` are fully specified `arima` objects.

Store the submodels in a vector with their 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 ${\mathit{y}}_{\mathit{t}}$.

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

`Mdl` is a fully specified `msVAR` object representing a univariate two-state Markov-switching dynamic regression model. You can pass `Mdl` to any `msVAR` object function for further analysis, or initialize the estimation procedure using the parameter values of `Mdl`.

`Mdl.Submodels(1)`
```ans = varm with properties: Description: "AR-Stationary 1-Dimensional VAR(2) Model" SeriesNames: "Y1" NumSeries: 1 P: 2 Constant: 5 AR: {0.3 0.2} at lags [1 2] Trend: 0 Beta: [1×0 matrix] Covariance: 2 ```
`Mdl.Submodels(2)`
```ans = varm with properties: Description: "AR-Stationary 1-Dimensional VAR(1) Model" SeriesNames: "Y1" NumSeries: 1 P: 1 Constant: -5 AR: {0.1} at lag  Trend: 0 Beta: [1×0 matrix] Covariance: 1 ```

`msVAR` converts the `arima` object submodels to 1-D `varm` object equivalents.

### 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.

Also suppose that ${\mathit{y}}_{\mathit{t}}$ is a univariate response process representing an economic measurement that can suggest which state the economy experiences during a period. During an expansion, ${\mathit{y}}_{\mathit{t}}$ is this AR(2) model. During a recession, ${\mathit{y}}_{\mathit{t}}$ is an AR(1) model. State-specific submodel coefficients and innovations variances 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 `NaN`s, and pass it to `dtmc` to create the chain. Label the states.

```P = NaN(2); mc = dtmc(P,StateNames=["Expansion" "Recession"])```
```mc = dtmc with properties: P: [2x2 double] StateNames: ["Expansion" "Recession"] NumStates: 2 ```
`mc.P`
```ans = 2×2 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 `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 as well as zero-order differencing and moving average polynomials. Describe each model by using dot notation.

```mdl1 = arima(1,0,0); mdl1.Description = "Expansion State"```
```mdl1 = arima with properties: Description: "Expansion State" SeriesName: "Y" Distribution: Name = "Gaussian" P: 1 D: 0 Q: 0 Constant: NaN AR: {NaN} at lag  SAR: {} MA: {} SMA: {} Seasonality: 0 Beta: [1×0] Variance: NaN ARIMA(1,0,0) Model (Gaussian Distribution) ```
```mdl2 = arima(2,0,0); mdl2.Description = "Recession State"```
```mdl2 = arima with properties: Description: "Recession State" SeriesName: "Y" Distribution: Name = "Gaussian" P: 2 D: 0 Q: 0 Constant: NaN AR: {NaN NaN} at lags [1 2] SAR: {} MA: {} SMA: {} Seasonality: 0 Beta: [1×0] Variance: NaN ARIMA(2,0,0) Model (Gaussian Distribution) ```

`mdl1` and `mdl2` are partially specified `arima` objects. `NaN`-valued properties correspond to unknown, estimable parameters.

Store the submodels in a vector with their 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 ${\mathit{y}}_{\mathit{t}}$.

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

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

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

`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`.

### Create Partially Specified Univariate 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 Partially Specified Univariate Model for Estimation.

Consider adjusting the response variable with exogenous variables ${\mathit{x}}_{1\mathit{t}}$ and ${\mathit{x}}_{2\mathit{t}}$ by including a regression component in each submodel.

Create a discrete-time Markov chain representing the switching mechanism.

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

Create the ARX(1) and ARX(2) submodels by using the longhand syntax of `arima`. For each model, supply a 2-by-1 vector of `NaN`s to the `Beta` name-value argument. This setting specifies the size of the regression component (that is, which predictors are included) and that it is estimable. Store the submodels in a vector.

```mdl1 = arima(ARLags=1,Beta=NaN(2,1),Description="Expansion State"); mdl2 = arima(ARLags=1:2,Beta=NaN(2,1),Description="Recession State"); mdl = [mdl1; mdl2];```

Create a Markov-switching dynamic regression model.

```Mdl = msVAR(mc,mdl); Mdl.Submodels(1)```
```ans = varm with properties: Description: "1-Dimensional VARX(1) Model with 2 Predictors" SeriesNames: "Y1" NumSeries: 1 P: 1 Constant: NaN AR: {NaN} at lag  Trend: 0 Beta: [NaN NaN] Covariance: NaN ```
`Mdl.Submodels(2)`
```ans = varm with properties: Description: "1-Dimensional VARX(2) Model with 2 Predictors" SeriesNames: "Y1" NumSeries: 1 P: 2 Constant: NaN AR: {NaN NaN} at lags [1 2] Trend: 0 Beta: [NaN NaN] Covariance: NaN ```