Skip to content

BUG: Do not write defaults into the caller's extend_kwargs - #11169

Merged
bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-ss-extend-kwargs-copy
Nov 24, 2026
Merged

bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-ss-extend-kwargs-copy

Conversation

@bashtage

Copy link
Copy Markdown
Member

Stacked on #11167, merge that first (its commits are the first ones here; this PR adds one commit): #11167 makes VARMAX pass extend_kwargs on 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

  • 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.

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

  • 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.

  • 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 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

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
bashtage force-pushed the wip-ss-extend-kwargs-copy branch from 6f25d22 to 9c6f2b0 Compare November 24, 2026 16:18
@bashtage
bashtage marked this pull request as ready for review November 24, 2026 18:47
@bashtage
bashtage merged commit 3f14d45 into statsmodels:main Nov 24, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-ss-extend-kwargs-copy branch November 25, 2026 16:51
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