Repository navigation
Description
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_modewheneverself.transformsis 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
autofit/messages/composed_transform.py(~lines 446-457): for aLinearOperator
covariance, keep the dense matrix (covariance.to_dense(), or the existing dense
accessor onLinearOperator/MatrixOperator/DiagonalMatrixin
autofit/mapper/operator.py) through the Jacobian loop whenself.transformsis
non-empty; take.diagonal()only in the no-transform case. Add a short comment
explaining the coupled-Jacobian reason.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 overMultiLogitNormalMessagewith mode
[0.3, 0.4]and both Σ = diag(0.02, 0.03) and a non-diagonal 2x2 Σ, asserting that
ndarray,MatrixOperatorandDiagonalMatrixinputs agree with each other (rtol 1e-6)
and with an independently computed delta-methoddiag(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.- 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 fromVariableOperator.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'sShermanMorrisonJacobian.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-methoddiag(J Σ Jᵀ)) for an ndarray, aMatrixOperatorand aDiagonalMatrixinput (operator forms currently [2.3611, 2.1875]);test_autofit/messagesstays 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
Jammy2211 commentedon Sep 7, 2026
Library PR Created
feature/transformed-from-mode-coupled-covariance, labelpending-release)API changes: none —
TransformedMessage.from_modekeeps its signature; only the internal handling of aLinearOperatorcovariance changes (dense through the Jacobian loop when transforms are present, diagonal only when there are none).Workspace impact: none.
MultiLogitNormalMessagehas 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/messages95 passed (93 + the 2 new parametrised cases),test_autofit/graphical265 passed, run in the task worktree.Worked as member C of the
ep-phase2-reviewbundle (siblings #1570, #1571).Jammy2211 commentedon Sep 7, 2026
Shipped
31f13c1f0c68139661ef32240baec3571bcd7a1d(head470473f7)470473f7):Tests [pull_request]run 34161565978 —unittest (3.12),unittest (3.13),unittest-nojaxall success;Docs [pull_request]run 34161565939 success. mergeStateStatus CLEAN.autofit/messages/composed_transform.py(+12/-3)test_autofit/messages/test_transformed_from_mode.py(+56/-1)TransformedMessage.from_modereduced aLinearOperatorcovariance to its diagonal before applying the transform Jacobians;jac.quadcontracts 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.