Skip to content

BUG: Raise a clear error for the Kalman gain under memory conservation - #11116

Draft
bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-kalman-gain-conserve
Draft

bashtage wants to merge 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-kalman-gain-conserve

Conversation

@bashtage

Copy link
Copy Markdown
Member

No conflict in kalman_filter.py with #11028, #11035-#11037, #11039 or #11071-#11073. Other open statespace PRs also append tests at the end of the same test files: keep both appended blocks.

FilterResults.kalman_gain computes the gain from the predicted state
and forecast error covariance matrices of every period when the filter
did not store it (MEMORY_NO_GAIN). If memory conservation also skipped
storing those matrices (memory_conserve=True, MEMORY_CONSERVE, or
MEMORY_NO_GAIN with MEMORY_NO_PREDICTED_COV or
MEMORY_NO_FORECAST_COV), it read the few matrices that the filter
keeps for its own use as if they were those of the first periods. For an
ARMA(1, 1) with 20 observations it raised "IndexError: index 2 is out of
bounds for axis 2 with size 2", and with 2 observations it returned wrong
values without an error.

Changes

  • kalman_gain raises a ValueError that says that the gain is not
    available if memory conservation was used to avoid storing it and the
    predicted state or forecast error covariance matrices, as predict
    does for in-sample predictions without the arrays it needs.
  • versionchanged note in its docstring.

Not changed

  • The gain stored by the filter, and the gain computed with
    MEMORY_NO_GAIN alone, which uses the stored covariance matrices.

Tests

  • For MEMORY_CONSERVE, and MEMORY_NO_GAIN with
    MEMORY_NO_PREDICTED_COV or with MEMORY_NO_FORECAST_COV, with 2 and 20
    observations, kalman_gain raises the ValueError (all 6 fail on main,
    with IndexError for 20 observations and no error for 2).

  • With MEMORY_NO_GAIN alone, the computed gain equals the gain stored by
    a filter without memory conservation (passes on main too).

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

`FilterResults.kalman_gain` computes the gain from the predicted state
and forecast error covariance matrices of every period when the filter
did not store it (`MEMORY_NO_GAIN`). If memory conservation also skipped
storing those matrices (`memory_conserve=True`, `MEMORY_CONSERVE`, or
`MEMORY_NO_GAIN` with `MEMORY_NO_PREDICTED_COV` or
`MEMORY_NO_FORECAST_COV`), it read the few matrices that the filter
keeps for its own use as if they were those of the first periods. For an
ARMA(1, 1) with 20 observations it raised "IndexError: index 2 is out of
bounds for axis 2 with size 2", and with 2 observations it returned wrong
values without an error.

Changes
- `kalman_gain` raises a ValueError that says that the gain is not
  available if memory conservation was used to avoid storing it and the
  predicted state or forecast error covariance matrices, as `predict`
  does for in-sample predictions without the arrays it needs.
- `versionchanged` note in its docstring.

Not changed
- The gain stored by the filter, and the gain computed with
  `MEMORY_NO_GAIN` alone, which uses the stored covariance matrices.

Tests
- For `MEMORY_CONSERVE`, and `MEMORY_NO_GAIN` with
  `MEMORY_NO_PREDICTED_COV` or with `MEMORY_NO_FORECAST_COV`, with 2 and 20
  observations, `kalman_gain` raises the ValueError (all 6 fail on main,
  with IndexError for 20 observations and no error for 2).
- With `MEMORY_NO_GAIN` alone, the computed gain equals the gain stored by
  a filter without memory conservation (passes on main too).
@bashtage
bashtage force-pushed the wip-ss-kalman-gain-conserve branch from 3a08953 to b5624cd 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