Skip to content

BUG: Divide the Lyapunov solution by the trsyl scale factor - #11027

Merged
bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-lyapunov-scale
Nov 23, 2026
Merged

bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-lyapunov-scale

Conversation

@bashtage

Copy link
Copy Markdown
Member

The compiled solver of the discrete Lyapunov equation, which computes the
stationary initial state covariance of state space models (stationary
initialization, initialize_stationary()), solves a Sylvester equation
with LAPACK's ?trsyl. ?trsyl returns X with A X + X B = scale * C, where
scale <= 1 is chosen to avoid overflow, so the solution for C is
X / scale. The code multiplied by scale instead. scale is 1 unless the
solution is close to overflowing, so only very large state covariances
were affected: for a VAR(1) transition [[0.5, 0.1], [0, 0.3]] and
state_cov = s * I, the stationary covariance was about zero for s of
about 1e294 or more (P0[0, 0] = 1.18e-292 instead of 1.35e295 for
s = 1e295), and the filter and loglikelihood started from that
covariance.

For the complex dtypes, the old code also passed the address of the real
scale factor where BLAS expects a complex number, so it read past it (the
result had a spurious imaginary part).

Changes

  • Divide the ?trsyl solution by scale, passing a scalar of the matrix
    dtype to ?scal.

Not changed

  • Results when ?trsyl does not rescale, which is the case unless the
    solution is close to overflow.
  • The Python tools.solve_discrete_lyapunov, which calls
    scipy.linalg.solve_sylvester; scipy multiplies by scale as well.

Tests

  • KalmanFilter.initialize_stationary() for float64 and complex128
    models with state_cov = s * I, s = 1, 1e295 and 1e300: the initial
    state covariance divided by s equals the closed form
    vec(P) = (I - T kron T)^{-1} vec(I). The cases with s >= 1e295 fail
    without the fix.

  • 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

The compiled solver of the discrete Lyapunov equation, which computes the
stationary initial state covariance of state space models (stationary
initialization, `initialize_stationary()`), solves a Sylvester equation
with LAPACK's ?trsyl. ?trsyl returns X with A X + X B = scale * C, where
scale <= 1 is chosen to avoid overflow, so the solution for C is
X / scale. The code multiplied by scale instead. scale is 1 unless the
solution is close to overflowing, so only very large state covariances
were affected: for a VAR(1) transition [[0.5, 0.1], [0, 0.3]] and
state_cov = s * I, the stationary covariance was about zero for s of
about 1e294 or more (P0[0, 0] = 1.18e-292 instead of 1.35e295 for
s = 1e295), and the filter and loglikelihood started from that
covariance.

For the complex dtypes, the old code also passed the address of the real
scale factor where BLAS expects a complex number, so it read past it (the
result had a spurious imaginary part).

Changes
- Divide the ?trsyl solution by scale, passing a scalar of the matrix
  dtype to ?scal.

Not changed
- Results when ?trsyl does not rescale, which is the case unless the
  solution is close to overflow.
- The Python `tools.solve_discrete_lyapunov`, which calls
  `scipy.linalg.solve_sylvester`; scipy multiplies by scale as well.

Tests
- `KalmanFilter.initialize_stationary()` for float64 and complex128
  models with state_cov = s * I, s = 1, 1e295 and 1e300: the initial
  state covariance divided by s equals the closed form
  vec(P) = (I - T kron T)^{-1} vec(I). The cases with s >= 1e295 fail
  without the fix.
@bashtage
bashtage force-pushed the wip-ss-lyapunov-scale branch from f16cb08 to 2f99c5d Compare November 23, 2026 19:14
@bashtage
bashtage marked this pull request as ready for review November 23, 2026 20:04
@bashtage
bashtage merged commit 76c6006 into statsmodels:main Nov 23, 2026
10 of 11 checks passed
@bashtage
bashtage deleted the wip-ss-lyapunov-scale branch November 24, 2026 18:28
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