Skip to content

TST: Cover the statespace smoothers, simulation smoothers and tools - #11025

Merged
bashtage merged 5 commits into
statsmodels:mainfrom
bashtage:wip-tsa-cov-c9b
Nov 27, 2026
Merged

bashtage merged 5 commits into
statsmodels:mainfrom
bashtage:wip-tsa-cov-c9b

Conversation

@bashtage

@bashtage bashtage commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11051, which is stacked on #11113, #11044, #11047 and #11140: merge those first (this PR adds four commits). 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.

This adds tests for the untested Python paths of statespace/kalman_smoother.py, simulation_smoother.py, cfa_simulation_smoother.py and tools.py, and for the float64 Cython smoother and simulation smoother paths that the Python layer reaches (the C14 Cython coverage audit), and makes their docstrings match the code. Every assertion compares with an independent value: another smoother method or configuration on the same model, the Kalman smoother for the CFA simulation smoother, a closed form, or an identity. The source changes are docstrings only.

Changes

  • Tests, smoothers: exact diffuse initialization with the alternative and classical smoothers against the conventional smoother; the classical smoother with a singular predicted state covariance; errors for smoothing after conserving memory, for univariate smoothing after the conventional filter, and for an invalid method; subsets of the smoother output; a custom results class.
  • Tests, SmootherResults: default and invalid periods of news() and smoothed_state_gain(), against two smoother runs; impacts and decompositions from a partition of the states add up; the default end of forward autocovariances in a time-varying model.
  • Tests, simulation smoothers: results classes, output options, generated series against the recursions, nobs errors, a model whose dimensions changed.
  • Tests, CFA: exact diffuse initialization against the Kalman smoother; a single observation and singular covariances raise; the posterior covariance and a draw without updating the posterior; a pickle round trip.
  • Tests, tools: companion_matrix with a non-identity leading matrix; concat; the defaults of the pure-Python twins against the compiled routines; prefix arguments; reorder/copy argument errors; compute_smoothed_state_weights without period 0; Initialization.from_components with scalar and vector input.
  • Docstrings: the default smoother output and smoothing method; the set_smooth_method examples (doctest-checked); smoothing_error; the sign of lag in smoothed_state_autocovariance; the deprecated random_state entries; the shape of the initial state variates; CFA diffuse initialization; the is_invertible default; the companion_matrix formula; the return types of concat, diff and is_invertible; array-only parameters; the copy-helper summaries.

Not changed

  • Bugs found while testing are fixed separately, and no new test asserts their current behaviour: smooth() drops smooth_method and complex_step; smooth(smoother_output=...) and some output subsets give wrong values; state_index in the signal decomposition; the cache of smoothed_state_autocovariance; compute_smoothed_state_weights on filtered results; the CFA posterior before simulate(); ndarray matrix polynomials in companion_matrix; the returned variance of constrain_stationary_multivariate; set_smooth_method(smooth_univariate=True); SmootherResults.smoother_output.
  • Pickling of the KFS simulation smoother and the LDL definiteness checks have their own PRs.

Tests

  • statsmodels/tsa/statespace: 3520 passed, 87 skipped, 34 xfailed, no warnings.

  • Coverage (lines / branches): kalman_smoother.py 93.5/84.8% to 98.1/96.2%; simulation_smoother.py 97.3/82.3% to 100/100%; cfa_simulation_smoother.py 98.0/71.4% to 98.0/92.9%; tools.py 93.7/83.6% to 96.7/92.0%. What remains is reachable only through one of the bugs above, or is listed as a removal candidate.

  • Cython (measured together with TST: Cover the statespace representation, Kalman filter and initialization #11028 on a -Dcython-coverage=true build, Linux, Python 3.12): the tests of the two PRs reach 143 lines of the compiled filters, smoothers, tools, innovations and STL that the full TSA test suite did not; this PR's share is mainly _cfa_simulation_smoother.pyx (+48), _kalman_smoother.pyx (+24) and the smoothers and _tools.pyx argument checks.

  • Mutation check: 35 single-line changes to newly covered lines, all caught.

  • 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: auditing the coverage gaps and docstrings of the statespace smoothers and tools, writing the tests and docstring fixes, and checking them (coverage, 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-tsa-cov-c9b branch 2 times, most recently from 9245421 to 173ba5e Compare November 23, 2026 22:10
@bashtage
bashtage force-pushed the wip-tsa-cov-c9b branch 3 times, most recently from 3dc779a to 8757189 Compare November 25, 2026 12:12
Add tests for the untested paths of kalman_smoother.py,
simulation_smoother.py, cfa_simulation_smoother.py and tools.py, and for
the float64 Cython smoother and simulation smoother paths that the Python
layer reaches. Every assertion compares with an independent value: another
smoother method or configuration on the same model, the Kalman smoother
for the CFA simulation smoother, a closed form, or an identity.

- Smoothers: exact diffuse initialization with the alternative and
  classical smoothers against the conventional smoother; the classical
  smoother with a singular predicted state covariance; smoothing after
  conserving memory, univariate smoothing after the conventional filter and
  an invalid smoothing method raise; subsets of the smoother output equal
  the full output; a custom results class.
- SmootherResults: news() and smoothed_state_gain() with their default
  periods (checked against the change of the smoothed forecasts and
  states between two smoother runs), invalid periods, and a partition of
  the states (impacts and decompositions add up); the default end of
  smoothed_state_autocovariance() for a negative lag in a time-varying
  model.
- Simulation smoothers: custom and invalid results classes, the
  simulation output options, generated series against the state space
  recursions, the nobs errors and a model whose dimensions changed.
- CFA simulation smoother: exact diffuse initialization against the
  Kalman smoother, a single observation and singular covariance matrices
  raise, the posterior covariance and a draw without updating the
  posterior, and a pickle round trip.
- tools: companion_matrix with a non-identity leading matrix, concat with
  scalars, Series and invalid input, the defaults of the pure-Python twin
  helpers against the compiled routine, the prefix of
  constrain_stationary_multivariate and the copy helpers, the argument
  errors of the reorder and copy helpers, compute_smoothed_state_weights
  without period 0, and Initialization.from_components with scalar and
  vector input.
Make the docstrings of kalman_smoother.py, simulation_smoother.py,
cfa_simulation_smoother.py and tools.py match the code.

- KalmanSmoother: the default smoother output is 31; set_smooth_method
  describes the smoothing method (not the filter method), its default of 0
  and has working examples; the `prefix` of smooth() is optional.
- SmootherResults: `smoothing_error` is the smoothing error vector, not a
  covariance matrix; the shape and number of extension matrices for
  forward autocovariances in smoothed_state_autocovariance had the sign of
  `lag` wrong; smoothed_state_gain accepts any sequence of indices.
- Simulation smoother: the deprecated `random_state` keyword was
  documented as a second `rng`; the Cython simulation smoother parameter
  showed a template placeholder; the initial state variates are
  one-dimensional; get_simulation_output gets a Returns section.
- CFA simulation smoother: exact diffuse initialization of the whole state
  vector is supported.
- tools: the default threshold of is_invertible, the formula of
  companion_matrix for a non-identity leading matrix, the return types of
  concat, diff and is_invertible, `a` of solve_discrete_lyapunov and
  `polynomial_trend` of prepare_trend_data must be arrays, `nobs` of the
  shape validators may be None, the summaries and parameter references of
  the copy helpers, and a reference to a parameter that
  unconstrain_stationary_multivariate does not have.
This branch is now stacked on statsmodels#11051, which makes
constrain_stationary_multivariate (and its public Python twin) return the
input variance with transform_variance=False. The private helpers of the
twin still return the variance of the internal recursion, so
test_python_twin_helpers_default_dimensions failed for
transform_variance=False once both were merged (found by merging all open
branches and running the test suite). The coefficients are compared in
both cases and the variance with transform_variance=True.
@bashtage
bashtage marked this pull request as ready for review November 27, 2026 10:06
@bashtage
bashtage merged commit 05c8554 into statsmodels:main Nov 27, 2026
21 of 22 checks passed
@bashtage
bashtage deleted the wip-tsa-cov-c9b branch November 27, 2026 10:06
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