MSSETUP Procedures |
@MSSetup pulls in a set of general Markov switching setup procedures. These are included automatically in more specific MS analysis procedure groups, such as @MSRegression, @MSSysRegression and @MSVARSetup and you don't need to use @MSSetup yourself if you are relying on those. However, you may use some of the other procedures or functions described here.
@MSSetup( options )
Options
REGIMES=number of regimes[2]
Sets the number of regimes. RATS numbers the regimes starting at 1. Note that descriptions of two-state switching models may identity the regimes as 0 and 1—RATS uses 1 and 2 instead, since it more naturally extends to more than 2 regimes. The options STATES can be used as a synonym for REGIMES, though REGIMES is preferred.
LAGS=number of lags[0]
A non-zero value is used when the likelihood depends upon a (a finite number of) lagged regimes. For most models, even if the underlying model has lagged dynamics, only the current regime determines the likelihood function.
If you use the options REGIMES=M with LAGS=p (non-zero), the likelihood function is assumed to depend upon not just the current (time \(t\)) regime, but also the regimes at \(t - 1,{\rm{ }}t - 2, \ldots ,t - p\). This means that there are \(M_{p+1}\) total branches in the likelihood function—in the case of the well-known Hamilton(1989) model, for instance, \(M=2\) and \(p=4\), which means there are 32 branches needed to evaluate the likelihood at time \(t\). RATS internally numbers these with the most distance lag changing fastest, and current value changing slowest, that is, in the case of the Hamilton model, expanded regime 1 is 1 at all positions, expanded regime 2 is 1 for \(t\) to \(t−3\) and 2 for \(t−4\), 3 is 1 for \(t\), \(t−1\), \(t−2\), 2 at \(t−3\) and 1 at \(t−4\), etc.
The %MSLagRegime function can be used to recover the regime at a specific lag from the expanded regime number.
Variables Defined
|
NREGIMES |
number of regimes (INTEGER) |
|
NSTATES |
number of regimes (INTEGER) (provided for backwards compatibility) |
|
NEXPAND |
number of expanded regimes (INTEGER) |
|
%MSFILTERSIZE |
number of regimes used in a filter pass (INTEGER) |
|
PT_T |
SERIES[VECT] with the filtered (estimate at t given information through t) estimates of the probabilities of expanded regimes. This is computed as a side-effect of several of the update functions/procedures. |
|
PT_T1 |
SERIES[VECT] with the predictive estimates of the probabilities of expanded regimes, that is, the probabilities of the expanded regimes at t given information through \(t−1\). This is computed as a side-effect of several of the update functions/procedures. |
|
MSRegime |
SERIES[INT] with the regime number at time t. Used for Gibbs sampling. |
|
PSTAR |
Used in doing calculations. It's the probability of the (expanded) regime vector at the last entry. |
|
P |
Either \(M−1 \times M\) or \(M \times M\) RECTANGULAR of transition probabilities. (Which dimensions are used depends upon the estimation method). Note that with two-regime models, the transition probabilities are often parameterized as the probability of staying in each regime. That doesn't generalize to more than two regimes, so we don't use it. We are also defining p(i,j) to be the probability of moving from j to i. The transition matrix is often written as the transpose of this (p(i,j) is the probability of moving from i to j) which produces formulas which look unnatural (order reversed from what one would expect from most matrix algebra) so you have to keep that in mind in comparing and reporting results. |
|
THETA |
\(M−1 \times M\) RECTANGULAR of logistic indexes for parameterizing transition probabilities. |
%MSLogisticP(theta)
returns the result from transforming an \(M−1 \times M\) matrix of logistic indexes into an \(M−1 \times M\) matrix of transition probabilities. (The parameterization in terms of logistic indexes can be useful in doing maximum likelihood estimation).
%MSPLogistic(p)
is the inverse of %MSLogisticP—it takes the \(M−1 \times M\) matrix of transition probabilities and returns the implied logistic indexes. This is generally only needed for transforming guess values.
returns the ergodic regime probabilities for transition matrix p.
is a high-end function which can be applied if the transition probabilities are fixed over time (that is, not time-varying). It does both the predictive and filtering steps of the filter (basically combining the calculations done by %MCSTATE and %MSUPDATE).
%MCState(p,pstar)
returns the predictive regime probabilities given initial probabilities pstar and transition matrix p.
%MSUpdate(f,pstar,fpt)
returns the updated regime probabilities given the vector of likelihoods (levels, not logs) of the regimes in f and the prior probabilities of the regimes in the vector pstar. fpt is a REAL returned by the function which gives the sum over \(i\) of \(f \times pstar\). If f is a vector of likelihoods given the regimes, fpt will be the overall likelihood for this entry.
returns the regime at lag lag for expanded regime i. (LAG=0 is for the current regime).
@%MSSmooth p pt_t pt_t1 psmooth
is a procedure which produces the VECT[SERIES] psmooth given the coded transition probability matrix p and the SERIES[VECT] pt_t and pt_t1, which give the estimated probabilities at t given information through t and t-1 respectively.
@%MSSample(options) p pt_t pt_t1 states
is a procedure which produces the SERIES[INT]states as a backwards sampled sequence of regimes, given the coded transition probability matrix p and the SERIES[VECT] pt_t and pt_t1, which give the estimated probabilities at t given information through t and t-1 respectively. The option is COUNTS=(output) VECT[INT] of sampled regimes counts which can be used to make sure none of the regimes is too small to continue.
%MSSmoothCalc(pfull,pt_t,pt1_sm,pt1_t)
does the smoothing calculation for one time period. pt_t is the estimate of the probabilities for time period t given data through t, pt1_t is for time period t+1 given data through t, pt1_sm is for time period t+1 given all data, and pfull is the (full) transition matrix for regimes at t to t+1. This is safeguarded against divides by zero when pt1_t has underflowed down to a zero value.
@MSPChangeFnc newfnc
is used to change the transition probability function. newfnc has to be a FUNCTION of a single INTEGER which returns a RECT for the given entry number. The return matrix can either be \(M−1 \times M\) or the full \(M \times M\). The default (fixed transition probability) function is called %MSP.
%MSSwapCheck(swaps)
is used in Markov Chain Monte Carlo analysis to check whether the regimes need to be relabeled. swaps is a VECT[INTEGER] with the index order for whatever statistic is being used to control the labeling. %MSSwapCheck(swaps) returns 1 if swaps shows that a relabeling is required, and 0 if it isn't (that is, it's 0 if the statistics are in the desired order).
Examples
This is the start of a Markov Switching model with three regimes for the variance of a process. Note that @MSSetup itself doesn't do anything to give content to the model—that's what (for instance), the definition of SIGMAS is starting to do, as those will be the free parameters in the model, and are initialized to have regimes numbered 1 to 3 from low variance to high.
@MSSetup(regimes=3)
*
dec vect sigmas(nregimes)
stats ew_excs
compute sigmas(1)=0.2*%variance
compute sigmas(2)= %variance
compute sigmas(3)=5.0*%variance
This is the start of a Markov Switching ARCH model. Because of the particular way the likelihood is computed, this requires LAGS=Q (2 in this case). This means that there will be 8 branches in the log likelihood (all combinations of two regimes at \(\{ t,t - 1,t - 2\} \)).
compute q=2
@MSSetup(regimes=2,lags=q)
*
* GV will be the relative variances in the regimes.
*
dec vect gv(nregimes)
*
* This will be the vector of ARCH parameters and the asymmetry
* parameter. The constant in the ARCH equation is fixed at 1 as the
* normalization.
*
dec vect a(q)
dec real xi
This does a particular form of time-varying transition probability (TVTP), with the transition probabilities being Normal with an index that's depends upon the lagged value of RATE. The - signs in P(1,2) are because the paper from which it was taken modelled P(2,2) instead—since, for a Normal, \(\Phi (x) = 1 - \Phi ( - x)\), flipping the signs in P(1,2) will give the same results. A logistic rather than Normal distribution is often used for the TVTP as well. The guess values for the C1, D1, C2 and D2 give .90 probabilities of remaining in each regime, with (as a guess) no variation with the RATE.
nonlin(parmset=tvtpparms) c1 d1 c2 d2
*
function %MSPTV time
type rect %MSPTV
type integer time
*
local rect p(1,2)
*
compute p(1,1)=%cdf(c1+d1*rate(time-1))
compute p(1,2)=%cdf(-c2-d2*rate(time-1))
compute %MSPTV=p
end
*
@MSPChangeFnc %MSPTV
*
frml logltvtp = p=%MSPMat(t),f=RegimeGRSF(t),fpt=%MSProb(t,f),log(fpt)
compute c1=c2=%invnormal(.90),d1=d2=0.0
maximize(start=(pstar=%mcergodic(||%cdf(c1+d1*avgrate),%cdf(-c2-d2*avgrate)||)),$
parmset=meanparms+grsparms+tvtpparms,$
method=bfgs,iters=100,pmethod=simplex,piters=10) logltvtp gstart gend
Copyright © 2026 Thomas A. Doan