Repository navigation
BUG: Key the autocovariance cache on whether extend_kwargs is given - #11047
Merged
Merged
Conversation
This was referenced Nov 23, 2026
bashtage
force-pushed
the
wip-ss-autocov-cache
branch
from
November 25, 2026 12:12
ffe5346 to
ae4f96c
Compare
This was referenced Nov 25, 2026
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
force-pushed
the
wip-ss-autocov-cache
branch
from
November 27, 2026 00:32
ae4f96c to
9f23fce
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_autocovarianceReturns section take this PR's twolag < -1bullets (they replace #11025's partial correction of the same bullets); intest_smoothing.pykeep both blocks. The other C9F-B PRs that append totest_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:
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.
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.
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