Skip to content

BUG: Keep the time axis of Markov switching predictions with one observation - #11143

Draft
bashtage wants to merge 4 commits into
statsmodels:mainfrom
bashtage:wip-ms-predict-nobs-one
Draft

bashtage wants to merge 4 commits into
statsmodels:mainfrom
bashtage:wip-ms-predict-nobs-one

Conversation

@bashtage

@bashtage bashtage commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Stacked on #10995, merge that first (this PR adds one commit): without it the period selection applies to the regime axis, so conditional predictions with one observation stay wrong. The stack was rebuilt on 2026-11-25 so that it merges without conflicts in this order; notes about conflicts with PRs of the same stack no longer apply. #11012 conflicts with this PR only in the two versionchanged blocks under conditional (as it does with #10995): keep one directive with all three paragraphs. With #11012 merged too, the MarkovAutoregression predictions with conditional 1 to order and one observation are the expected values given those regimes (checked on a trial merge). Other open PRs append tests at the end of the same test file: keep both appended blocks.

BUG: Select periods, not regimes, in conditional Markov switching predict

MarkovSwitching.predict (and MarkovSwitchingResults.predict) with
conditional=True or an int and with start or end applied the
[start:end + 1] selection to the first axis of the conditional
predictions, which indexes the regime. The result had every period and
the wrong regimes, or no regimes at all when start was at least
k_regimes, with no error. For example, predict(params, start=10,
end=20, conditional=True) of a two-regime MarkovRegression returned an
array of shape (0, nobs).

The selection now applies to the last axis, the periods. Unconditional
predictions, which are one-dimensional, and conditional predictions of
the full sample do not change; conditional predictions with start or
end change.

Tests: conditional predictions of MarkovRegression against the regime
means, and of an AR(1) MarkovAutoregression given the current and last
regime against mu_i + phi_i (y_{t-1} - mu_j), for several start/end
pairs and for date keys; unconditional and current-regime predictions
with start/end against the same periods of the full-sample predictions.

BUG: Keep the time axis of Markov switching predictions with one observation

MarkovSwitching.predict squeezed the predictions conditional on the
regimes, shaped (k_regimes,) * n + (nobs,), to drop the length-one axis of
the previous regime that MarkovRegression predictions carry. With a single
observation (nobs 1) np.squeeze also dropped the time axis, so the last
regime axis was taken for the time axis: the check whether to average over
the regimes compared one axis too few, and the selection of the periods
kept the first regime of that axis only. For MarkovRegression, predict()
(also with conditional False), fittedvalues and resid were the prediction
of the first regime instead of the weighted average; conditional=True
returned one value instead of one per regime. For MarkovAutoregression,
conditional=order returned the predictions given the first regime in the
earliest period instead of their average over that regime, and larger
conditional values lost that regime axis.

Only the regime axes of length one are dropped now, never the time axis.

Stacked on the PR that selects periods along the time axis of conditional
predictions (wip-ms-predict-conditional-slice): without it, the period
selection applies to the regime axis and the conditional predictions with
one observation are still wrong.

Changes

  • MarkovSwitching.predict and MarkovSwitchingResults.predict (and so
    fittedvalues and resid) with nobs 1 give the results of the same
    computation as with more observations.
  • A paragraph in the versionchanged notes under conditional in both
    predict docstrings.

Not changed

  • Models with more than one observation: the time axis was never
    squeezed.
  • MarkovAutoregression with conditional False, which averaged over every
    regime axis already.

Tests

  • MarkovRegression with one observation: the unconditional prediction,
    fittedvalues and resid equal the regime means averaged with the
    smoothed probabilities written out by hand (ergodic probabilities times
    the normal densities), and conditional True, 1 and 2 give one prediction
    per regime, the regime means, through the model and the results.

  • MarkovAutoregression of order 1 with one observation: conditional 2 and
    3 give mu_i + phi_i (y_0 - mu_j) for each pair of regimes, and the
    unconditional prediction (a control that passes before) their average
    with the joint probabilities written out by hand.

  • The new tests fail on the base (6 of 7; the control passes), and every
    changed line is guarded (5 mutants, all killed).

  • 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 coverage audit of statsmodels.tsa, writing the fix and its tests, and checking them (tests failing on the base, 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

@bashtage
bashtage force-pushed the wip-ms-predict-nobs-one branch 2 times, most recently from 41ac6ab to e09b574 Compare November 25, 2026 12:14
@bashtage
bashtage force-pushed the wip-ms-predict-nobs-one branch 3 times, most recently from b78aa2f to 0f6f0df Compare November 27, 2026 23:06
…dict

MarkovSwitching.predict (and MarkovSwitchingResults.predict) with
conditional=True or an int and with start or end applied the
[start:end + 1] selection to the first axis of the conditional
predictions, which indexes the regime. The result had every period and
the wrong regimes, or no regimes at all when start was at least
k_regimes, with no error. For example, predict(params, start=10,
end=20, conditional=True) of a two-regime MarkovRegression returned an
array of shape (0, nobs).

The selection now applies to the last axis, the periods. Unconditional
predictions, which are one-dimensional, and conditional predictions of
the full sample do not change; conditional predictions with start or
end change.

Tests: conditional predictions of MarkovRegression against the regime
means, and of an AR(1) MarkovAutoregression given the current and last
regime against mu_i + phi_i (y_{t-1} - mu_j), for several start/end
pairs and for date keys; unconditional and current-regime predictions
with start/end against the same periods of the full-sample predictions.
…rvation

MarkovSwitching.predict squeezed the predictions conditional on the
regimes, shaped (k_regimes,) * n + (nobs,), to drop the length-one axis of
the previous regime that MarkovRegression predictions carry. With a single
observation (nobs 1) np.squeeze also dropped the time axis, so the last
regime axis was taken for the time axis: the check whether to average over
the regimes compared one axis too few, and the selection of the periods
kept the first regime of that axis only. For MarkovRegression, predict()
(also with conditional False), fittedvalues and resid were the prediction
of the first regime instead of the weighted average; conditional=True
returned one value instead of one per regime. For MarkovAutoregression,
conditional=order returned the predictions given the first regime in the
earliest period instead of their average over that regime, and larger
conditional values lost that regime axis.

Only the regime axes of length one are dropped now, never the time axis.

Stacked on the PR that selects periods along the time axis of conditional
predictions (wip-ms-predict-conditional-slice): without it, the period
selection applies to the regime axis and the conditional predictions with
one observation are still wrong.

Changes
- MarkovSwitching.predict and MarkovSwitchingResults.predict (and so
  fittedvalues and resid) with nobs 1 give the results of the same
  computation as with more observations.
- A paragraph in the versionchanged notes under conditional in both
  predict docstrings.

Not changed
- Models with more than one observation: the time axis was never
  squeezed.
- MarkovAutoregression with conditional False, which averaged over every
  regime axis already.

Tests
- MarkovRegression with one observation: the unconditional prediction,
  fittedvalues and resid equal the regime means averaged with the
  smoothed probabilities written out by hand (ergodic probabilities times
  the normal densities), and conditional True, 1 and 2 give one prediction
  per regime, the regime means, through the model and the results.
- MarkovAutoregression of order 1 with one observation: conditional 2 and
  3 give mu_i + phi_i (y_0 - mu_j) for each pair of regimes, and the
  unconditional prediction (a control that passes before) their average
  with the joint probabilities written out by hand.
- The new tests fail on the base (6 of 7; the control passes), and every
  changed line is guarded (5 mutants, all killed).
@bashtage
bashtage force-pushed the wip-ms-predict-nobs-one branch from 0f6f0df to f8e0e13 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