Skip to content

BUG: Use the filter's options in results and concentrated loglike - #11036

Draft
bashtage wants to merge 4 commits into
statsmodels:mainfrom
bashtage:wip-ss-filter-option-flags
Draft

bashtage wants to merge 4 commits into
statsmodels:mainfrom
bashtage:wip-ss-filter-option-flags

Conversation

@bashtage

@bashtage bashtage commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11035, merge that first (this PR adds one commit). The stack was rebuilt on 2026-11-25 so that it merges without conflicts in this order; notes about conflicts with PRs of the same stack no longer apply.

Based on main; #11028 (the C9a coverage chunk) changes docstrings of the same file and appends to the same test files, so whichever merges second needs a rebase. With #11028: keep its block at the end of test_options.py, then this one. The versionchanged in filter()'s Notes also conflicts with the filter_timing/tolerance PR: keep both paragraphs under one directive.

Options given for one call to KalmanFilter.filter, loglike,
loglikeobs or smooth reach the compiled filter, but the results and
the concentrated-likelihood adjustment read the boolean options of the
model instead:

  • FilterResults.update_filter copied the bitmasks (filter_method,
    inversion_method, ...) from the compiled filter, but kept the boolean
    flags (filter_concentrated, invert_lu, filter_collapsed,
    memory_no_*, ...) that update_representation had read from the
    model, and the output is built from those flags. So
    filter(inversion_method=INVERT_LU).standardized_forecasts_error was
    all zeros (the compiled filter does not compute it for LU, and the
    Python fallback was skipped), filter(filter_method=FILTER_CONVENTIONAL | FILTER_CONCENTRATED) returned a loglikelihood without the scale
    terms (-46.09 instead of -64.85 for an AR(1)) and scale 1,
    the reverse (a concentrated model filtered with filter_method= FILTER_CONVENTIONAL) returned nan, and FILTER_COLLAPSED given as an
    argument returned forecasts of the collapsed (k_states) series.
  • loglike and loglikeobs decided whether to concentrate the scale
    from the model's filter_method, so loglike(filter_method=... | FILTER_CONCENTRATED) was wrong in the same way.

Changes:

  • update_filter sets the boolean options from the bitmasks of the
    compiled filter (the same descriptors the model uses), right after
    copying the bitmasks.
  • loglike and loglikeobs test FILTER_CONCENTRATED on the compiled
    filter's filter_method; loglikeobs picks its default
    conserve_memory from the filter_method it is given.
  • versionchanged note in KalmanFilter.filter.

Not changed: results of options set on the model (the flags of the model
and of the compiled filter agree then); the fixed-scale override of
filter_concentrated in update_filter; smooth(filter_method=...),
which still raises TypeError in KalmanSmoother._initialize_smoother
(filter_method is passed twice; fixed separately).

Tests: for INVERT_LU, SOLVE_LU, FILTER_CONCENTRATED (known and exact
diffuse initialization), removing FILTER_CONCENTRATED from a concentrated
model, and FILTER_COLLAPSED, the output of the option given to filter
(and, for the inversion methods, to smooth) equals the output with the
option set on the model: every boolean option, llf, scale, filtered
states and covariances, forecasts, forecast errors and their
covariances, standardized forecast errors and smoothed state
covariances. loglike and loglikeobs with the concentrated cases equal
the model-option values and the filter's llf_obs. All 14 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

@bashtage
bashtage force-pushed the wip-ss-filter-option-flags branch from 6d4cdd0 to 29759e9 Compare November 24, 2026 11:48
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
The `versionchanged` note of the `complex_step` argument also says that
the results no longer initialize the state again without complex step,
which made the derivatives of the first period of the smoothed state and
of `initial_state_cov` wrong. `KalmanFilter.filter`, whose results had the
wrong `initial_state_cov` too, gets no note here because statsmodels#11028, statsmodels#11035
and statsmodels#11036 change its docstring and code next to `complex_step`; the
release note covers it.
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 25, 2026
The `versionchanged` note of the `complex_step` argument also says that
the results no longer initialize the state again without complex step,
which made the derivatives of the first period of the smoothed state and
of `initial_state_cov` wrong. `KalmanFilter.filter`, whose results had the
wrong `initial_state_cov` too, gets no note here because statsmodels#11028, statsmodels#11035
and statsmodels#11036 change its docstring and code next to `complex_step`; the
release note covers it.
@bashtage
bashtage force-pushed the wip-ss-filter-option-flags branch from 29759e9 to b8d6b78 Compare November 25, 2026 12:12
bashtage added a commit to bashtage/statsmodels that referenced this pull request Nov 27, 2026
The `versionchanged` note of the `complex_step` argument also says that
the results no longer initialize the state again without complex step,
which made the derivatives of the first period of the smoothed state and
of `initial_state_cov` wrong. `KalmanFilter.filter`, whose results had the
wrong `initial_state_cov` too, gets no note here because statsmodels#11028, statsmodels#11035
and statsmodels#11036 change its docstring and code next to `complex_step`; the
release note covers it.
`KalmanFilter._filter` passed its options positionally to
`_initialize_filter`, whose signature has `tolerance` before
`filter_timing`, so the two were swapped. `filter(filter_timing=1)` ran
with the model's timing and a convergence tolerance of 1 (llf -76.37
instead of -76.27 for an AR(1) with an approximate diffuse
initialization), and `filter(tolerance=1e-4)` ran with the model's
tolerance (1e-19) and the tolerance as the timing. `loglike`,
`loglikeobs` and `smooth` pass these options through `_filter`, so they
were affected in the same way.

Changes: `_filter` passes every option to `_initialize_filter` by
keyword, and `KalmanFilter.filter` has a `versionchanged` note.

Not changed: options set on the model, and the other options given to
`filter` (they were already passed in the right positions).

Tests: `filter_timing=1` and `tolerance=1e-4` given to `filter`, `smooth`,
`loglike` and `loglikeobs` give the output of the same option set on the
model (results options, period of convergence, llf, filtered and smoothed
states, loglikelihood values), and the options change the loglikelihood
(and a looser tolerance converges earlier), so that the comparison is
not vacuous. A second test checks that each of the seven options given
to `filter` reaches the compiled filter and matches the option set on
the model. 10 of the 15 new tests fail on main; the other five guard the
options that were already passed correctly.
Options given for one call to `KalmanFilter.filter`, `loglike`,
`loglikeobs` or `smooth` reach the compiled filter, but the results and
the concentrated-likelihood adjustment read the boolean options of the
model instead:

- `FilterResults.update_filter` copied the bitmasks (`filter_method`,
  `inversion_method`, ...) from the compiled filter, but kept the boolean
  flags (`filter_concentrated`, `invert_lu`, `filter_collapsed`,
  `memory_no_*`, ...) that `update_representation` had read from the
  model, and the output is built from those flags. So
  `filter(inversion_method=INVERT_LU).standardized_forecasts_error` was
  all zeros (the compiled filter does not compute it for LU, and the
  Python fallback was skipped), `filter(filter_method=FILTER_CONVENTIONAL
  | FILTER_CONCENTRATED)` returned a loglikelihood without the scale
  terms (-46.09 instead of -64.85 for an AR(1)) and scale 1,
  the reverse (a concentrated model filtered with `filter_method=
  FILTER_CONVENTIONAL`) returned nan, and `FILTER_COLLAPSED` given as an
  argument returned forecasts of the collapsed (k_states) series.
- `loglike` and `loglikeobs` decided whether to concentrate the scale
  from the model's `filter_method`, so `loglike(filter_method=... |
  FILTER_CONCENTRATED)` was wrong in the same way.

Changes:
- `update_filter` sets the boolean options from the bitmasks of the
  compiled filter (the same descriptors the model uses), right after
  copying the bitmasks.
- `loglike` and `loglikeobs` test FILTER_CONCENTRATED on the compiled
  filter's `filter_method`; `loglikeobs` picks its default
  `conserve_memory` from the `filter_method` it is given.
- `versionchanged` note in `KalmanFilter.filter`.

Not changed: results of options set on the model (the flags of the model
and of the compiled filter agree then); the fixed-scale override of
`filter_concentrated` in `update_filter`; `smooth(filter_method=...)`,
which still raises TypeError in `KalmanSmoother._initialize_smoother`
(filter_method is passed twice; fixed separately).

Tests: for INVERT_LU, SOLVE_LU, FILTER_CONCENTRATED (known and exact
diffuse initialization), removing FILTER_CONCENTRATED from a concentrated
model, and FILTER_COLLAPSED, the output of the option given to `filter`
(and, for the inversion methods, to `smooth`) equals the output with the
option set on the model: every boolean option, llf, scale, filtered
states and covariances, forecasts, forecast errors and their
covariances, standardized forecast errors and smoothed state
covariances. `loglike` and `loglikeobs` with the concentrated cases equal
the model-option values and the filter's `llf_obs`. All 14 new tests
fail on main.
@bashtage
bashtage force-pushed the wip-ss-filter-option-flags branch from b8d6b78 to cc4d42f 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