Page 1 of 3

Markov switching model

Posted: Tue Sep 30, 2008 2:04 pm
by jpl
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
by csolbach
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
by megan
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
by tgoodwin
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
by trubador
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 matrix
matrix(!ns,!ns) transp
transp(1,1) = !p11
transp(1,2) = 0
transp(1,3) = !p11
transp(1,4) = 0
transp(2,1) = 1-!p11
transp(2,2) = 0
transp(2,3) = 1-!p11
transp(2,4) = 0
transp(3,1) = 0
transp(3,2) = 1-!p22
transp(3,3) = 0
transp(3,4) = 1-!p22
transp(4,1) = 0
transp(4,2) = !p22
transp(4,3) = 0
transp(4,4) = !p22

'Ergodic probabilities
!tol = 1000
matrix erg = transp
while !tol>1e-06
  erg = erg*transp
  !tol = @abs(erg(1,1) - erg(1,2))
wend

'Coefficients
coef(!nr) mu
coef(!nl) beta
coef(!nr) p
coef(1) sigma

'Initialization of coefficent vectors
mu(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.@se
p(1) = !p11
p(2) = !p22

smpl @first @first
series prob11 = erg(1,1)
series prob12 = erg(2,1)
series prob21 = erg(3,1)
series prob22 = erg(4,1)

smpl @all
series dcptotal = 0

smpl @first + !nl @last

'Set up the log-likelihood object
logl markov
markov.append @logl logl
'Residuals
markov.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 functions
markov.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*condprob11
markov.append denscondprob12 = dens12*condprob12
markov.append denscondprob21 = dens21*condprob21
markov.append denscondprob22 = dens22*condprob22

markov.append dcptotal = denscondprob11 + denscondprob12 + denscondprob21 + denscondprob22

'Probabilities of being in State i given State j (i,j = 1,2)
markov.append prob11 = denscondprob11/dcptotal
markov.append prob12 = denscondprob12/dcptotal
markov.append prob21 = denscondprob21/dcptotal
markov.append prob22 = denscondprob22/dcptotal

markov.append logl = log(dcptotal)

smpl @first + !nl @last
markov.ml(showopts, m=500, c=1e-6)
show markov.output

'Probability of being in state i (i=1,2)
series pr1 = prob11 + prob12
series pr2 = prob21 + prob22
area pr1 pr2

smpl @all

Re: Markov switching model

Posted: Thu Apr 07, 2011 11:59 am
by mayxanh
Hi trubador,

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
by Abraham Vela
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
by donihue
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
by Abraham Vela
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.

Thanks for your reply. I appreciate it

Regards,
Abe

Re: Markov switching model

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

I am much appreciated

Re: Markov switching model

Posted: Sat May 14, 2011 5:56 pm
by mayxanh
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
by trubador
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
by Aqua
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
by mayxanh
Thank you trubador


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
by nsy6779
Hello
thank you all for sharing.
how can I estimate Std. Error, z-Statistic, and Prob for parameters?
thanks