MarkovSwitchingAR#

class cultivars.univariate.regime_switching.MarkovSwitchingAR(endog, *, order, n_regimes=2, switching_mean=True, switching_variance=True, switching_ar=False)[source]

Bases: _MarkovSwitchingModel[MarkovSwitchingARResult]

Markov-switching autoregression with K latent regimes.

The intercept-switching parameterization: the regime enters contemporaneously through \(c_{S_t}\), so conditional on the observed lags each regime is a linear autoregression and inference runs on a \(K\)-state Hamilton filter rather than the \(K^{p+1}\) states a mean-switching form needs. Which blocks switch is chosen here and reported by the result’s specification in Krolzig’s MS{I,A,H}(K)-AR(p) code; at least one block must switch, since otherwise no regime is identified. Hamilton’s recession-dating model is MSIH(2)-AR(4), MarkovSwitchingAR(y, order=4, n_regimes=2). order=0 is allowed and gives a hidden Markov model with Gaussian emissions.

Variables:
  • _endog – The validated series.

  • _order – Autoregressive order within each regime.

  • _k – Number of regimes.

  • _sw_mean – Whether the intercept switches.

  • _sw_var – Whether the variance switches.

  • _sw_ar – Whether the autoregressive block switches.

Parameters:
  • endog (ArrayLike) – The series.

  • order (int) – Autoregressive order p within each regime.

  • n_regimes (int) – Number of regimes K, at least two.

  • switching_mean (bool) – Whether the intercept switches.

  • switching_variance (bool) – Whether the innovation variance switches.

  • switching_ar (bool) – Whether the autoregressive coefficients switch. Off by default: switching the AR block multiplies the parameter count by K and is rarely what identifies the regimes.

Raises:
  • SpecificationError – If order is not a non-negative integer, n_regimes is below two, or no block switches.

  • DimensionError – If endog is not one-dimensional or has fewer than K(p + 2) observations.

  • NumericalError – If endog contains non-finite values.

See also

References

Hamilton, J. D. (1989). A new approach to the economic analysis of nonstationary time series and the business cycle. Econometrica, 57(2), 357-384.

Hamilton, J. D. (1990). Analysis of time series subject to changes in regime. Journal of Econometrics, 45(1-2), 39-70.

Example

>>> import numpy as np
>>> rng = np.random.default_rng(0)
>>> p = np.array([[0.97, 0.03], [0.05, 0.95]])
>>> mu = np.array([-1.0, 1.5])
>>> s = np.zeros(1500, dtype=int)
>>> for t in range(1, 1500):
...     s[t] = np.searchsorted(np.cumsum(p[s[t - 1]]), rng.random())
>>> y = mu[s] + 0.5 * rng.standard_normal(1500)
>>> res = MarkovSwitchingAR(y, order=1, n_regimes=2).fit(seed=0)
>>> bool(res.intercepts[0] < res.intercepts[1])
True
>>> bool(np.allclose(res.expected_durations, [1 / 0.03, 1 / 0.05], rtol=0.25))
True

The switching blocks are the specification, and a model with none is refused:

>>> MarkovSwitchingAR(y, order=1, switching_variance=False).fit(seed=0).specification
'MSI(2)-AR(1)'
>>> MarkovSwitchingAR(y, order=1, switching_mean=False, switching_variance=False)
...
Traceback (most recent call last):
    ...
cultivars.exceptions.SpecificationError: at least one of switching_mean, ...