Repository navigation
Conversation
bashtage
force-pushed
the
wip-msar-predict-conditional-k
branch
from
November 25, 2026 12:11
763a437 to
e9481c5
Compare
4 of 5 tasks
bashtage
force-pushed
the
wip-msar-predict-conditional-k
branch
3 times, most recently
from
November 27, 2026 23:07
752d2aa to
e4b9185
Compare
…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).
`MarkovAutoregression.predict(params, conditional=k)` with
0 < k < order + 1 (`conditional=True` is 1) returned the conditional
predictions weighted by the joint probabilities and summed over the
regimes that are not conditioned on: sum_B Pr[A, B] E[y_t | A, B] =
Pr[A] E[y_t | A], where A are the k conditioning regimes (the current
regime and k - 1 past ones) and B the earlier ones. So the expected value
given A was multiplied by the probability of A. With `order=1`,
`conditional=1` returned Pr[S_t = i] E[y_t | S_t = i]: for a regime with
smoothed probability 3.0e-5 the prediction was 8.5e-5 instead of 2.80.
`MarkovSwitching.predict` now sums the probabilities over the same regime
axes as the weighted predictions and divides by that sum, so the averaged
regimes are weighted by their probabilities conditional on A. Where the
probability of A is zero (it underflows to zero when the regimes are far
apart), the expected value is undefined and the prediction is NaN, without
a warning.
Changes
- Results of `MarkovAutoregression.predict` and
`MarkovSwitchingResults.predict` with `conditional` True or 1 to `order`
change, for each `probabilities` choice ("smoothed", "filtered",
"predicted").
- `.. versionchanged:: 0.16.0` notes in both `predict` docstrings.
Not changed
- `conditional=False`: the probabilities sum to one, so the division
changes the unconditional predictions (and `fittedvalues`, `resid`) only
at the rounding level.
- `conditional=order + 1` (or larger) for `MarkovAutoregression`: no
averaging.
- `MarkovRegression`: its predictions depend only on the current regime,
so `conditional=True`/1 never averaged; only `conditional=False` reaches
the changed code (rounding level).
Tests
- For orders 1 and 2, `conditional` 1, True and (order 2) 2, and smoothed,
filtered and predicted probabilities: the predictions, through the model
and the results, equal the conditional means written out from the
parameters (mu_i + sum_j phi_{j,i} (y_{t-j} - mu_{S_{t-j}})) averaged by
hand with the joint probabilities. Averaging them over the conditioning
regimes gives the unconditional prediction (law of total expectation).
- Controls: `conditional=order + 1` equals the hand-written conditional
means, and `conditional=False` their average with the joint
probabilities (both pass on main).
- Regimes far enough apart that a regime probability underflows to zero:
NaN exactly there, no warning, the hand-computed values elsewhere.
- The new tests fail on main (16 of 22; the 6 controls pass), and every
changed line is guarded (7 mutants, all killed).
bashtage
force-pushed
the
wip-msar-predict-conditional-k
branch
from
November 28, 2026 21:36
e4b9185 to
1df9085
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #11143, which is stacked on #10995: merge those first (this PR adds one commit). 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.
MarkovAutoregression.predict(params, conditional=k)with0 < k < order + 1 (
conditional=Trueis 1) returned the conditionalpredictions weighted by the joint probabilities and summed over the
regimes that are not conditioned on: sum_B Pr[A, B] E[y_t | A, B] =
Pr[A] E[y_t | A], where A are the k conditioning regimes (the current
regime and k - 1 past ones) and B the earlier ones. So the expected value
given A was multiplied by the probability of A. With
order=1,conditional=1returned Pr[S_t = i] E[y_t | S_t = i]: for a regime withsmoothed probability 3.0e-5 the prediction was 8.5e-5 instead of 2.80.
MarkovSwitching.predictnow sums the probabilities over the same regimeaxes as the weighted predictions and divides by that sum, so the averaged
regimes are weighted by their probabilities conditional on A. Where the
probability of A is zero (it underflows to zero when the regimes are far
apart), the expected value is undefined and the prediction is NaN, without
a warning.
Changes
MarkovAutoregression.predictandMarkovSwitchingResults.predictwithconditionalTrue or 1 toorderchange, for each
probabilitieschoice ("smoothed", "filtered","predicted").
.. versionchanged:: 0.16.0notes in bothpredictdocstrings.Not changed
conditional=False: the probabilities sum to one, so the divisionchanges the unconditional predictions (and
fittedvalues,resid) onlyat the rounding level. A
probabilitieslist whose weights do not sumto one in each period is now normalised, which gives the weighted
average that the docstring describes.
conditional=order + 1(or larger) forMarkovAutoregression: noaveraging.
MarkovRegression: its predictions depend only on the current regime,so
conditional=True/1 never averaged; onlyconditional=Falsereachesthe changed code (rounding level).
Tests
For orders 1 and 2,
conditional1, True and (order 2) 2, and smoothed,filtered and predicted probabilities: the predictions, through the model
and the results, equal the conditional means written out from the
parameters (mu_i + sum_j phi_{j,i} (y_{t-j} - mu_{S_{t-j}})) averaged by
hand with the joint probabilities. Averaging them over the conditioning
regimes gives the unconditional prediction (law of total expectation).
Controls:
conditional=order + 1equals the hand-written conditionalmeans, and
conditional=Falsetheir average with the jointprobabilities (both pass on main).
Regimes far enough apart that a regime probability underflows to zero:
NaN exactly there, no warning, the hand-computed values elsewhere.
The new tests fail on main (16 of 22; the 6 controls pass), and every
changed line is guarded (7 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.
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