Repository navigation
BUG: Predict only the dependent variable in constrained RecursiveLS - #11147
Merged
bashtage merged 9 commits intoNov 24, 2026
Merged
Conversation
This was referenced Nov 24, 2026
A `dynamic` date key that matches several dates of a DatetimeIndex, such as "2001-04" for daily data, gives a slice of locations, which raised "TypeError: unsupported operand type(s) for -=: 'slice' and 'int'" in `AutoReg.predict` (and so `ARDL.predict`), `MLEResults.get_prediction` (SARIMAX, ARIMA, UnobservedComponents, ...), `STLForecastResults`, `ETSResults` and `RecursiveLSResults.get_prediction`. Dynamic prediction now begins at the first date of the key, as `MLEResults.simulate` already does for a multi-date `anchor`.
Covers the branch of the new slice check where a date string matches a single date, so that its location is used unchanged.
Since statsmodels#10975 merged, ETSModel.fit, fit_constrained and smooth warn when param_index is not given (the default changes from "integer" to "names" after statsmodels 0.17). The tests this PR adds called them without it, so they leaked that FutureWarning. Pass param_index="integer", as the existing tests do; results are unchanged.
RecursiveLSResults.get_prediction (and so predict) used the location of a dynamic date string as an offset relative to start. With start=10, dynamic="2001-04-01" began dynamic prediction on 2001-04-11, silently. The shared docstring says that a date is not interpreted as an offset, and MLEResults.get_prediction converts it to an offset by subtracting start. A dynamic Timestamp or datetime raised "TypeError: Addition/subtraction of integers and integer-arrays with Timestamp is no longer supported". Changes - A dynamic date string, Timestamp or datetime is converted to its location and then to an offset relative to start, as in MLEResults.get_prediction, so dynamic prediction begins at that date. Results change when start is not the first observation. - The docstring notes the change in a versionchanged directive. Not changed - An integer or bool dynamic, which is an offset relative to start. - A dynamic date when start is the first observation (the default). - The check that raises NotImplementedError for dynamic prediction in a model with constraints. As for an integer offset of 0, a dynamic date equal to start is not caught by it. Tests - With start=10 or start="2000-01-11", a dynamic date string, month string, Timestamp or datetime gives the predictions from the coefficients estimated through the previous period up to the date, and from those estimated through the period before the date after it, and the same predictions and variances as the integer offset. All eight cases fail on main.
RecursiveLS imposes linear constraints through extra endog variables, one per constraint, whose design rows are the constraint matrix. The results remove them from the forecasts and the forecast errors, but RecursiveLSResults.get_prediction (and so predict) used the prediction of the state space model with all of them: with ndarray data, predicted_mean held the predictions of the constraint variables as a second column (and var_pred_mean the full covariance matrices), and with pandas data get_prediction and predict raised "TypeError: Index(...) must be called with a collection of some kind". The prediction results are restricted to the first endog variable before the PredictionResults are built: the arrays they cache (endog, design, obs_intercept, obs_cov, forecasts, forecasts_error and forecasts_error_cov) keep only its rows, so the filtered and smoothed forecasts and the signals, computed from these, are those of the dependent variable too. Stacked on the PR that begins dynamic prediction at a dynamic date (wip-recursivels-dynamic-date): it builds the get_prediction docstring that the versionchanged note goes into. Changes - With constraints, get_prediction and predict return the predictions of the dependent variable only, as a 1-d array or a Series with the index of the data, for every information_set, also with signal_only. - A versionchanged note under Returns in the get_prediction docstring. Not changed - Models without constraints. - Out-of-sample and dynamic prediction with constraints, which still raise NotImplementedError. Tests - With the constraint x1 = x2, the model is the regression on const and x1 + x2: for predicted, filtered and smoothed information sets, with and without signal_only, the predicted mean, its variance and the confidence intervals equal those of that regression after the diffuse periods, and so do the arrays of the underlying prediction results; the design is the regressors. The one-step predictions equal z_t' b, with b from OLS of y on z = (1, x1 + x2) over the observations before t. - With pandas data, the predictions equal those of the ndarray data and have the index of the data, also in summary_frame and predict. - The new tests fail on the base, and every changed line is guarded (11 mutants, all killed). They pass with pandas 3.0.5, 2.3.3 and 1.5.3.
…ction RecursiveLSResults.get_prediction takes its docstring from MLEResults.get_prediction, which documents extend_model and extend_kwargs. RecursiveLSResults.get_prediction does not take them: out-of-sample predictions use the design matrix built from exog (or a design keyword), not an extended model. Given by keyword, they land in **kwargs and have no effect. Changes - The two parameters are removed from the docstring. Tests - The parameters in the docstring are those of the signature, in order (fails before this commit).
bashtage
force-pushed
the
wip-recursivels-constrained-prediction
branch
from
November 24, 2026 16:17
5a40099 to
6e09248
Compare
bashtage
marked this pull request as ready for review
November 24, 2026 18:31
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 #11104 (itself stacked on #10940), merge those first (their commits are the first ones here; this PR adds two commits, the fix and a DOC commit): #11104 builds the
get_predictiondocstring that both commits edit. Other open PRs append tests at the end of the same test file: keep both appended blocks.BUG: Accept dynamic date keys that cover several dates
A
dynamicdate key that matches several dates of a DatetimeIndex, suchas "2001-04" for daily data, gives a slice of locations, which raised
"TypeError: unsupported operand type(s) for -=: 'slice' and 'int'" in
AutoReg.predict(and soARDL.predict),MLEResults.get_prediction(SARIMAX, ARIMA, UnobservedComponents, ...),
STLForecastResults,ETSResultsandRecursiveLSResults.get_prediction. Dynamic predictionnow begins at the first date of the key, as
MLEResults.simulatealreadydoes for a multi-date
anchor.TST: Check a single-date dynamic key of RecursiveLS
Covers the branch of the new slice check where a date string matches a
single date, so that its location is used unchanged.
TST: Pass param_index in the new ETS tests
Since #10975 merged, ETSModel.fit, fit_constrained and smooth warn when
param_index is not given (the default changes from "integer" to "names"
after statsmodels 0.17). The tests this PR adds called them without it,
so they leaked that FutureWarning. Pass param_index="integer", as the
existing tests do; results are unchanged.
BUG: Begin RecursiveLS dynamic prediction at a dynamic date
RecursiveLSResults.get_prediction (and so predict) used the location of a
dynamic date string as an offset relative to start. With start=10,
dynamic="2001-04-01" began dynamic prediction on 2001-04-11, silently. The
shared docstring says that a date is not interpreted as an offset, and
MLEResults.get_prediction converts it to an offset by subtracting start. A
dynamic Timestamp or datetime raised "TypeError: Addition/subtraction of
integers and integer-arrays with Timestamp is no longer supported".
Changes
location and then to an offset relative to start, as in
MLEResults.get_prediction, so dynamic prediction begins at that date.
Results change when start is not the first observation.
Not changed
model with constraints. As for an integer offset of 0, a dynamic date
equal to start is not caught by it.
Tests
Timestamp or datetime gives the predictions from the coefficients
estimated through the previous period up to the date, and from those
estimated through the period before the date after it, and the same
predictions and variances as the integer offset. All eight cases fail on
main.
BUG: Predict only the dependent variable in constrained RecursiveLS
RecursiveLS imposes linear constraints through extra endog variables, one
per constraint, whose design rows are the constraint matrix. The results
remove them from the forecasts and the forecast errors, but
RecursiveLSResults.get_prediction (and so predict) used the prediction of
the state space model with all of them: with ndarray data, predicted_mean
held the predictions of the constraint variables as a second column (and
var_pred_mean the full covariance matrices), and with pandas data
get_prediction and predict raised "TypeError: Index(...) must be called
with a collection of some kind".
The prediction results are restricted to the first endog variable before
the PredictionResults are built: the arrays they cache (endog, design,
obs_intercept, obs_cov, forecasts, forecasts_error and
forecasts_error_cov) keep only its rows, so the filtered and smoothed
forecasts and the signals, computed from these, are those of the
dependent variable too.
Stacked on the PR that begins dynamic prediction at a dynamic date
(wip-recursivels-dynamic-date): it builds the get_prediction docstring
that the versionchanged note goes into.
Changes
the dependent variable only, as a 1-d array or a Series with the index of
the data, for every information_set, also with signal_only.
Not changed
NotImplementedError.
Tests
x1 + x2: for predicted, filtered and smoothed information sets, with and
without signal_only, the predicted mean, its variance and the confidence
intervals equal those of that regression after the diffuse periods, and
so do the arrays of the underlying prediction results; the design is the
regressors. The one-step predictions equal z_t' b, with b from OLS of y
on z = (1, x1 + x2) over the observations before t.
have the index of the data, also in summary_frame and predict.
mutants, all killed). They pass with pandas 3.0.5, 2.3.3 and 1.5.3.
DOC: Remove extend_model and extend_kwargs from RecursiveLS get_prediction
RecursiveLSResults.get_prediction takes its docstring from
MLEResults.get_prediction, which documents extend_model and
extend_kwargs. RecursiveLSResults.get_prediction does not take them:
out-of-sample predictions use the design matrix built from exog (or a
design keyword), not an extended model. Given by keyword, they land in
**kwargs and have no effect.
Changes
Tests
The parameters in the docstring are those of the signature, in order
(fails before this commit).
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 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