Markov switching model

For technical questions regarding estimation of single equations, systems, VARs, Factor analysis and State Space Models in EViews. General econometric questions and advice should go in the Econometric Discussions forum.

Moderators: EViews Gareth, EViews Moderator

jpl
Posts: 4
Joined: Tue Sep 30, 2008 1:48 pm

Markov switching model

Postby jpl » 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.

csolbach
Posts: 2
Joined: Wed Oct 08, 2008 1:51 am

Re: Markov switching model

Postby csolbach » 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.

megan
Posts: 5
Joined: Wed Oct 08, 2008 1:18 pm

Re: Markov switching model

Postby megan » Fri Nov 21, 2008 10:07 pm

I am interested in this topic too. Does anyone know how this can be done?

tgoodwin
Posts: 13
Joined: Fri Oct 23, 2009 1:58 pm

Re: Markov switching model

Postby tgoodwin » Fri Oct 23, 2009 2:17 pm

Did you ever find a way to estimate Markov Switching models in E-Views?

trubador
Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

Re: Markov switching model

Postby trubador » 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 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

mayxanh
Posts: 19
Joined: Sat Feb 12, 2011 12:03 pm

Re: Markov switching model

Postby mayxanh » Thu Apr 07, 2011 11:59 am

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

Abraham Vela
Posts: 4
Joined: Mon May 02, 2011 9:33 am

Re: Markov switching model

Postby Abraham Vela » 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
Last edited by Abraham Vela on Thu May 05, 2011 9:32 am, edited 1 time in total.

donihue
Posts: 137
Joined: Wed Oct 07, 2009 8:51 am

Re: Markov switching model

Postby donihue » 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

Abraham Vela
Posts: 4
Joined: Mon May 02, 2011 9:33 am

Re: Markov switching model

Postby Abraham Vela » 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.

Thanks for your reply. I appreciate it

Regards,
Abe

mayxanh
Posts: 19
Joined: Sat Feb 12, 2011 12:03 pm

Re: Markov switching model

Postby mayxanh » Sat May 14, 2011 5:50 pm

Thank you so much for your help.

I am much appreciated

mayxanh
Posts: 19
Joined: Sat Feb 12, 2011 12:03 pm

Re: Markov switching model

Postby mayxanh » 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.

trubador
Did you use forum search?
Posts: 1518
Joined: Thu Nov 20, 2008 12:04 pm

Re: Markov switching model

Postby trubador » 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.

Aqua
Posts: 14
Joined: Mon Aug 02, 2010 7:17 am

Re: Markov switching model

Postby Aqua » 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.

mayxanh
Posts: 19
Joined: Sat Feb 12, 2011 12:03 pm

Re: Markov switching model

Postby mayxanh » Fri May 27, 2011 8:27 am

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.

nsy6779
Posts: 1
Joined: Thu Oct 20, 2011 2:51 pm

Re: Markov switching model

Postby nsy6779 » 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


Return to “Estimation”

Who is online

Users browsing this forum: No registered users and 4 guests