Repository navigation
BUG: Divide the Lyapunov solution by the trsyl scale factor - #11027
Merged
Merged
Conversation
This was referenced Nov 23, 2026
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
force-pushed
the
wip-ss-lyapunov-scale
branch
from
November 23, 2026 19:14
f16cb08 to
2f99c5d
Compare
bashtage
marked this pull request as ready for review
November 23, 2026 20: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.
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 equationwith 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
dtype to ?scal.
Not changed
solution is close to overflow.
tools.solve_discrete_lyapunov, which callsscipy.linalg.solve_sylvester; scipy multiplies by scale as well.Tests
KalmanFilter.initialize_stationary()for float64 and complex128models 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.
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