Skip to content

fix(messages): keep the full covariance through coupled transforms in from_mode #1569

Closed

Description

@Jammy2211

Overview

TransformedMessage.from_mode reduces a LinearOperator covariance to its diagonal
vector before applying the transform Jacobians. LinearOperator.quad then contracts
that 1-D input as a vector (J(Jᵀv)) rather than as diag(v), so for a coupled Jacobian
(MultinomialLogitTransform's ShermanMorrison, used by MultiLogitNormalMessage) the
off-diagonal J Σ Jᵀ mass that belongs on the diagonal is lost. Equivalent ndarray and
operator inputs disagree: [1.2222, 1.2431] vs [2.3611, 2.1875]. Scalar transforms
(log, log10, logistic) have diagonal Jacobians where both contractions coincide, which is
why every existing test passes. Pre-#1560 this path raised ValueError; #1560 turned a
crash into a silently wrong answer, so it is defensive-path only today — but it is wrong.

Finding 3 (P2) of the Codex review of the graphical-ep phase-2 fixes (#1558, #1560, #1562),
umbrella #1405. Confirmed on main at f6a9915 on 2026-09-07.

Plan

  • Keep the dense covariance matrix through the Jacobian loop in
    TransformedMessage.from_mode whenever self.transforms is non-empty; take .diagonal()
    only in the no-transform case.
  • Do not key on is_diagonal: diag(v) under a coupled Jacobian still needs the full
    matrix.
  • Add a parametrised operator-vs-ndarray equivalence case over MultiLogitNormalMessage
    (2x2 Σ, diagonal and non-diagonal) to the phase-2 test file, checked against an
    independently computed delta-method reference.
  • Confirm scalar-transform results are bit-identical and the graphical suite is unchanged.
Detailed implementation plan

Work Classification

Library

Affected Repositories

  • PyAutoFit (primary)

Branch Survey

Repository Current Branch Dirty?
./PyAutoFit main (f6a9915) clean

Suggested branch: feature/transformed-from-mode-coupled-covariance

Worktree root: ~/Code/PyAutoLabs-wt/ep-phase2-review/ (shared bundle worktree, created by /start_library)

Implementation Steps

  1. autofit/messages/composed_transform.py (~lines 446-457): for a LinearOperator
    covariance, keep the dense matrix (covariance.to_dense(), or the existing dense
    accessor on LinearOperator / MatrixOperator / DiagonalMatrix in
    autofit/mapper/operator.py) through the Jacobian loop when self.transforms is
    non-empty; take .diagonal() only in the no-transform case. Add a short comment
    explaining the coupled-Jacobian reason.
  2. test_autofit/messages/test_transformed_from_mode.py (added by fix(messages): carry truncation limits through every message rebuild; TransformedMessage.from_mode applies every Jacobian (#1559) #1560, currently
    scalar-only): add a parametrised case over MultiLogitNormalMessage with mode
    [0.3, 0.4] and both Σ = diag(0.02, 0.03) and a non-diagonal 2x2 Σ, asserting that
    ndarray, MatrixOperator and DiagonalMatrix inputs agree with each other (rtol 1e-6)
    and with an independently computed delta-method diag(J Σ Jᵀ) (rtol 1e-4), with J from
    the transform's Jacobian or finite differences in the test. Match the file's existing
    fixture/style.
  3. Run python -m pytest test_autofit/messages -q (93 + new) and
    python -m pytest test_autofit/graphical -q (265). Never modify code to make a test pass.

Testing

  • test_autofit/messages — 93 existing + the new parametrised case.
  • test_autofit/graphical — 265, unchanged (this branch is defensive-only in the live
    EP/Laplace path: MeanField.from_mode_covariance, autofit/graphical/mean_field.py:397,
    is fed dense ndarrays from VariableOperator.blocks()).

Key Files

  • autofit/messages/composed_transform.py — the diagonalisation, ~lines 446-457.
  • autofit/mapper/operator.py — LinearOperator / MatrixOperator / DiagonalMatrix,
    quad, and the dense accessor.
  • autofit/messages/transform.py — MultinomialLogitTransform's ShermanMorrison Jacobian.
  • test_autofit/messages/test_transformed_from_mode.py — the test to parametrise.

Trade-offs

Keeping the dense matrix costs O(n²) memory in this branch instead of O(n). The branch is
defensive-only and n here is a message's parameter count, so the cost is negligible
against returning a silently wrong variance.

Original Prompt

Click to expand starting prompt

TransformedMessage.from_mode diagonalises an operator covariance before a coupled Jacobian

Type: bug
Target: PyAutoFit
Repos:

  • PyAutoFit
    Themes:
  • graphical-ep
    Difficulty: small
    Autonomy: safe
    Priority: medium
    Status: formalised
    Consequence: glance
    Witness: MultiLogitNormalMessage.from_mode(mode=[0.3, 0.4], covariance=...) returns the same variances ([1.2222, 1.2431], the delta-method diag(J Σ Jᵀ)) for an ndarray, a MatrixOperator and a DiagonalMatrix input (operator forms currently [2.3611, 2.1875]); test_autofit/messages stays green with the new parametrised case; scalar-transform results bit-identical
    Review-minutes: 4
    Unattended: ready
    Epic: graphical-ep
    Filed: 2026-09-07

Finding 3 (P2) of the Codex review of the graphical-ep phase-2 fixes (review text verbatim in the
sibling ep_laplace_deterministic_hessian.md). Confirmed on main (f6a9915) on 2026-09-07.
Not a regression in the strict sense: pre-#1560 the operator path raised ValueError; #1560
turned a crash into a silently wrong answer. No live caller reaches it (see Impact), so P2 stands
but is defensive-path only.

Reproduction (2026-09-07)

MultiLogitNormalMessage, mode [0.3, 0.4], Σ = diag(0.02, 0.03), base-message variances:

covariance input variance
np.diag([0.02, 0.03]) [1.2222, 1.2431]
MatrixOperator(np.diag(...)) [2.3611, 2.1875]
DiagonalMatrix([0.02, 0.03]) [2.3611, 2.1875]
delta method diag(J Σ Jᵀ) [1.2222, 1.2431]

Mode [0.2, 0.3], Σ = 0.01·I: matrix [0.5300, 0.3244] vs operator [0.7767, 0.5711] — the same
shape as the reviewer's [0.5700, 0.6089] vs [1.0233, 0.8956].

Mechanism

autofit/messages/composed_transform.py:446-457: an operator covariance is reduced to
covariance.diagonal() before the Jacobian loop; jac.quad(M) (LinearOperator.quad,
(M * self).T * self) contracts a 1-D input as a vector (J(Jᵀv)), not as diag(v). For
MultinomialLogitTransform the Jacobian is a coupled ShermanMorrison operator, so the
off-diagonal J Σ Jᵀ mass that belongs on the diagonal is lost. Scalar transforms (log, log10,
logistic) have diagonal Jacobians where both contractions coincide, which is why every campaign
test passes.

Fix

For a LinearOperator covariance keep the dense matrix (covariance.to_dense()) through the
Jacobian loop when self.transforms is non-empty; take .diagonal() only in the no-transform
case. Keying on is_diagonal is wrong: diag(v) under a coupled J still needs the full matrix.
Verified by monkeypatch: all three input forms return [1.2222, 1.2431]; scalar-transform results
bit-identical.

Test: parametrise the operator-vs-array equivalence in
test_autofit/messages/test_transformed_from_mode.py (added by #1560, currently scalar-only) over
MultiLogitNormalMessage with a 2x2 Σ, asserting both forms equal the delta-method reference.

Impact

MeanField.from_mode_covariance (autofit/graphical/mean_field.py:397) is the only in-library
caller and is fed dense ndarrays from VariableOperator.blocks() /
VariableFullOperator.blocks(), so the branch is defensive-only in the live EP/Laplace path.
MultiLogitNormalMessage has zero hits in any workspace.

Activity

  1. Jammy2211 commented on Sep 7, 2026

    @Jammy2211
    CollaboratorAuthor

    Library PR Created

    API changes: none — TransformedMessage.from_mode keeps its signature; only the internal handling of a LinearOperator covariance changes (dense through the Jacobian loop when transforms are present, diagonal only when there are none).

    Workspace impact: none. MultiLogitNormalMessage has zero hits in any workspace, and the sole in-library caller (MeanField.from_mode_covariance) passes dense ndarrays, so this branch is defensive-path only.

    Tests: test_autofit/messages 95 passed (93 + the 2 new parametrised cases), test_autofit/graphical 265 passed, run in the task worktree.

    Worked as member C of the ep-phase2-review bundle (siblings #1570, #1571).

  2. Jammy2211 commented on Sep 7, 2026

    @Jammy2211
    CollaboratorAuthor

    Shipped

    • PR: fix(messages): keep the full covariance through coupled transforms in from_mode #1572 (MERGED 2026-09-07)
    • Merge commit: 31f13c1f0c68139661ef32240baec3571bcd7a1d (head 470473f7)
    • CI (head 470473f7): Tests [pull_request] run 34161565978 — unittest (3.12), unittest (3.13), unittest-nojax all success; Docs [pull_request] run 34161565939 success. mergeStateStatus CLEAN.
    • Files changed:
      • autofit/messages/composed_transform.py (+12/-3)
      • test_autofit/messages/test_transformed_from_mode.py (+56/-1)

    TransformedMessage.from_mode reduced a LinearOperator covariance to its diagonal before applying the transform Jacobians; jac.quad contracts a vector as J(Jᵀv), not diag(v), so a coupled Jacobian (MultinomialLogitTransform / MultiLogitNormalMessage) gave [2.3611, 2.1875] where the ndarray form gave [1.2222, 1.2431] (delta method). The dense matrix (to_dense()) is now kept through the Jacobian loop when transforms are present. Pre-#1560 this path raised; #1560 had made it silently wrong.

    Verdict: defensive-path only — the sole in-library caller passes dense ndarrays and no workspace uses MultiLogitNormalMessage; a new parametrised test pins operator-vs-array equivalence and the delta-method reference (fails without the fix), messages 95 / graphical 265 pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions