Skip to content

BUG: Detect indefinite observation covariances in the univariate LDL - #11071

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

bashtage merged 2 commits into
statsmodels:mainfrom
bashtage:wip-ss-ldl-definiteness

Conversation

@bashtage

Copy link
Copy Markdown
Member

Univariate filtering (filter_univariate=True, the exact diffuse periods, and
the fallback of the conventional filter when the forecast error covariance
is singular) diagonalizes a non-diagonal obs_cov with the LDL decomposition
in _tools.pyx.in. Its check of definiteness was wrong:

  • It tested the sign of the original diagonal element A[j, j] instead of
    the pivot d(j) after the update, so a negative pivot (an indefinite
    matrix) went unnoticed: [[1, 2], [2, 1]] gave a loglikelihood of -8.07e34
    with univariate filtering, and -2.55e14 with the conventional filter,
    which falls back to it.
  • It reported a negative first diagonal element as info = -j = 0, that is,
    as positive definite: [[-1, 0.5], [0.5, 1]] gave -51454.
  • A zero diagonal element was taken as a zero pivot whatever the rest of
    its column, and the "zero the column" statement had no assignment:
    [[1, 0.5], [0.5, 0]] and [[0, 1], [1, 1]], both indefinite, only warned
    that the matrix is positive semi-definite, and gave -204 and -241.
  • A rank-deficient positive semi-definite matrix did not warn, and an
    exactly singular one such as u u' with u = (1, 2, 3) raised
    ZeroDivisionError (0 / 0 in the update of the column).

Changes

  • A pivot is compared with the original diagonal element, with the relative
    tolerance 1e-10, instead of comparing the diagonal element with the
    absolute tolerance 1e-15.
  • A negative diagonal element, or a pivot below -1e-10 * A[j, j], means
    that the matrix is indefinite: info = -(j + 1), which raises the existing
    LinAlgError "Invalid value in LDL factorization of observation covariance
    matrix".
  • A pivot with |d(j)| <= 1e-10 * A[j, j] whose column below, after the
    update, is below 1e-10 * sqrt(A[i, i] * A[j, j]) is a zero pivot: that
    column is set to zero (a positive pivot itself is kept), and the existing
    positive semi-definite warning is issued.
  • If the column below such a pivot is not small, a zero or negative pivot
    means that the matrix is indefinite, and a positive one is used as an
    ordinary pivot (an ill-conditioned positive definite matrix).
  • The Notes of set_filter_method describe the warning and the error
    (versionchanged 0.15.1).

Not changed

  • Positive definite matrices keep their results. Their pivots are processed
    with the same operations in the same order as before, also for
    ill-conditioned ones: L D L' with L = [[1, 0, 0], [0.3, 1, 0],
    [0.2, 5e5, 1]] and D = diag(1, 1e-12, 0.75) (condition number 2e12)
    gives the same loglikelihood, without a warning. The exception is a
    pivot below 1e-10 times its diagonal element whose column below is also
    below the tolerance (condition number above about 1e10): such a matrix
    now warns that it is positive semi-definite. If the pivot is the last
    one, the result is unchanged; otherwise the small column is set to zero,
    and the factorization still reproduces the matrix to about 1e-10
    (relative). Only matrices too ill-conditioned for double precision
    (condition number above about 1e16) can come out indefinite and raise;
    before, these mostly produced a negative variance.
  • The texts of the warning and of the LinAlgError.

Tests

  • test_univariate_psd_obs_cov: rank-1 (2x2 and 3x3) and rank-2 matrices,
    among them three u u' with u of shape (3, 2) whose last pivot rounds to
    about -1e-15 times its diagonal element (main gave the conventional
    loglikelihood for them, without a warning), a matrix with a zero row, and
    [[1, 1], [1, 1 + 1e-12]]: univariate filtering warns and gives the
    loglikelihood of the conventional filter, which does not decompose
    obs_cov, with float64 and with complex-step (complex128) filtering. All
    but the zero row fail on main.

  • test_univariate_pd_obs_cov: positive definite matrices, including
    [[1, 1], [1, 1 + 1e-8]] and [[1e-11, 1e-9], [1e-9, 1]] (the tolerance is
    relative), do not warn and give the conventional loglikelihood, as on
    main.

  • test_univariate_ill_conditioned_pd_obs_cov: the L D L' example above
    gives main's loglikelihood (-297.21603787944184) without a warning or an
    error, and agrees with the conventional filter to the precision its
    conditioning allows.

  • test_univariate_small_last_pivot_kept: a positive definite obs_cov whose
    last pivot (5e-11) is below the tolerance warns and keeps the pivot. That
    matters here, because the states add little variance in that direction
    (setting the pivot to zero would change the loglikelihood by 4%); it
    agrees with the conventional filter. It fails on main, which does not
    warn.

  • test_univariate_indefinite_obs_cov_raises: a negative pivot, a negative
    first or second diagonal element, a zero diagonal element, and zero
    pivots with a nonzero column (2x2, 3x3, and a rescaled 3x3) raise the
    LinAlgError. All but the negative second diagonal element, which raised
    on main, fail on main.

  • The existing tests of a non-diagonal obs_cov use X X' with X of rank 2,
    so univariate filtering warns for it now; they expect the warning.

  • 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

Univariate filtering (filter_univariate=True, the exact diffuse periods, and
the fallback of the conventional filter when the forecast error covariance
is singular) diagonalizes a non-diagonal obs_cov with the LDL decomposition
in _tools.pyx.in. Its check of definiteness was wrong:
- It tested the sign of the original diagonal element A[j, j] instead of
  the pivot d(j) after the update, so a negative pivot (an indefinite
  matrix) went unnoticed: [[1, 2], [2, 1]] gave a loglikelihood of -8.07e34
  with univariate filtering, and -2.55e14 with the conventional filter,
  which falls back to it.
- It reported a negative first diagonal element as info = -j = 0, that is,
  as positive definite: [[-1, 0.5], [0.5, 1]] gave -51454.
- A zero diagonal element was taken as a zero pivot whatever the rest of
  its column, and the "zero the column" statement had no assignment:
  [[1, 0.5], [0.5, 0]] and [[0, 1], [1, 1]], both indefinite, only warned
  that the matrix is positive semi-definite, and gave -204 and -241.
- A rank-deficient positive semi-definite matrix did not warn, and an
  exactly singular one such as u u' with u = (1, 2, 3) raised
  ZeroDivisionError (0 / 0 in the update of the column).

Changes
- A pivot is compared with the original diagonal element, with the relative
  tolerance 1e-10, instead of comparing the diagonal element with the
  absolute tolerance 1e-15.
- A negative diagonal element, or a pivot below -1e-10 * A[j, j], means
  that the matrix is indefinite: info = -(j + 1), which raises the existing
  LinAlgError "Invalid value in LDL factorization of observation covariance
  matrix".
- A pivot with |d(j)| <= 1e-10 * A[j, j] whose column below, after the
  update, is below 1e-10 * sqrt(A[i, i] * A[j, j]) is a zero pivot: that
  column is set to zero (a positive pivot itself is kept), and the existing
  positive semi-definite warning is issued.
- If the column below such a pivot is not small, a zero or negative pivot
  means that the matrix is indefinite, and a positive one is used as an
  ordinary pivot (an ill-conditioned positive definite matrix).
- The Notes of set_filter_method describe the warning and the error
  (versionchanged 0.15.1).

Not changed
- Positive definite matrices keep their results. Their pivots are processed
  with the same operations in the same order as before, also for
  ill-conditioned ones: L D L' with L = [[1, 0, 0], [0.3, 1, 0],
  [0.2, 5e5, 1]] and D = diag(1, 1e-12, 0.75) (condition number 2e12)
  gives the same loglikelihood, without a warning. The exception is a
  pivot below 1e-10 times its diagonal element whose column below is also
  below the tolerance (condition number above about 1e10): such a matrix
  now warns that it is positive semi-definite. If the pivot is the last
  one, the result is unchanged; otherwise the small column is set to zero,
  and the factorization still reproduces the matrix to about 1e-10
  (relative). Only matrices too ill-conditioned for double precision
  (condition number above about 1e16) can come out indefinite and raise;
  before, these mostly produced a negative variance.
- The texts of the warning and of the LinAlgError.

Tests
- test_univariate_psd_obs_cov: rank-1 (2x2 and 3x3) and rank-2 matrices,
  among them three u u' with u of shape (3, 2) whose last pivot rounds to
  about -1e-15 times its diagonal element (main gave the conventional
  loglikelihood for them, without a warning), a matrix with a zero row, and
  [[1, 1], [1, 1 + 1e-12]]: univariate filtering warns and gives the
  loglikelihood of the conventional filter, which does not decompose
  obs_cov, with float64 and with complex-step (complex128) filtering. All
  but the zero row fail on main.
- test_univariate_pd_obs_cov: positive definite matrices, including
  [[1, 1], [1, 1 + 1e-8]] and [[1e-11, 1e-9], [1e-9, 1]] (the tolerance is
  relative), do not warn and give the conventional loglikelihood, as on
  main.
- test_univariate_ill_conditioned_pd_obs_cov: the L D L' example above
  gives main's loglikelihood (-297.21603787944184) without a warning or an
  error, and agrees with the conventional filter to the precision its
  conditioning allows.
- test_univariate_small_last_pivot_kept: a positive definite obs_cov whose
  last pivot (5e-11) is below the tolerance warns and keeps the pivot. That
  matters here, because the states add little variance in that direction
  (setting the pivot to zero would change the loglikelihood by 4%); it
  agrees with the conventional filter. It fails on main, which does not
  warn.
- test_univariate_indefinite_obs_cov_raises: a negative pivot, a negative
  first or second diagonal element, a zero diagonal element, and zero
  pivots with a nonzero column (2x2, 3x3, and a rescaled 3x3) raise the
  LinAlgError. All but the negative second diagonal element, which raised
  on main, fail on main.
- The existing tests of a non-diagonal obs_cov use X X' with X of rank 2,
  so univariate filtering warns for it now; they expect the warning.
@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.

@bashtage
bashtage marked this pull request as ready for review November 23, 2026 21:11
@bashtage
bashtage merged commit 89ab034 into statsmodels:main Nov 23, 2026
11 of 12 checks passed
@bashtage
bashtage deleted the wip-ss-ldl-definiteness 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