Skip to content

BUG: Key the autocovariance cache on whether extend_kwargs is given - #11047

Merged
bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-ss-autocov-cache
Nov 27, 2026
Merged

bashtage merged 4 commits into
statsmodels:mainfrom
bashtage:wip-ss-autocov-cache

Conversation

@bashtage

@bashtage bashtage commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Stacked on #11140, 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; #11025 (the C9b coverage chunk) changes docstrings of the same files and appends to the same test files, so whichever merges second needs a rebase. With #11025: in the smoothed_state_autocovariance Returns section take this PR's two lag < -1 bullets (they replace #11025's partial correction of the same bullets); in test_smoothing.py keep both blocks. The other C9F-B PRs that append to test_smoothing.py (smooth arguments, smoother output, autocovariance cache, smoother unreachable code) conflict there too: keep every appended block and the union of the imports.

SmootherResults.smoothed_state_autocovariance caches its results when
extend_kwargs is None or empty, with the key (lag, t, start, end). For
lag < -1 the default end depends on whether extend_kwargs is None: without
it the last -lag - 1 autocovariances, which need the model beyond the
sample, are not computed (the result has nobs + lag + 1 periods), while
with extend_kwargs={} they are computed for a time-invariant model (nobs
periods). Both calls shared the key, so the second call returned the
result of the first, and the shape depended on the order of the calls.

The Returns section also said that for a time-invariant model and
lag < -1 the default result has nobs periods. The code returns
nobs + lag + 1 periods without extend_kwargs for time-invariant and
time-varying models alike.

Changes:

  • The cache key includes whether extend_kwargs is None.
  • The Returns section and the Notes describe what the code returns:
    without extend_kwargs the result has nobs + lag + 1 periods, with it
    nobs; extend_kwargs={} is enough for a time-invariant model, and a
    time-varying model needs -lag - 1 additional matrices.

Not changed: the result of the first call in each case, so each default
keeps the result it gave when called first.

Tests (appended to test_smoothing.py): for lag -2 and -3 and both call
orders, the result without extend_kwargs has nobs + lag + 1 periods and
the result with extend_kwargs={} has nobs, and both equal Cov(alpha_t,
alpha_{t - lag}) from the joint posterior covariance of the states of the
CFA simulation smoother (in the sample) and, beyond the sample, the last
in-sample covariance times powers of the transposed transition matrix.

  • 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

With a block initialization in which some blocks are diffuse and others
known (or stationary), the CFA simulation smoother
(`simulation_smoother(method="cfa")`) depended on the order in which the
blocks were set. The compiled statespace resets `initialized_diffuse` for
each block, so the flag describes only the block set last, and the CFA used
it to treat the whole initial state as diffuse or as known:

- known block set last: it inverted the full initial state covariance
  matrix, which is singular for the diffuse block, and raised
  "LinAlgError: Non-positive-definite initial state covariance matrix";
- diffuse block set last: it gave every state prior precision zero, so the
  known block's mean and covariance were ignored and the posterior mean,
  covariance and draws were silently wrong (for example 0.485 instead of
  the Kalman smoother's 2.091 in a two-state model with a known state of
  mean 3 and variance 0.5).

Changes
- The CFA takes the diffuse states from the diffuse part of the initial
  state covariance matrix (nonzero diagonal). They have prior precision
  zero; the precision of the other states is the inverse of their block of
  the initial state covariance matrix. A non-positive-definite block raises
  LinAlgError as before.
- A whole-state diffuse or known initialization uses the same code as
  before.
- The class docstring no longer says that diffuse initialization is not
  available (an exact diffuse initialization of the whole state already
  worked) and has a `versionchanged` note.

Not changed
- `initialized_diffuse` of the compiled statespace (no longer used by the
  CFA; nothing else reads it).

Tests (test_cfa_simulation_smoothing.py)
- A three-state model with two observed series, for four block layouts
  (diffuse block first or last, a known block of two correlated states, a
  diffuse block between two known ones) and for a whole-state diffuse and
  known initialization: the posterior mean, the draw with zero variates and
  the per-period blocks of the posterior covariance equal the exact diffuse
  Kalman smoother's smoothed state and covariance (rtol 1e-6; the two
  methods agree to about 1e-8 for this model). The smoother first runs with
  a known initialization of all states, so values left from that run would
  show.
- A known block with a singular covariance matrix raises LinAlgError for
  every layout.
SmootherResults.smoothed_state_autocovariance caches its results when
extend_kwargs is None or empty, with the key (lag, t, start, end). For
lag < -1 the default end depends on whether extend_kwargs is None: without
it the last -lag - 1 autocovariances, which need the model beyond the
sample, are not computed (the result has nobs + lag + 1 periods), while
with extend_kwargs={} they are computed for a time-invariant model (nobs
periods). Both calls shared the key, so the second call returned the
result of the first, and the shape depended on the order of the calls.

The Returns section also said that for a time-invariant model and
lag < -1 the default result has nobs periods. The code returns
nobs + lag + 1 periods without extend_kwargs for time-invariant and
time-varying models alike.

Changes:
- The cache key includes whether extend_kwargs is None.
- The Returns section and the Notes describe what the code returns:
  without extend_kwargs the result has nobs + lag + 1 periods, with it
  nobs; extend_kwargs={} is enough for a time-invariant model, and a
  time-varying model needs -lag - 1 additional matrices.

Not changed: the result of the first call in each case, so each default
keeps the result it gave when called first.

Tests (appended to test_smoothing.py): for lag -2 and -3 and both call
orders, the result without extend_kwargs has nobs + lag + 1 periods and
the result with extend_kwargs={} has nobs, and both equal Cov(alpha_t,
alpha_{t - lag}) from the joint posterior covariance of the states of the
CFA simulation smoother (in the sample) and, beyond the sample, the last
in-sample covariance times powers of the transposed transition matrix.
@bashtage
bashtage force-pushed the wip-ss-autocov-cache branch from ae4f96c to 9f23fce Compare November 27, 2026 00:32
@bashtage
bashtage merged commit 9f23fce into statsmodels:main Nov 27, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-ss-autocov-cache branch November 27, 2026 20:09
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