Skip to content

BUG: Fix the CFA simulation smoother with partly diffuse initialization - #11140

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-cfa-mixed-init
Nov 27, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-cfa-mixed-init

Conversation

@bashtage

Copy link
Copy Markdown
Member

#11025 rewrites the "does not yet allow diffuse initialization" bullet of the CFASimulationSmoother docstring 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 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.

  • 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 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

@bashtage
bashtage force-pushed the wip-ss-cfa-mixed-init branch from 577b78c to 9ab07a7 Compare November 25, 2026 12:14
@bashtage

Copy link
Copy Markdown
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).

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
bashtage force-pushed the wip-ss-cfa-mixed-init branch from 9ab07a7 to 40929e0 Compare November 27, 2026 00:31
@bashtage
bashtage marked this pull request as ready for review November 27, 2026 10:04
@bashtage
bashtage merged commit a83eea9 into statsmodels:main Nov 27, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-ss-cfa-mixed-init 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