Page 1 of 3

### Markov switching model

Posted: Tue Sep 30, 2008 2:04 pm
The reference guide mentions that the Kalman Filter function can be used to estimate markov switching models, MSM (pg 383 in User Guide II, Eviews 6). However, I do not know how to set up the state variables as discrete probabilities as would be needed to define the state-space model for a MSM. Does anyone have some simple sample code that shows how this is done? Or if there is some easier way to estimate these models, that would work too. Thanks much.

### Re: Markov switching model

Posted: Wed Oct 08, 2008 2:10 am
I would also be interested by applying Markov switching model on Eviews.

I would appreciate if anyone would have some inputs.

### Re: Markov switching model

Posted: Fri Nov 21, 2008 10:07 pm
I am interested in this topic too. Does anyone know how this can be done?

### Re: Markov switching model

Posted: Fri Oct 23, 2009 2:17 pm
Did you ever find a way to estimate Markov Switching models in E-Views?

### Re: Markov switching model

Posted: Sat Oct 24, 2009 6:54 am
It seems there is a growing interest in Markov Switching (MS) analysis in EViews (again). I truly understand the importance of this type of analysis and do support the implementation of it in EViews. However, it is not an easy task to develop a general purpose MS analysis for users. I think the new features of EViews 7 might ease this burden somewhat and may provide several useful tools to build more complex regime switching models.

I just scratched a few lines of code just to give an idea how a Markov Switching analysis can be carried out in EViews. This is a well-known Hamiltonian-type MS model with 2 regimes and one lag (i.e. MS(2)-AR(1)). Please feel free to improve and optimize the code further. A more generic version of the code may take quite sometime...

Code: Select all

`'Number of regimes!nr = 2'Number of lags!nl = 1'Number of states!ns = !nr^(!nl+1)'Fixed transition probabilities!p11 = 0.85!p22 = 0.70'Transition probabilities matrixmatrix(!ns,!ns) transptransp(1,1) = !p11transp(1,2) = 0transp(1,3) = !p11transp(1,4) = 0transp(2,1) = 1-!p11transp(2,2) = 0transp(2,3) = 1-!p11transp(2,4) = 0transp(3,1) = 0transp(3,2) = 1-!p22transp(3,3) = 0transp(3,4) = 1-!p22transp(4,1) = 0transp(4,2) = !p22transp(4,3) = 0transp(4,4) = !p22'Ergodic probabilities!tol = 1000matrix erg = transpwhile !tol>1e-06  erg = erg*transp  !tol = @abs(erg(1,1) - erg(1,2))wend'Coefficientscoef(!nr) mucoef(!nl) betacoef(!nr) pcoef(1) sigma'Initialization of coefficent vectorsmu(1) =  @quantile(y,0.9)mu(2) = @quantile(y,0.1)equation ols.ls y c y(-1)beta(1) = ols.@coef(2)sigma(1) = ols.@sep(1) = !p11p(2) = !p22smpl @first @firstseries prob11 = erg(1,1)series prob12 = erg(2,1)series prob21 = erg(3,1)series prob22 = erg(4,1)smpl @allseries dcptotal = 0smpl @first + !nl @last'Set up the log-likelihood objectlogl markovmarkov.append @logl logl'Residualsmarkov.append res11 = (y - mu(1))-beta(1)*(y(-1) - mu(1))markov.append res12 = (y - mu(1))-beta(1)*(y(-1) - mu(2))markov.append res21 = (y - mu(2))-beta(1)*(y(-1) - mu(1))markov.append res22 = (y - mu(2))-beta(1)*(y(-1) - mu(2))'Density functionsmarkov.append dens11 = @dnorm(res11/sigma(1))/sigma(1)markov.append dens12 = @dnorm(res12/sigma(1))/sigma(1)markov.append dens21 = @dnorm(res21/sigma(1))/sigma(1)markov.append dens22 = @dnorm(res22/sigma(1))/sigma(1)'Conditional probabilities of being in State i given State j (i,j = 1,2)markov.append condprob11 = p(1)*(prob11(-1) + prob12(-1))markov.append condprob12 = (1-p(2))*(prob21(-1) + prob22(-1))markov.append condprob21 = (1-p(1))*(prob11(-1) + prob12(-1))markov.append condprob22 = p(2)*(prob21(-1) + prob22(-1))markov.append denscondprob11 = dens11*condprob11markov.append denscondprob12 = dens12*condprob12markov.append denscondprob21 = dens21*condprob21markov.append denscondprob22 = dens22*condprob22markov.append dcptotal = denscondprob11 + denscondprob12 + denscondprob21 + denscondprob22'Probabilities of being in State i given State j (i,j = 1,2)markov.append prob11 = denscondprob11/dcptotalmarkov.append prob12 = denscondprob12/dcptotalmarkov.append prob21 = denscondprob21/dcptotalmarkov.append prob22 = denscondprob22/dcptotalmarkov.append logl = log(dcptotal)smpl @first + !nl @lastmarkov.ml(showopts, m=500, c=1e-6)show markov.output'Probability of being in state i (i=1,2)series pr1 = prob11 + prob12series pr2 = prob21 + prob22area pr1 pr2smpl @all`

### Re: Markov switching model

Posted: Thu Apr 07, 2011 11:59 am

I would like to use your MS(2)AR(1) to do the forecasting. However, when I retrieve the model and solve for it, I only get the fitted series for the residuals, the density function, the conditional probability.

Could trubador or someone please to show me how to compute the fitted values of y?

Thanks a lot

### Re: Markov switching model

Posted: Mon May 02, 2011 4:10 pm
It is very simple. In fact you already have the residuals. Anyway, since you have coefficients for each state, compute the fitted value for each:

series fity1 = mu(1))+beta(1)*(y(-1) - mu(1))
series fity2 = mu(2))+beta(2)*(y(-1) - mu(2))

Then the fitted value is:

series fity=pr1*fity1+pr2*fity2

Now because of the MS model structure, you shall obtained a fitted value very close to the actual value of "y".

I hope this helps.

Abe

### Re: Markov switching model

Posted: Tue May 03, 2011 1:59 am
Actually, there is a typo in the reply by Abraham Vela. The correct formula for the fitted values is:

series fity1 = mu(1)+beta(1)*(y(-1) - mu(1))
series fity2 = mu(2)+beta(1)*(y(-1) - mu(2))

Regards
Donihue

### Re: Markov switching model

Posted: Thu May 05, 2011 9:19 am
Dear Donihue:

You are correct. I made a mistake in the signs. (Jesus, maybe I need glasses!). I have already corrected them in my former post.

Now, you refer only to the intermediate steps needed to arrive to the actual (final, aggregate) fitted value. It shall not be overlooked.

I have graphed all fity1 fity2 and fity. The later is just a linear combination of the former two. Certainly, one cannot be at both states at the same time, but there is a probability of being on each. For instance, if your results were that the probability of staying in state 1 is unity and the probability of being in state 2 is 0 all the time, then only fity1 would suffice. Normally this is not the case and the transition matrix is usually not diagonal. There is two probabilities of "switching among states", which is the main issue in Markov analysis. We are dealing with risk or, most accurately, uncertainty.

Regards,
Abe

### Re: Markov switching model

Posted: Sat May 14, 2011 5:50 pm
Thank you so much for your help.

I am much appreciated

### Re: Markov switching model

Posted: Sat May 14, 2011 5:56 pm
Can someone explain to me why the size of the transition matrix is (!ns,!ns) where ns > 2

I think the dimension of the transition matrix should be equal to the number of states which is 2 in this case.

if !ns = !nr^(!nl+1), then how to fill in the matrix if the number of lag is more than 1?

Thank you in advance for explanation.

### Re: Markov switching model

Posted: Sun May 15, 2011 11:51 pm
You are missing the point that the code is specifically written to perform Hamiltonian-type MS model with 2 regimes and one lag (i.e. MS(2)-AR(1)) and therefore cannot be used for general purposes unless properly modified. That said, the size of transition matrix explodes mainly due to autoregressive lags, which complicates the relationship among the regimes.

### Re: Markov switching model

Posted: Mon May 23, 2011 2:16 pm
Hello world,
Just to keep the subject going. I have one question about the fitted and actual values for a dependent variable (say y) . I have found that the fitted y fits PERFECTLY the actual y. Using alternative starting values (maximum likelihood, grid search,ect...), the fit is still perfect. I suspect then a big mistake I cannot figure out. Is it possible that the fitted y matches perfectly the actual one?

In the Vela's post, he writes:

" series fity1 = mu(1))+beta(1)*(y(-1) - mu(1))
series fity2 = mu(2))+beta(2)*(y(-1) - mu(2))

Then the fitted value is:

series fity=pr1*fity1+pr2*fity2

Now because of the MS model structure, you shall obtained a fitted value VERY CLOSE to the actual value of "y". Note that he says "VERY CLOSE", not "EQUAL".

That raises my question.

Thanks,
F.

### Re: Markov switching model

Posted: Fri May 27, 2011 8:27 am

Aqua,

Could you please to post your workfile? Mine is close, not perfect.
Are you working with simulated data or real data?

Thank you.

### Re: Markov switching model

Posted: Thu Oct 20, 2011 3:16 pm
Hello
thank you all for sharing.
how can I estimate Std. Error, z-Statistic, and Prob for parameters?
thanks