 # Markov-Switching

## Example: Markov-Switching in TSMT

Suppose we have a dependent variable that is follows an autoregressive process with regime switching.  For a starting example, assume we have switching white noise process such that the constant and error variance all depend on an unobservable Markov Chain: The model requires full estimation, via maximum likelihood, of the parameter vector , where p00 and p11 represent the Markov-Chain transition probabilities. The switchmt function that performs the estimation takes four arguments, (1) a switchmt control structure, (2) a DS data structure, (3) a scalar defining the number of state in the Markov chain, and (4) a scalar defining the number of lags of the dependent variable. GAUSS structures are simply bins containing other objects such as matrices, strings, arrays, etc.  They can be defined by the programmer, but one of the structures used by switchmt is defined in the Run-Time Library, and the control structure is defined in the tsmt library.

### The PV Parameter Structure

The PV parameter structure is created and filled using GAUSS Run-Time Library functions.  Using these functions the structure can be filled with vectors, matrices, and arrays containing starting parameter values.  Masks can be used to specify fixed versus free parameters.  For example,

```b0 = { 3.3, -2.7 };
sig = { 10, 37 };
p = { .8 .2, .2 .8 };

struct PV st0;

st0 = pvPacki(pvCreate, b0, "beta0", 1);
st0 = pvPacki(st0, sig, "sigma", 4);
st0 = pvPacki(st0, p, "p", 5);```
The structure now contains starting values for a 2x1 vector of coefficients called beta0, a 2x1 vector of autoregressive coefficients called phi, a 2x1 vector of variances called sig, and a 4x1 vector of transition probabilities called p. This structure will be passed the switchmt control structure as starting values for the parameters. Note that not all elements of the PV parameter structure must be specified but rather only those corresponding to the estimation model. The specified PV parameter should follow the specifications below:
1. beta0   number of states by 1 vector, constants
2. beta    number of states by K, coefficients on K independent variables
3. phi     number of lags by 1 vector, autoregression coefficients
4. sigma   scalar or number of states by 1 vector, error variances.
5.  p       number states by number states matrix, transition probabilities.

### The DS Data Structure

The DS data structure is a general purpose bucket of GAUSS types.  It contains one of each of the types, matrix, array, string, string array, sparse matrix, and scalar.  It is passed to the log-likelihood procedure untouched by switchmt.  It can be used by programmers in any way they choose to help in computing the log-likelihood.  Typically it is used to pass data to the procedure.  The DS structure can also be reshaped into a vector of structures giving the programmer great flexibility in handling data and other information.  For example,
```load y0[58,3] = exdata.asc;

struct DS d0;

/*Dependent Variable*/
d0.dataMatrix = y0[.,1];```

### The switchmt Control Structure

This structure handles the matrices and strings that control the estimation process such as setting the descent number of relevant states, optimization controls, equality constraints on the transition probabilities, and so on.  Continuing with the example above,
```struct switchmtControl c0;
c0 = switchmtControlCreate();

/*specifies switching variance*/
c0.constVariance = 0;

/*Sets number of relevant lagged states equal to zero*/
c0.relevantStates = 0;

/*sets output printing*/
c0.output = 1;

/*sets Bayesian control parameters (Hamilton, 1991)*/
c0.aBayes = 0.2;
c0.bBayes = 1;
c0.cBayes = 0.1;

/*passes starting parameter estimate values*/
c0.start = st0;```
This appropriately passes to switchmt that the model has switching error variances and does not require lagged states for estimations.  The remaining controls elements set preferences for output display and estimation. Finally, the starting values of the estimate parameters should be passed to the control switchmt structure

### The Command File

Finally we put it all together in the command file:
```/*
**  This example reproduces the results for the French exchange
**  rate in "Long Swings in the Exchange Rate: Are They in the
**  Data and Do Markets Know It?" by Charles Engel and James
**  D. Hamilton American Economic Review, Sept. 1990.
**
*/

library tsmt;

/*Load data and specify PV data structure*/

struct DS d0;
d0=reshape(d0,1,1);
d0.dataMatrix = y0[1:58,1];

/*Define switchmt control structure*/
struct switchmtControl c0;
c0 = switchmtControlCreate();

/*Specifies switching variance*/
c0.constVariance = 0;

/*Sets number of relevant lagged states equal to zero*/
c0.relevantStates = 0;

/*Sets output printing*/
c0.output = 1;

/*Sets Bayesian control parameters (Hamilton, 1991)*/
c0.aBayes = .2;
c0.bBayes = 1;
c0.cBayes = .1;

/*Set parameter estimate starting values*/
b0 = { 3.3, -2.7 };
sig = { 10, 37 };
p = { .8 .2, .2 .8 };

struct PV st0;
st0 = pvPacki(pvCreate,b0,"beta0",1);
st0 = pvPacki(st0,sig,"sigma",4);
st0 = pvPacki(st0,p,"p",5);

c0.start = st0;

struct switchmtOut out0;

/*Call switchmt procedure*/
out0 = switchmt(c0,d0,2,0);

print out0.logl;```
This program produces the following output:
```run C:\gauss12\examples\switch1.e

===============================================

switchmt Version 1.0.4      4/27/2012  10:26 am

===============================================

Beta0 - Constant(s)

3.2961944

-2.7028352

standard errors

0.97714370

1.3642475

==============================

Sigma - Variances

9.9999022

36.999826

standard errors

5.0328476

10.296018

==============================

P - Transition Probabilities

0.81806046      0.093176683

0.18193954       0.90682332

standard errors

0.10870594      0.063674743

0.10870594      0.063674744

-129.26498```

### Have a Specific Question?

Get a real answer from a real person

### Need Support?

Get help from our friendly experts.