Repository navigation
Description
Overview
On the new default hessian="fd" path, LaplaceOptimiser.optimise_approx writes only
next_state.hessian (free-variable curvature) and returns; det_hessian keeps the cavity
precision prepare_state built. The only code that refreshes it,
newton.quasi_deterministic_update, is reached from the line search and from refine_state,
which #1562 moved to the else branch. OptimisationState.inv_hessian_blocks then merges the
stale det_hessian.inv() into the projection, so deterministic variables come back at their
cavity covariance with SUCCESS: for z = 2x the fd path returns Var(x)=0.1996 (correct) and
Var(z)=1.0 (100.0 when the initial q(z) is N(0,10)), where the truth is 0.8 and the pre-#1562
quasi path returns 0.8. A silent wrong uncertainty on a variable EP reports as updated.
Finding 1 (P1) of the Codex review of the graphical-ep phase-2 fixes (#1558, #1560, #1562),
umbrella #1405. Reproduced exactly on main at f6a9915 on 2026-09-07.
Plan
- In the fd branch of
LaplaceOptimiser.optimise_approx, afternext_state.hessian = hessian,
refreshnext_state.det_hessianwhenever it is set, instead of leaving the cavity precision. - Compute the refresh by central-differencing the factor's deterministic outputs w.r.t. the free
parameters (same step rule as the existingmake_mode_hessian) to get J, then write the
diagonal precision1 / diag(J Σ Jᵀ)with Σ =hessian.inv(). - Diagonal only —
J Σ Jᵀis rank-deficient when there are more deterministic outputs than free
parameters, andMeanField.from_modeconsumes marginals anyway. Outputs with a zero J row keep
their cavity curvature. - Port the verified prototype; do not redesign it.
- Add the first test that asserts a deterministic variable's variance, over both
fdandquasi.
Detailed implementation plan
Work Classification
Library
Affected Repositories
- PyAutoFit (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main (f6a9915) | clean |
Suggested branch: feature/ep-laplace-deterministic-hessian
Worktree root: ~/Code/PyAutoLabs-wt/ep-phase2-review/ (shared bundle worktree, created by /start_library)
Implementation Steps
autofit/graphical/laplace/optimiser.py(~lines 219-248,LaplaceOptimiser.optimise_approx):
in the fd branch, afternext_state.hessian = hessian, whennext_state.det_hessianis set,
refresh it with a newmake_deterministic_hessian(state, hessian, mean_field).make_deterministic_hessian: central-difference the factor's
FactorValue.deterministic_valuesw.r.t. the free parameters, using the same step rule as
make_mode_hessian, to obtain J; then write
VariableFullOperator.from_diagonal(1 / diag(J Σ Jᵀ))with Σ =hessian.inv(). Keep it
diagonal:J Σ Jᵀis rank-deficient when n_det > n_free (the regression tests with 50 outputs
/ 3 params fail on a full block) andMeanField.from_modeconsumes marginals. An output whose
J row is all zeros keeps its cavity curvature.- Port the verified prototype (265/265
test_autofit/graphicalpass,
autofit_workspace_test/scripts/graphical/ep_deterministic.pyPASS) rather than redesigning. test_autofit/graphical/functionality/test_laplace_hessian.py: add thez = 2xEP graph
(Factor(2*x, x, factor_out=z), prior N(0,1) on x, likelihood N(0,0.5) on x, initial
q(z) = N(0,10)) and assert Var(x) ≈ 0.2 and Var(z) ≈ 0.8 (rtol 1e-2) for bothhessian="fd"
andhessian="quasi".- Run
python -m pytest test_autofit/graphical -q(265+ green) and, if practical, the workspace
smokeautofit_workspace_test/scripts/graphical/ep_deterministic.py. Never modify code to make
a test pass.
Testing
No existing test asserts a deterministic variable's variance:
regression/test_linear_regression.py checks a and b only, and ep_deterministic.py only asserts
z_variance < z_prior_variance under a dominating exact likelihood. The campaign referee
(analytic_gaussian_collapse.py) has no factor_out, so it could not see this at all. The new
test closes that gap.
Key Files
autofit/graphical/laplace/optimiser.py—LaplaceOptimiser.optimise_approx, the fd branch
(~219-248),prepare_state(~138-140),make_mode_hessian(the step rule to mirror).autofit/graphical/laplace/newton.py—quasi_deterministic_update(~104-119), the existing
refresh that the fd path never reaches.autofit/graphical/laplace/line_search.py—OptimisationState.inv_hessian_blocks(~241-246),
where the staledet_hessian.inv()is merged in.test_autofit/graphical/functionality/test_laplace_hessian.py— the new test's home.
Trade-offs
A diagonal deterministic precision discards the cross-covariance between deterministic outputs.
That is the deliberate choice: the full block is rank-deficient in the common n_det > n_free case
and MeanField.from_mode only reads marginals, so the block would be discarded downstream anyway.
Original Prompt
Click to expand starting prompt
Laplace fd-Hessian path leaves deterministic variables at their cavity covariance
Type: bug
Target: PyAutoFit
Repos:
- PyAutoFit
Themes: - graphical-ep
Difficulty: small
Autonomy: safe
Priority: high
Status: formalised
Consequence: glance
Witness:z = 2xEP graph (prior N(0,1), likelihood N(0,0.5) on x, initial q(z) = N(0,10)) returns Var(x) ≈ 0.2 and Var(z) ≈ 0.8 onhessian="fd"(currently 100.0) andhessian="quasi"; new test intest_autofit/graphical/functionality/test_laplace_hessian.py;test_autofit/graphicalstays 265+ green;autofit_workspace_test/scripts/graphical/ep_deterministic.pyPASS
Review-minutes: 6
Unattended: ready
Epic: graphical-ep
Filed: 2026-09-07
Finding 1 (P1) of the Codex review of the graphical-ep phase-2 fixes (PyAutoFit#1558, #1560,
#1562; record complete/2026/09/ep-scale-collapse-basin-cure-or-caveat.md). Reproduced exactly on
main (f6a9915) on 2026-09-07. Siblings from the same review:
ep_full_revert_not_updated.md, transformed_from_mode_coupled_covariance.md.
Original review (verbatim)
Phase 2 review: three code findings, across the merged fixes #1558, #1560 and #1562.
- P1 — Incorrect uncertainties for deterministic variables (fix(graphical): Laplace projection uses a finite-difference Hessian at the mode; failed updates are skipped, not written (#1561) #1562). The new Hessian path
updates the free-variable covariance but leaves deterministic outputs using their old
estimate. For z = 2x, the correct variances are Var(x)=0.2, Var(z)=0.8; the new default
returns Var(z)=1.0 with success. The previous quasi-Newton path returns 0.8.- P2 — Fully rejected updates can suppress the stale warning (fix(graphical): Laplace projection uses a finite-difference Hessian at the mode; failed updates are skipped, not written (#1561) #1562). The new tracking trusts
status.updated, but existing projection logic sets it true after restoring every invalid
parameter. A reproduced full EP run returned its unchanged starting distribution with no
STALE warning.- P2 — Coupled transformations receive incorrect covariance (fix(messages): carry truncation limits through every message rebuild; TransformedMessage.from_mode applies every Jacobian (#1559) #1560). Converting operator
covariance into a diagonal vector breaks MultiLogitNormalMessage. Equivalent matrix/operator
inputs return variances [0.5700, 0.6089] versus [1.0233, 0.8956]. The campaign's scalar
transformations pass, so its tests miss this.The phase-1 reporting error also carries into phase 2's README: the minimal Laplace example
demonstrates rejected updates, not numerical scatter collapse.Validation: 239 focused tests passed. The original phase-2 script reproduced RECOVER 5/5 on the
exact PR snapshots. That confirms the recorded interval checks; the completion record correctly
acknowledges that reliable scatter estimation remains unfinished. No defects found in #1558 or
workspace #93.
Reproduction (2026-09-07)
Factor(2*x, x, factor_out=z), prior N(0,1) x likelihood N(0,0.5) on x, EP with
LaplaceOptimiser:
| path | Var(x) | Var(z) | status |
|---|---|---|---|
hessian="fd" (new default) |
0.1996 | 1.0000 (initial q(z) untouched; 100.0 with q(z)=N(0,10)) | SUCCESS |
hessian="quasi" (pre-#1562) |
0.1996 | 0.8000 | SUCCESS |
| truth | 0.2 | 0.8 |
Both paths report success, so EP records the factor as updated: a silent wrong uncertainty. The
fd path exits the ascent at iteration 0 when the mean-field mean is already the mode, so not even
the in-loop secant update fires.
Mechanism
autofit/graphical/laplace/optimiser.py:219-248 (LaplaceOptimiser.optimise_approx): the fd
branch writes only next_state.hessian (free-variable curvature) and returns. det_hessian keeps
the cavity precision prepare_state built (lines 138-140). The only code that refreshes it,
newton.quasi_deterministic_update (laplace/newton.py:104-119), is reached from the line search
and from refine_state, which #1562 moved to the else branch. OptimisationState.inv_hessian_blocks
(laplace/line_search.py:241-246) then merges the stale det_hessian.inv() into the projection.
Fix
In the fd branch, after next_state.hessian = hessian, when next_state.det_hessian is set,
refresh it with a new make_deterministic_hessian(state, hessian, mean_field): central-difference
the factor's FactorValue.deterministic_values w.r.t. the free parameters (same step rule as
make_mode_hessian) to get J, then write VariableFullOperator.from_diagonal(1 / diag(J Σ Jᵀ))
with Σ = hessian.inv(). Diagonal only: J Σ Jᵀ is rank-deficient when n_det > n_free (the
regression tests with 50 outputs / 3 params fail on a full block), and MeanField.from_mode
consumes marginals. Outputs with a zero J row keep their cavity curvature. A verified prototype
(265/265 test_autofit/graphical pass, ep_deterministic.py PASS) exists; port it, do not
redesign.
Test: z = 2x as above, assert Var(x) ≈ 0.2 and Var(z) ≈ 0.8 (rtol 1e-2) for both fd and
quasi. No existing test asserts a deterministic variable's variance
(regression/test_linear_regression.py checks a, b only; ep_deterministic.py only asserts
z_variance < z_prior_variance under a dominating exact likelihood). The campaign referee
(analytic_gaussian_collapse.py) has no factor_out, so it could not see this.
Activity
Jammy2211 commentedon Sep 7, 2026
Library PR Created
feature/ep-laplace-deterministic-hessian, labelpending-release)API changes: none.
LaplaceOptimiser.make_deterministic_hessianis new, andfd_stepsis a pure extraction of the step rule that was inline inmake_mode_hessian; no existing signature or default changes.Workspace impact: none — no API change. Verified on this branch from
autofit_workspace_test:scripts/graphical/ep_deterministic.pyPASS, andscripts/graphical/analytic_gaussian_collapse.pyCOLLAPSE CONFIG: PASS (5/5 seeds), RECOVER 5/5.Tests:
test_autofit/graphical267 passed (265 + the 2 new parametrised cases),test_autofit/messages93 passed, run in the task worktree. Control: the new[fd]case fails without the source fix and[quasi]passes either way.Worked as member A of the
ep-phase2-reviewbundle (siblings #1569, #1571).Jammy2211 commentedon Sep 7, 2026
Shipped
470a89cf5fcd28df7cdb4def1c75c547384fac13(headd5acab35)d5acab35):Tests [pull_request]run 34162699077 —unittest (3.12),unittest (3.13),unittest-nojaxall success;Docs [pull_request]run 34162699044 success. mergeStateStatus CLEAN.autofit/graphical/laplace/optimiser.py(+90/-10)test_autofit/graphical/functionality/test_laplace_hessian.py(+56/-0)The fd branch of
LaplaceOptimiser.optimise_approx(default since #1562) wrote only the free-variable Hessian;det_hessiankept the cavity precision, so deterministic variables were projected unchanged withsuccess=True— z = 2x with initial q(z) = N(0, 10) returned Var(z) = 100 (quasi path and truth 0.8). Newmake_deterministic_hessiantakes a central-difference Jacobian ofdeterministic_valueson the sharedfd_steps, precision = diag(J Σ Jᵀ)⁻¹ (diagonal only — rank-deficient when n_det > n_free;from_modereads marginals); zero-row outputs keep cavity curvature.Verdict:
test__deterministic_variance_follows_the_free_variable[fd|quasi]fails on fd without the fix; graphical 267 pass; workspaceep_deterministic.pyPASS andanalytic_gaussian_collapse.pyRECOVER 5/5 with σ/μ unchanged from the phase-2 record. The campaign referee has nofactor_out, which is why every gate missed this.