Skip to content

Preserve the triangular block in the tria JVP at rank deficiency - #288

Draft
AdrienCorenflos wants to merge 1 commit into
mainfrom
block-tria-jvp
Draft

AdrienCorenflos wants to merge 1 commit into
mainfrom
block-tria-jvp

Conversation

@AdrienCorenflos

Copy link
Copy Markdown
Contributor

Problem

The current custom JVP of tria (introduced in #213 for #211) returns a tangent dL that satisfies the Gramian identity
dL L^T + L dL^T = dA A^T + A dA^T at any rank, so gradients of quantities that depend on L only through L L^T are exact.

At a rank-deficient input, however, this tangent is in general not lower triangular: its null-space term (I - L L^+) dA Q has entries above the diagonal.
The Kalman update reads tria(M) blockwise: it uses the leading n_y x n_y block as a triangular factor (solve_triangular, log-determinant from the diagonal) and never reads the upper-right block.
Those entries of the tangent are therefore discarded, so the update is not a function of L L^T along that tangent, and the gradient is silently wrong.
The filter values are unaffected.

This happens, for example, when the observation noise is singular and its column space rotates with a parameter (chol_R = [[1, 0], [c, 0]], derivative with respect to c): the gradient of the log-likelihood from predict/update is off by a few percent, while the values match the covariance-form filter to machine precision.

Fix

tria gets a static argument n_lower (default 0) giving the size of the leading block that is used downstream as a triangular factor.
With A = L Q^T (thin QR of A^T), G = dA Q, and phi(X) = tril(X) - diag(X) / 2, the tangent is

  • dL11 = L11 phi(K11 + K11^T), with K11 = L11^{-1} G11 (classical rule, lower triangular),
  • dL12 = 0,
  • dL21 = G21 - L21 W + L22 K12^T, with K12 = L11^{-1} G12 and W = K11 - phi(K11 + K11^T) (skew-symmetric),
  • dL22 = the previous pseudoinverse/null-space tangent applied to L22 and G22 - L21 K12.

Each block of the Gramian identity holds exactly, at any rank of L22.
n_lower = 0 is exactly the previous tangent, and n_lower = n with L invertible is the classical QR rule.
L11 L11^T must be positive definite, as already required by the consumers (innovation covariance).

n_lower is set where the leading block is read as a triangular factor:

  • kalman.filtering.update (n_y),
  • kalman.filtering.associative_params_single (ny) and filtering_operator (nx, for Xi),
  • kalman.smoothing.update and associative_params_single (nx).

The other call sites use their factor either through products or as a whole triangular factor that must be invertible anyway, for which the default already gives the classical lower-triangular tangent, so they are unchanged.

Tests

Added to tests/cuthbertlib/linalg/test_tria.py:

  • the Gramian identity and the block structure (dL12 = 0, dL11 lower triangular) for rank-deficient inputs and several n_lower,
  • agreement with the built-in QR rule at full rank,
  • the log-likelihood value and gradient of predict/update against a covariance-form Kalman filter for the rotating rank-one chol_R above.
    This test fails on main and passes here.

For reference, on the model of #211 (cuthbert.filter, sequential and parallel), the gradients with this change agree with the covariance-form filter to about 1e-15.

Note

filter/filtering_operator still cannot handle a singular filtering factor at the value level, because cho_solve((U1, True), b1) inverts U1 (for example with a singular chol_R or chol_P0).
That is independent of differentiation and not addressed here.

The previous tangent satisfied dL L^T + L dL^T = dA A^T + A dA^T at any
rank, but it was not lower triangular when A is rank deficient. The Kalman
update reads the leading block of tria(A) as a triangular factor and
ignores its upper-right block, so the gradients were wrong whenever a
singular noise covariance rotates with a parameter (2.7% error in the
added test).

tria now takes a static `n_lower` argument giving the size of the leading
block used as a triangular factor downstream. The tangent keeps that block
lower triangular and the upper-right block zero, solves the off-diagonal
block exactly, and uses the previous pseudoinverse/null-space tangent on
the Schur-complement block. The default n_lower = 0 keeps the previous
behaviour. It reduces to the classical QR rule at full rank.

n_lower is set in the Kalman filter update, the associative filter and
smoother elements, and the filtering operator.

This branch has not been deployed

No deployments
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