Repository navigation
BUG: Fix the CFA simulation smoother with partly diffuse initialization - #11140
Merged
Merged
Conversation
bashtage
force-pushed
the
wip-ss-cfa-mixed-init
branch
from
November 25, 2026 12:14
577b78c to
9ab07a7
Compare
Member
Author
|
Merge order. These open PRs build on this one (their branches contain its commits), so please merge this PR first:
Indented PRs build on the PR above them, so merge them after it. Once this PR is merged, their diffs shrink to their own changes. Each of them also says "Stacked on #..." at the top of its description. Merge-order note generated with Claude Code (updated 2026-11-25). |
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.
bashtage
force-pushed
the
wip-ss-cfa-mixed-init
branch
from
November 27, 2026 00:31
9ab07a7 to
40929e0
Compare
bashtage
marked this pull request as ready for review
November 27, 2026 10:04
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.
#11025 rewrites the "does not yet allow diffuse initialization" bullet of the
CFASimulationSmootherdocstring to say that only a diffuse initialization of the entire state vector works; with this PR that is no longer true: drop the bullet (take this PR's side). In the CFA test file, keep both imports and both appended blocks. Other open statespace PRs also append tests at the end of the same test files: keep both appended blocks.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 theblocks were set. The compiled statespace resets
initialized_diffuseforeach 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:
matrix, which is singular for the diffuse block, and raised
"LinAlgError: Non-positive-definite initial state covariance matrix";
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
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.
before.
available (an exact diffuse initialization of the whole state already
worked) and has a
versionchangednote.Not changed
initialized_diffuseof the compiled statespace (no longer used by theCFA; 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.
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.
Tool(s): Claude Code (Claude Opus 5.5).
Used for: finding the bug in a coverage audit of the compiled state space code, writing the fix and its tests, and checking them (tests failing on main, mutation check with a rebuild of the extension per mutant).
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