Repository navigation
BUG: Do not write defaults into the caller's extend_kwargs - #11169
Merged
Merged
Conversation
This was referenced Nov 24, 2026
VARMAXResults.get_prediction built its own extend_kwargs (the trend
offset of the model of the forecast periods) and passed it to
MLEResults.get_prediction together with **kwargs, so any call with
extend_kwargs, documented in the shared docstring, raised "TypeError:
MLEResults.get_prediction() got multiple values for keyword argument
'extend_kwargs'", also extend_kwargs={}. get_forecast, forecast and
predict call get_prediction and raised as well.
Changes
- get_prediction takes extend_kwargs and passes it on.
- The trend offset that VARMAX set is dropped: the shared code that builds
the extended model (_get_extension_time_varying_matrices) sets the same
default, trend_offset + nobs, with setdefault. A key given in
extend_kwargs therefore wins over the default, as for every other state
space model and as in VARMAXResults.extend.
Not changed
- Results without extend_kwargs: the predictions, their variances, the
dynamic predictions and the forecasts of 60 VARMAX specifications (each
trend with 0, 1 and 2 exog, two orders, two trend offsets) are identical
to main.
- simulate (of the results and of the model) passed extend_kwargs on
already; with zero shocks it equals the forecast for both an empty
extend_kwargs and a trend_offset.
Tests
- VARMAX(1, 0) with a linear trend, without and with exog, at fixed
parameters: get_forecast, forecast, predict and get_prediction with an
empty extend_kwargs and with a trend_offset equal the forecasts computed
by hand from the VAR recursion (the trend values after the first forecast
come from the trend_offset). Every case fails on main.
The out-of-sample extension of state space models set its defaults with setdefault on the extend_kwargs dict that the caller passed: the trend offset of the extended model (MLEModel, used by VARMAX since statsmodels#11167, and SARIMAX) and validate_specification=False (SARIMAX). Reusing that dict for another model, or for the same model after append/extend, silently used the first model's trend offset. For example, with ek = {}, SARIMAX(y, exog=x[:40], order=(1, 0, 0), trend="t") .smooth([.1, .5, .3, 1]).get_forecast(3, exog=x[40:], extend_kwargs=ek) and then the 30-observation model's forecast(3, exog=x[30:33], extend_kwargs=ek) gave [3.42, 5.46, 6.24] instead of [3.42, 4.46, 4.94]. Stacked on statsmodels#11167, which makes VARMAX pass extend_kwargs on to the shared MLEModel code (before, VARMAX raised for any extend_kwargs). Changes - MLEModel._get_extension_time_varying_matrices and SARIMAX._get_extension_time_varying_matrices copy extend_kwargs before setting their defaults. - A versionchanged note under extend_kwargs in MLEResults.get_prediction (shared by the SARIMAX and VARMAX docstrings). Tests (test_forecasting.py) - SARIMAX with a time trend and exog, and VARMAX with "ct" and exog: the dict passed to get_forecast of a 40-observation model stays empty, and the forecasts of a 30-observation model with that dict equal its forecasts without extend_kwargs. The SARIMAX case fails with the old sarimax.py, the VARMAX case with the old mlemodel.py.
bashtage
force-pushed
the
wip-ss-extend-kwargs-copy
branch
from
November 24, 2026 16:18
6f25d22 to
9c6f2b0
Compare
bashtage
marked this pull request as ready for review
November 24, 2026 18:47
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 #11167, merge that first (its commits are the first ones here; this PR adds one commit): #11167 makes VARMAX pass
extend_kwargson to the shared MLEModel code, which wrote its default trend offset into the caller's dict. mlemodel.py and sarimax.py merge cleanly with every open PR that changes them. #11162 also appends tests at the end of test_forecasting.py: keep both blocks.BUG: Pass extend_kwargs on in VARMAXResults.get_prediction
VARMAXResults.get_prediction built its own extend_kwargs (the trend
offset of the model of the forecast periods) and passed it to
MLEResults.get_prediction together with **kwargs, so any call with
extend_kwargs, documented in the shared docstring, raised "TypeError:
MLEResults.get_prediction() got multiple values for keyword argument
'extend_kwargs'", also extend_kwargs={}. get_forecast, forecast and
predict call get_prediction and raised as well.
Changes
the extended model (_get_extension_time_varying_matrices) sets the same
default, trend_offset + nobs, with setdefault. A key given in
extend_kwargs therefore wins over the default, as for every other state
space model and as in VARMAXResults.extend.
Not changed
dynamic predictions and the forecasts of 60 VARMAX specifications (each
trend with 0, 1 and 2 exog, two orders, two trend offsets) are identical
to main.
already; with zero shocks it equals the forecast for both an empty
extend_kwargs and a trend_offset.
Tests
parameters: get_forecast, forecast, predict and get_prediction with an
empty extend_kwargs and with a trend_offset equal the forecasts computed
by hand from the VAR recursion (the trend values after the first forecast
come from the trend_offset). Every case fails on main.
BUG: Do not write defaults into the caller's extend_kwargs
The out-of-sample extension of state space models set its defaults with
setdefault on the extend_kwargs dict that the caller passed: the trend
offset of the extended model (MLEModel, used by VARMAX since #11167, and
SARIMAX) and validate_specification=False (SARIMAX). Reusing that dict
for another model, or for the same model after append/extend, silently
used the first model's trend offset. For example, with ek = {},
SARIMAX(y, exog=x[:40], order=(1, 0, 0), trend="t")
.smooth([.1, .5, .3, 1]).get_forecast(3, exog=x[40:], extend_kwargs=ek)
and then the 30-observation model's forecast(3, exog=x[30:33],
extend_kwargs=ek) gave [3.42, 5.46, 6.24] instead of [3.42, 4.46, 4.94].
Stacked on #11167, which makes VARMAX pass extend_kwargs on to the
shared MLEModel code (before, VARMAX raised for any extend_kwargs).
Changes
SARIMAX._get_extension_time_varying_matrices copy extend_kwargs before
setting their defaults.
(shared by the SARIMAX and VARMAX docstrings).
Tests (test_forecasting.py)
SARIMAX with a time trend and exog, and VARMAX with "ct" and exog: the
dict passed to get_forecast of a 40-observation model stays empty, and
the forecasts of a 30-observation model with that dict equal its
forecasts without extend_kwargs. The SARIMAX case fails with the old
sarimax.py, the VARMAX case with the old mlemodel.py.
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 the state space code, writing the fix and its test, and checking them (the test fails with each changed file reverted).
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