Page 1 of 1

MSREGRESSION (Markov switching regression)

PostPosted: Wed Sep 21, 2011 1:09 pm
by TomDoan
This 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. This was revised (May 2012) to allow for time-varying transition probabilities. You also need a matching version of @MSSETUP (http://www.estima.com/forum/viewtopic.php?f=7&t=1207)

If you want to do a linear autoregression using the "Hamilton" model with switching means, use @MSVARSETUP instead (http://www.estima.com/forum/viewtopic.php?f=7&t=512).

msregression.src
MS Single Linear Equation - requires RATS 8.0 or later
(15.26 KiB) Downloaded 88 times


@MSRegression( options ) depvar
# list of regressors (if you don't use the EQUATION option)

Options

STATES=# of states [2]
SWITCH=[C]/CH/H
C means that coefficients are switching, H indicates variances are switching.
EQUATION=equation for the regressors
NFIX=# of explanatory variables which are fixed across regimes [0 or all]
If SWITCH=C or CH, the default is 0, if SWITCH=H, it's all. The fixed coefficients must be placed first in the supplementary card for regressors.

Example

This does an AR(4) model on GNP, similar to Hamilton's model, but with all coefficients and variance switching between states (rather than the Hamilton model where only the process mean switches). It's quite a bit faster than the Hamilton model since the likelihood depends only on the current state.

fruehp366b.RPF
Example file
(1 KiB) Downloaded 78 times

GNP.DAT
Data file for example
(6.99 KiB) Downloaded 57 times

Re: MSREGRESSION (Markov switching regression)

PostPosted: Sat Oct 15, 2011 11:54 pm
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

Re: MSREGRESSION (Markov switching regression)

PostPosted: Mon May 07, 2012 12:29 pm
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)