MSREGRESSION—Markov switching regression

Use this forum to post complete RATS "procedures". Please be sure to include instructions on using the procedure and detailed references where applicable.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

MSREGRESSION—Markov switching regression

Unread post by TomDoan »

@MSRegression is a set of procedure files for setting up Markov Switching univariate linear regressions, with either the full coefficient vector switching or part of the coefficient vector is switching and part fixed. The residual variance can be either switching or fixed.

If you want to do a linear autoregression using the "Hamilton" model with switching means, use @MSVARSETUP (with a single dependent variable) instead.

Detailed description
banya56
Posts: 4
Joined: Mon Sep 12, 2011 10:32 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by banya56 »

Dear TomDoan,

I try to do a Markov Switching linear regression using the msregression.src which you posted.
However, when I impose a constraint on my model, the maximization does not work properly.
How can I impose a constraint that state 1 coefficient is zero?

This is my codes and my data:

Code: Select all

********************************************************

source msregression.src

OPEN DATA "Exchange Rate and Index-R2.xlsx"
Allocate 417
DATA(FORMAT=XLSX,ORG=COLUMNS) /

table

set ykr = 100.0*log(exkr/exkr{1})
set ytw = 100.0*log(extw/extw{1})
set yth = 100.0*log(exth/exth{1})
set yid = 100.0*log(exid/exid{1})
set yph = 100.0*log(exph/exph{1})

set reu = 100.0*log(exeu/exeu{1})
set rus = 100.0*log(exus/exus{1})

set spkr = 100.0*log(stkr/stkr{1})
set sptw = 100.0*log(sttw/sttw{1})
set spth = 100.0*log(stth/stth{1})
set spid = 100.0*log(stid/stid{1})
set spph = 100.0*log(stph/stph{1})

set xjp = 100.0*log(exjp/exjp{1})

*
* Markov Switching Linear Regression. Estimation by Maximum Likelihood
*

*@msregression(switch=CH,nfix=3,states=2) ykr ; # constant rus spkr xjp; disp "drt=EUR/SFR, dyt=KRW/SFR"
@msregression(switch=CH,nfix=3,states=2) ytw ; # constant rus sptw xjp; disp "drt=EUR/SFR, dyt=Taiwan/SFR"
*@msregression(switch=CH,nfix=3,states=2) yth ; # constant rus spth xjp; disp "drt=EUR/SFR, dyt=Thai/SFR"
*@msregression(switch=CH,nfix=3,states=2) yid ; # constant rus spid xjp; disp "drt=EUR/SFR, dyt=Ind/SFR"
*@msregression(switch=CH,nfix=3,states=2) yph ; # constant rus spph xjp; disp "drt=EUR/SFR, dyt=Ph/SFR"

*
nonlin(parmset=regparms) gammas betas sigsqv
nonlin(parmset=msparms)  p

nonlin(parmset=constraint) betas(1)==0.0


compute gstart=4,gend=417
@MSRegInitial gstart gend
*
* Compute a lower bound on the permitted values of the variance to
* prevent convergence to a zero-variance spike.
*
compute sigmalimit=1.e-6*%minvalue(sigsqv)
*
frml logl = f=%MSRegFVec(t),fpt=%MSProb(t,f),log(fpt)
@MSFilterInit
maximize(start=%(sigmatest=%MSRegInitVariances(),pstar=%msinit()),$
  parmset=regparms+msparms+constraint,$
  reject=sigmatest<sigmalimit,$
  method=bfgs,pmethod=simplex,piters=5) logl gstart gend
*
@MSSmoothed gstart gend psmooth
*
* Smoothed probabilities of the regimes
*
set p1smooth = psmooth(t)(1)
graph(footer="Smoothed Probabilities of Regime 1",max=1.0,min=0.0)
# p1smooth

Best Regards,
Banya56
Attachments
Exchange Rate and Index-R2.xlsx
(65.37 KiB) Downloaded 1366 times
msrm.ysh.obs.prg
(2.13 KiB) Downloaded 1576 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by TomDoan »

Looks fine to me.

Code: Select all

1.  GAMMAS(1)                    -6.9444e-003       0.0153     -0.45444  0.64951514
2.  GAMMAS(2)                          0.9354       0.0123     75.84867  0.00000000
3.  GAMMAS(3)                         -0.0213  4.2151e-003     -5.05171  0.00000044
4.  BETAS(1)(1)                   5.3562e-012       0.0000      0.00000  0.00000000
5.  BETAS(2)(1)                        0.0273       0.0463      0.58824  0.55637390
6.  SIGSQV(1)                          0.0456  6.5282e-003      6.98964  0.00000000
7.  SIGSQV(2)                          0.4677       0.0660      7.08117  0.00000000
8.  P(1,1)                             0.8831       0.0333     26.51279  0.00000000
9.  P(1,2)                             0.2004       0.0552      3.62931  0.00028418
Your pegged parameter is machine zero. However, you can change it to

nonlin(parmset=constraint) betas(1)(1)=0.0

which will give exactly zero for betas(1)
zipop
Posts: 17
Joined: Thu Sep 05, 2013 4:52 am

Re: MSREGRESSION (Markov switching regression)

Unread post by zipop »

dear Tom
i would like to estimate a MS regression with 4 regimes where the variances of the errors will switch across the 4 regimes. But i would like the coefficients to only switch across 2 regimes ie they will be identical across two regimes. how be be specified?

thanks
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by TomDoan »

Did you want a nested switching model with two sets of coefficients, then each set of coefficients having two possible variances?

I can't imagine that a model that complex (either switching models or a four regime switching model) would ever work---even simpler models with switching coefficients and variances give results that are almost impossible to interpret.
zipop
Posts: 17
Joined: Thu Sep 05, 2013 4:52 am

Re: MSREGRESSION (Markov switching regression)

Unread post by zipop »

a nested switching model may be the right term. this sort of specifications is employed by Davig and Leeper (2011, European Economic review) to estimate a tyalor rule for the USA.
please see the estimated result in Table 1 in the paper at http://www.nber.org/papers/w15133.pdf?new_window=1
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by TomDoan »

Have you tried doing that with a single chain first? I assume that's what they started with and decided that they needed the more complicated model. You probably can't even estimate the model with the separate coefficient and variance switches without first estimating the model with simultaneous switches to get guess values.

I haven't found on either Eric or Troy's web page the original estimation code---everything seems to be simulations for the already-estimated model.
zipop
Posts: 17
Joined: Thu Sep 05, 2013 4:52 am

Re: MSREGRESSION (Markov switching regression)

Unread post by zipop »

yes i did. that specification result does not show a taylor principle (coef on inflation larger than one) and even shows no significant coef on outputgap. this is the motivation to follow a regime switching model ...
but using simply two regime returns a negative coef on output when the taylor principle is found in that regime. maybe i impose an inequality constraint here


yes they did not post the codes used to estimate the switching in the policy rules

thanks
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by TomDoan »

You could contact them and see if they have that.

At any rate, I'm guessing that the process was that they first tried a switching model with a single variance, then a switching model with coefficients and variances switching together (which is what they ended up with for the fiscal rule), before settling on the independently switching coefficients and variances. Probably any of those models requires quite a bit of careful choice of guess values in order to get convergence to anything sensible.
gabri_london
Posts: 4
Joined: Mon Aug 07, 2017 3:16 am

Re: MSREGRESSION (Markov switching regression)

Unread post by gabri_london »

Dear Tom,

Hope you're well. I've been experimenting with a variety of Markov switching models using both the MSREGRESSION and MSVARSetup procedures on a univariate time series. I tried running the exact same specification (an MSAR(2,5) model with all parameters switching), with the initial values generated by @MSRegInitial and @MSVARInitial respectively. Though the labels are switched, the resulting parameter estimates are extremely similar, as are the smoothed regime probabilities. So far so good.

However, I run into a problem when I try to obtain the standardized residuals to assess the fit of this model (generated by @MSVARStdResiduals and @MSRegStdResiduals respectively): these do not appear to be similar at all. Most importantly, the MSVARSetup residuals do not appear to have residual autocorrelation, but the MSREGRESSION residuals do. This is surprising to me, because with the same specification and the same parameters, I would expect the same (standardized) residuals. And in particular, I would expect to draw the same conclusion with regards to residual autocorrelation.

Am I wrong to expect these to obtain the same (standardized) residuals? Or did I make a mistake somewhere? Would you be able to help? Reproducible example attached.

As an additional question, can I easily obtain the predicted values from MSREGRESSION like with @MSVARStaticForecast?
Attachments
example msvarsetup.rpf
(1017 Bytes) Downloaded 1558 times
example msregression.rpf
(1.04 KiB) Downloaded 1517 times
example data.prn
(17.1 KiB) Downloaded 1477 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by TomDoan »

I put an updated version of the @MSregression procedure on the starting post which corrects the calculation of the standardized residuals.

Note that with the more modern versions of the procedures, I get two very different results from the "MSVAR" version from the "MSReg" version, but with rather similar log likelihoods. (The MSVAR uses different guess values for the intercepts, which is why they end up in different places), and neither one is all that much better than a single linear regression when you have 1200+ data points.
gabri_london
Posts: 4
Joined: Mon Aug 07, 2017 3:16 am

Re: MSREGRESSION (Markov switching regression)

Unread post by gabri_london »

TomDoan wrote:I put an updated version of the @MSregression procedure on the starting post which corrects the calculation of the standardized residuals.

Note that with the more modern versions of the procedures, I get two very different results from the "MSVAR" version from the "MSReg" version, but with rather similar log likelihoods. (The MSVAR uses different guess values for the intercepts, which is why they end up in different places), and neither one is all that much better than a single linear regression when you have 1200+ data points.
That fixed it, thank you very much for your help.

I think I'm using the latest versions of the procedures, but the results aren't that different on my end: the output looks quite different on first impression, but this is because, in addition to the label switching, the coefficients are sorted differently. After rearranging and correcting the signs of theta, the results are very similar. I'm guessing the remaining differences are due to the way the first five values are handled. Other than this, they should be the same specification and hence result in the same coefficients, right?

I agree that this model is no much (or not at all) better than a simple linear regression. I went from the MSVAR procedure to the MSReg procedure so that I could incorporate other variables, and I only ran into this issue because I was checking my understanding and whether I'd adapted an MSReg example script correctly. Since model selection is actually an important aspect of my research, I'm interested in hearing you out on this: do you think 1200 data points is too little data to use this model on?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION (Markov switching regression)

Unread post by TomDoan »

1200 data points isn't the problem (either because it's too big or too small). What's the problem is that there is no reason to believe that a MS model of any type is likely to work much better than the same model without the MS on this data set.
abi
Posts: 74
Joined: Sat Apr 13, 2013 3:48 am

Re: MSREGRESSION—Markov switching regression

Unread post by abi »

Dear Tom,
I would like to compute the regime-specific residuals and then get the sums of squared residuals for each regime in a Markov switching linear regression. Could you give me an example with detail?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: MSREGRESSION—Markov switching regression

Unread post by TomDoan »

abi wrote:Dear Tom,
I would like to compute the regime-specific residuals and then get the sums of squared residuals for each regime in a Markov switching linear regression. Could you give me an example with detail?
You can use @MSRegResids (once with SPECIFIC=1 and once with SPECIFIC=2) to get the regime-specific residuals. However, the sums of squared regime-specific residuals is a garbage statistic that provides no useful information.
Post Reply