MSVAR
MSVAR
MSVAR
by Sven Schreiber
v0.22, July 2021, this is work in progress and not a finished product!
This package started as a port of the freely available Gauss code by Anders Warne, for
which I am very grateful. The interface is not yet cast in stone, incompatible changes may
happen until further notice. Estimation is based on an EM algorithm.
In order to prevent any disappointment, let us start reporting aspects that are not yet
available:
• The way the starting values are chosen right now probably has a lot of room for
improvement; see also section 4.1.
• Arbitrary exogenous variables are not supported.
• It is not yet possible to restrict the intercept term in such a way that the restriction
applies to the means of the variables. (This is related to the model type 6 below, which
is not yet enabled.)
• Additional parameter restrictions are in principle possible in the underlying code base,
but the interface is not established nor enabled in this package, and that part of the
code has not been tested yet (after the port).
1 Models
The notation is as follows:
p
X
yt = µ(st ) + Φ(st )Dt + Ai (st )yt−i + ut (1)
i=1
with ut |st = N (0, V (st )). In the model variants below some of the coefficients will be
restricted to be homogeneous, i.e. will not depend on the regime state st .
Dt is deterministic (d, 1) and st describes q regimes, meaning that it takes discrete values
among {1, . . . , q}. The q × q transition probability matrix is denoted by P .
The following describes the implemented model variants. The corresponding number
must be specified when setting up a new MSVAR model.
1. m1:switchall – This is the most general model variant. All coefficients in (1) are
actually regime-varying. Remember that this also includes the one-step forecast error
variance-covariance.
2. m2:switchallbutV – Same as model 1, except that the variance is restricted to be
homogeneous, V (st ) = V .
3. m3:switchVmu – Here the intercepts µ(st ) and variances V (st ) are regime-dependent,
but all other coefficients are homogeneous.
4. m4:switchmu – This model is a restricted version of model 3, because in addition the
variances are homogeneous, such that only the equation intercepts µ(st ) are regime-
varying.
5. m5:switchV – In a way this model is the opposite of model 2, because only the variance
V (st ) is switching while all other coefficients are assumed constant.
1
6. This model in Warne’s original list is not (yet?) supported, as it is not really covered
by the formulation (1) and also because it is computationally heavy. Maybe later?
Note that Warne’s special model numbers 7,8,9 which make it possible to have no lags in
the system (p = 0) are automatically detected from the lag order in the VAR. Since they
are special cases of models 1,2,5, just input these numbers along with a $system bundle
containing a zero lag specification. (However, models 3 and 4 cannot be combined with no
lags.)
Also notice that fancy specifications of exogenous variables are not (yet?) supported
and the package tries to detect such situations. Please just use a constant term always and
seasonal dummies if wanted; a linear trend is not recommended, but it should in principle
also be picked up when using the GUI approach or the MS_afterVAR interface below.
2 Usage
2.1 Scripting
The MSVAR package offers two different scripting interfaces, sharing the same backend
engine. Either you first estimate a regular VAR with gretl and then pass the correspond-
ing accessor bundle named “$system” to the MS_afterVAR function (which automatically
invokes MS_exec, so you don’t have to do that explicitly). Alternatively, if you prefer
not to run a plain VAR with gretl’s command first, there’s the more direct setup function
MS_setup, then followed by an explicit call of the MS_exec function. So in either case,
there are two steps involved.
2.3 MS_afterVAR
Arguments (as always with the hansl language, the arguments are positional, so the exact
names in this interface do not matter for the user):
2
8. bool timing, default FALSE
2.4 MS_setup
Again, this function is for scripting from scratch, without (explicitly) running a standard
linear VAR first. Its interface is heavily bundle-based, which means that all parameters and
options are specified in the options bundle (second argument). If the default settings are
OK for you, the most minimal usage only requires to specify the list of endogenous variables
(first argument).
Arguments:
1. list endo “endogenous variables”
2. bundle opts “options”; optional, can contain the following members which in this in-
terface must be exactly spelled as given. See also the corresponding arguments to
MS_afterVAR.
• “lagord”: lag order of the VAR (default: 1)
• “seasdums”: whether to include centered seasonal dummies (default: FALSE)
• “mtype”: switching model type as explained above (default: 1)
• “nregimes”: number of regimes (default: 2; irrelevant if qs > 1)
• “sdrivers”: number of latent variables driving the regime states (default 1; equiv-
alent to qs above; if qs > 1, then automatically nregimes: q = 2qs )
• “sump”, “verbosity”, “manageinitvals”, “timing” (see above)
Returns a bundle which forms the basis for the next step, to pass to MS_exec.
4 FIXME: The actual computation with different sets of starting values is not yet operational in this version
of the package. – See the manual “gretl + MPI” from the gretl help menu for background information on
what MPI is and how to enable it.
3
2.5 MS_exec
Only to be invoked by the user after the MS_setup call. In contrast, the MS_afterVAR
function takes care of this by itself already. The end result of both approaches is the same
(and described in Section 3).
Arguments:
1. pointer to bundle obtained from MS_setup
2. bool timing (optional, default FALSE)
If the second argument (timing) is TRUE, then some basic assessment of the time spent in
various parts of the code is attempted.
Returns as a scalar the number of sub-bundles that are added as nested members into
the main bundle, see subsection 3.2 for details. Also adds some stuff directly to the top level
of the input bundle. If all goes well (and verbosity is not switched off completely), it will
also print out estimates and some tests.
2.6 MS_irfs
This function computes regime-specific impulse response functions by taking internally the
array of estimated “compantops” (see subsection 3.1.2) and applying gretl’s built-in vma
function to each of them. Therefore arguments 2 and 3 are equivalent to those of vma.
Note that these impulse responses are conditional on staying in the analyzed regime and
therefore ignore the possibility that in any period the regime may switch. Also bear in
mind that the IRFs only depend on the autoregressive parameters and will only differ across
regimes if the AR coefficients are actually allowed to switch; i.e., only in model types 1 and
2.
Arguments:
1. MSVAR estimation result bundle, having been processed either by MS_afterVAR or
MS_exec (or via the GUI execution).
2. (Optional) Identification matrix K of dimension n × n, defining the relationship be-
tween reduced-form errors u and structural shocks e: ut = Ket . This matrix is taken
to be the same across regimes. If this is omitted (or an explicit null value is passed)
then it is taken to be the identity matrix.
3. (Optional) Maximum horizon range i, implying horizons from 0 (contemporaneous
impact) to i − 1. Default value is 24.
Returns q matrices arrays in a nested fashion, best explained by example:5 After a call
arrays switchIrfs = MS_irfs(b), one can access a specific impulse response matrix like
switchIrfs[2][4], which means to pick the impulse responses from the 2nd regime and
for the 4th shock. The rows of this matrix refer to the target variable, with the horizon
periods in columns.
4
3.1 Top level of the bundle (the one created by the setup functions
and subsequently amended)
3.1.1 Global and other results
• t1 and t2 – observation index numbers of the effective sample in the current gretl
workfile (copied from $system, more useful than ep,sp)
• T – effective sample size (t2 − t1 + 1)
• lnL – log-likelihood value (at the final iteration, thus either at convergence or at the
max iteration)
• dlnL – change of log-likelihood (at the final iteration, thus either at convergence or at
the max iteration)
• Conscore (matrix) – conditional score matrix (T columns)
• tauscore (matrix) – tau score matrix (T columns)
• it – number of iterations done
• infocrit (matrix) – row vector with the three information criteria
Finally we have the following results of the task of “Constructing the mean of A(s(t)) and
V(s(t)) conditional on y(t),y(t-1),., and the estimated parameters”:
• mus
• Vis
5
3.1.3 Estimated probabilities
• Pmat (matrix) – transition probabilities (q × q)
• nstates (matrix) – has q rows; first column simply holds regime index sequence 1 . . . q,
last column the number of observations that fall into the corresponding regime; for
qs = 1 these are the only columns, for qs > 1 there are further columns in the middle
with subindices
The following matrix results with estimated regime probabilities each have q columns if
qs = 1; else they have 2qs columns. In the following, “regime-number” ranges between
1 . . . q, and “statevarnum” ranges between 1..qs.
• exanteprobs (matrix) – ex-ante probabilities: P r(st = regime-number|yt−1 , yt−2 , ...)
or P r(sstatevarnum,t = <1 or 2>|yt−1 , yt−2 , ...)
• sump – Indicator for the user choice of a serially uncorrelated Markov process or not
(this restriction is not supported for qs > 1). Choosing this means that the transition
probabilities are restricted to be equal to the long-run probabilities.
• convcrit – convergence criterion for the loglik, usually 1e-9
6
• dparm – tolerance criterion for the relative change of parameter values (for the nuerical
calculation of derivatives), usually 1e-5
• Sea – indicating seasonal centered dummies; possible values are 0 (none) or the data
periodicity, e.g. 4 or 12
• d – number of deterministic terms (apart from the constant; Trend + Sea -1)
• manageinitvals – indicates whether to use (and how) different sets of initial values; 0:
try different sets only when MPI present (to do that in parallel), 1: only try the most
natural set (based on the plain VAR), even if MPI present, 2: try two sets, even if
MPI absent (by running them sequentially) (FIXME: not [fully?] working yet)
• initvalsets – An array of bundles (since bundles can live within bundles) holding dif-
ferent sets of possible initial (=starting) values for A, V, Pmat. So far there are two
array members. To look at the initial values for A in the second set you would have
to write (assuming the main bundle is called ’bMS’): bMS.inputs.initvalsets[2].A
• bs.rho
• bs.Ji
• bs.Jij
• bs.piT
• bs.pijT
• bs.pjT
• bs.xiqt
• bs.lnL
• bs.dlnL
• bs.xikt
• bs.xikt1
• bs.Pstar
• bg.maxgr
• bg.Pmat
7
• bg.Pmat0
• bg.p0q
• bg.prijT
• bg.H1qs
• bg.e1t
• bg.e2t
• bg.Jhqs
• Nyeqst (and Nyeqstcv) – Nyblom stability tests on params in equations (rows; state
in cols; again only for at least one lag)
• Wald (and Waldpv) – Wald test of serially uncorrel. Markov chain (only if serially
uncorrel. MC choice FALSE)
• Waldf (and Waldfpv) – F-test of serially uncorrel. Markov chain (only if serially
uncorrel. MC choice FALSE)
• Wald1 (and Wald1pv) – Wald tests of no serial correlation for each independent Markov
chain (only applicable to qs ≥ 2)
• Wald1f (and Wald1fpv) – F-test of no serial correlation for each independent Markov
chain (only applicable to qs ≥ 2)
• LM (and LMpv) – LM test for qs independent two-state regime variables, with p-
values (given by s11-s1 d.o.f., where both s11 and s1 are also bundle members; only
applicable to qs ≥ 2)
• LMf (and LMfpv) – same thing, small-sample corrected F version (only applicable to
qs ≥ 2)
• s1 – restriction counter such that s12 restrictions are imposed on the Markov proba-
bilities
• LMrest (and LMrestpv) – LM test of restrictions; can be either restrictions on the VAR
parameters6 and/or restrictions on the Markov transition probabilities (only applicable
if restrictions, e.g. if serially uncorrel. MC choice TRUE)
• LMfres (and LMfrespv) – same thing, small-sample corrected F version
6 not implemented yet in this package!
8
3.2.5 bmis – misspecification test-related
Tthis sub-bundle contains results that are only applicable if no restrictions are imposed
(neither on the VAR parameters nor a serially uncorrelated MC), otherwise it will be absent.
All tests are based on conditional scores. Members:
• Diagp01
• Diagp11
• dely and convy – First two moments / Mean and covariance matrix conditional on s(t)
• stddely and seconvy – standard errors of the above (based on conditional scores and
the Delta method using numerical partials)
• muy and vy – First two moments / Mean and covariance matrix
• stdmuy and stdvy – standard errors of the above (based on conditional scores and the
Delta method using numerical partials)
• pivec and pierr1 – ergodic probabilities and their standard errors (based on conditional
scores) (only if serially uncorrel. MC choice FALSE)
[ these above only if no trend, and if at least one lag]
9
• fobs – first observation numerical pseudo label, would indicate subsampling if larger
than sp, not really needed in the gretl context
• sp, ep – numerical decimal pseudo labels of the start and ending observations, not
needed usually in the gretl context (example: 1959.08)
4 Other notes
4.1 Starting values
Currently the starting values (for the coefficients) are based on a non-switching plain VAR,
where some ad-hoc variations are introduced for the various regimes. Given that there is a
known danger to get trapped in a local maximum of the likelihood function, one plan for
the future is to run several estimation sets with different initializations in parallel and check
afterwards which one produced the best likelihood.
5 Changelog
• v0.22, July 2021, make the case of no lags (p=0) actually work, fix a bug in the
setVerrorsm1 function, fix a bug with plotting in the case qs > 1, fix the initialization
in the case q > 2 (otherwise regimes 2,3,... would be identical, a bug introduced at
some time along the way)
• v0.21, July 2021, fix harmless but show-stopping bug for the case of a single lag (p=1)
• v0.2, June 2021, add convenience function MS_irfs, and harmonize residual notation
(forecast errors are now u, as in the new SVAR ≥v1.95 notation and elsewhere); ensure
that alternative spec names are always included
• v0.12, May 2021, move internal stuff into a subbundle, introduce model type labels
• v0.11, May 2021, work on enabling seasonal dummies (and linear trend, partly)
• version 0.1, May 2021, requires bleeding-edge gretl 2021b because of the (possible)
menu attachment to the VAR model window
7 Tested on both Eviews versions 11 and 12. The Schwarz criterion from Eviews and the BIC from MSVAR
previously did not agree, but this was actually a typo bug in MSVAR (factor 2 applied to too many terms),
now fixed. Also a miscalculation of the date information was fixed.
10