Skip to content

BUG: Fix PredictionResults forecast error covariances and npredictions - #11037

Draft
bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-prediction-forecasts-cov
Draft

bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-prediction-forecasts-cov

Conversation

@bashtage

Copy link
Copy Markdown
Member

Two bugs in statsmodels.tsa.statespace.kalman_filter.PredictionResults
(the result of FilterResults.predict, also held by
MLEResults.get_prediction(...).prediction_results):

  • filtered_forecasts_error_cov and smoothed_forecasts_error_cov
    returned None when read before filtered_forecasts /
    smoothed_forecasts. The properties cache the covariances in
    _filtered_forecasts_cov / _smoothed_forecasts_cov, but __init__
    set _filtered_forecasts_error_cov / _smoothed_forecasts_error_cov
    instead. Reading the unset cache raised AttributeError inside the
    property, so Python fell back to __getattr__, which returned the
    None of the wrongly named attribute.
  • npredictions was start - end, a negative number, instead of the
    number of predicted periods end - start (end is exclusive here).

Changes: __init__ sets the caches the properties use, and
npredictions = end - start, with a versionchanged note in the
docstring.

Not changed: the values of the forecasts and covariances when the means
were read first (the path MLEResults.get_prediction uses), and the
other attributes.

Tests: for the filtered and smoothed information sets, reading the
covariances before or after the means, in sample and with forecasts
beyond the sample, the forecasts equal d + Z a_t and the forecast error
covariances Z P_t Z' + H computed from the predicted states, and
npredictions equals end - start and the number of predicted periods.
All 8 new tests fail on main.

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

Two bugs in `statsmodels.tsa.statespace.kalman_filter.PredictionResults`
(the result of `FilterResults.predict`, also held by
`MLEResults.get_prediction(...).prediction_results`):

- `filtered_forecasts_error_cov` and `smoothed_forecasts_error_cov`
  returned None when read before `filtered_forecasts` /
  `smoothed_forecasts`. The properties cache the covariances in
  `_filtered_forecasts_cov` / `_smoothed_forecasts_cov`, but `__init__`
  set `_filtered_forecasts_error_cov` / `_smoothed_forecasts_error_cov`
  instead. Reading the unset cache raised AttributeError inside the
  property, so Python fell back to `__getattr__`, which returned the
  None of the wrongly named attribute.
- `npredictions` was `start - end`, a negative number, instead of the
  number of predicted periods `end - start` (`end` is exclusive here).

Changes: `__init__` sets the caches the properties use, and
`npredictions = end - start`, with a `versionchanged` note in the
docstring.

Not changed: the values of the forecasts and covariances when the means
were read first (the path `MLEResults.get_prediction` uses), and the
other attributes.

Tests: for the filtered and smoothed information sets, reading the
covariances before or after the means, in sample and with forecasts
beyond the sample, the forecasts equal d + Z a_t and the forecast error
covariances Z P_t Z' + H computed from the predicted states, and
`npredictions` equals `end - start` and the number of predicted periods.
All 8 new tests fail on main.
@bashtage
bashtage force-pushed the wip-ss-prediction-forecasts-cov branch from 3505d17 to c9a604e 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