Skip to content

BUG: Use the current regime's variance in MarkovAutoregression - #10994

Draft
bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-msar-switching-variance
Draft

bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-msar-switching-variance

Conversation

@bashtage

Copy link
Copy Markdown
Member

With switching_variance=True and order >= 2, MarkovAutoregression used
the error variance of the regime order - 1 periods earlier instead of
that of the current regime: the variance was reshaped to
(k_regimes, 1, 1), which broadcasts against the third axis from the end
of the (k_regimes,) * (order + 1) + (nobs,) residuals. The
log-likelihood, the estimates and the regime probabilities of such
models were wrong, with no error or warning. Order 1, the default
switching_variance=False and MarkovRegression were not affected.

The variance is now reshaped to (k_regimes,) + (1,) * (order + 1), so
it varies along the current-regime axis for every order. Results of
models with switching_variance=True and order >= 2 change.

Tests: the log-likelihood with switching variances of orders 1 to 3
against the Hamilton filter written out over the joint state, and an
AR(2) with a zero second lag against the AR(1) on endog[1:] (log-
likelihood contributions and smoothed probabilities), with and without
a switching constant and switching variances.

  • tests added / passed.
  • code/documentation is well formatted.
  • properly formatted commit message. See
    NumPy's guide.

AI Disclosure

Contributions must comply with the statsmodels AI Policy.

Tick exactly one of the following. If AI tools were used, replace the <...>
placeholders in the second option with the tool name(s) and how the tool's output was
used.

  • No AI tools were used to develop this pull request.
  • AI tools were used.
    Tool(s): Claude Code (Claude Opus 5.5).
    Used for: finding the bug in a contract audit, writing the fix and its tests, and checking them (tests failing on main, mutation check).
    I have personally read, understood, and can explain every line of this diff, and
    have verified the statistical/numerical correctness of the change.

🤖 Generated with Claude Code

With switching_variance=True and order >= 2, MarkovAutoregression used
the error variance of the regime order - 1 periods earlier instead of
that of the current regime: the variance was reshaped to
(k_regimes, 1, 1), which broadcasts against the third axis from the end
of the (k_regimes,) * (order + 1) + (nobs,) residuals. The
log-likelihood, the estimates and the regime probabilities of such
models were wrong, with no error or warning. Order 1, the default
switching_variance=False and MarkovRegression were not affected.

The variance is now reshaped to (k_regimes,) + (1,) * (order + 1), so
it varies along the current-regime axis for every order. Results of
models with switching_variance=True and order >= 2 change.

Tests: the log-likelihood with switching variances of orders 1 to 3
against the Hamilton filter written out over the joint state, and an
AR(2) with a zero second lag against the AR(1) on endog[1:] (log-
likelihood contributions and smoothed probabilities), with and without
a switching constant and switching variances.
@bashtage
bashtage force-pushed the wip-msar-switching-variance branch from ff40e26 to 9f1dcfd Compare November 28, 2026 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant