MarkovSwitchingSSM#

class cultivars.state_space.regime_switching.MarkovSwitchingSSM(transition, intercepts, ar_params, variances, *, initial_prob=None)[source]#

A parameterized Markov-switching observation model over a latent chain.

The discrete counterpart of _LinearGaussianStateSpace, and its peer in every structural respect: both hold a fully specified system, both filter and smooth data handed to them, and neither estimates anything. What differs is the state space. A linear-Gaussian model carries a continuous state and propagates a mean and a covariance; this one carries a state drawn from {0, ..., K-1} and propagates a probability vector.

That difference is why the filter is Hamilton’s rather than Kalman’s, and why the likelihood is exact. Conditional on the observed lags, the density \(\Pr(y_t \mid S_t, y_{1:t-1})\) depends on \(S_t\) alone – the regime enters contemporaneously through the intercept, not through the lags – so the K-vector of filtered regime probabilities is a sufficient statistic and the sum over all \(K^T\) regime paths is carried out implicitly. Nothing here is approximated. That is a property of the intercept-switching form specifically: under Hamilton’s original mean-switching parameterization the density at \(t\) depends on \((S_t, \ldots, S_{t-p})\), and exact filtering then needs an augmented chain of \(K^{p+1}\) states.

Holding this as an object rather than as a pair of loose recursions is what lets a fitted model be re-applied: the same instance that produced an estimate can filter a different series, which the free functions in cultivars.state_space.regime_switching cannot do because they never see the data. They take a density matrix; this class owns the map that produces one.

Parameters:
  • transition (ArrayLike) – Row-stochastic (K, K) matrix, \(P_{ij} = \Pr(S_t = j \mid S_{t-1} = i)\).

  • intercepts (ArrayLike) – Per-regime intercepts, shape (K,).

  • ar_params (ArrayLike) – Per-regime autoregressive coefficients, shape (K, p). A width of zero is a switching-mean model with no dynamics.

  • variances (ArrayLike) – Per-regime innovation variances, shape (K,), all strictly positive.

  • initial_prob (ArrayLike | None) – Distribution of \(S_1\), length \(K\). Defaults to the ergodic distribution of transition.

Raises:
  • DimensionError – If the blocks do not agree on \(K\), or a block has the wrong rank.

  • SpecificationError – If transition is not row-stochastic, a variance is not strictly positive, or initial_prob is not a distribution.

  • NumericalError – If any block contains non-finite values.

See also

Attributes#

ar_params

Per-regime autoregressive coefficients, shape (K, p).

initial_prob

Distribution of the regime at the first modelled observation.

intercepts

Per-regime intercepts.

k_endog

Observed dimension; one, since the chain drives a scalar series.

k_states

the number of regimes.

n_regimes

The number of regimes, named as the chain rather than as a dimension.

order

Autoregressive order of the per-regime observation equation.

transition

The row-stochastic regime transition matrix.

variances

Per-regime innovation variances.

Methods#

conditional_means(lags)

Per-regime conditional means \(\mathbb{E}[y_t \mid S_t = j, \text{lags}]\).

density_matrix(target, lags)

The (T_eff, K) log-density matrix the recursions consume.

effective_sample(y)

Split a series into the modelled target and its lag block.

filter(y)

Filter a series, returning regime probabilities and the likelihood.

filter_densities(log_density)

Run the Hamilton filter over an already-built density matrix.

log_densities(target, means)

Gaussian log conditional densities, one column per regime.

loglikelihood(y)

The exact log-likelihood of a series under this system.

smooth(y)

Filter, then smooth, returning full-sample regime probabilities.

smooth_densities(filtered)

Run the Kim smoother over a completed forward pass.