Repository navigation
fix(graphical): Laplace projection uses a finite-difference Hessian at the mode; failed updates are skipped, not written (#1561) - #1562
Merged
Conversation
…t the mode; failed and narrower-than-cavity updates are skipped, not written (#1561) D2: `LaplaceOptimiser(hessian="fd")` (default) projects the inverse of a central finite-difference Hessian of the tilted gradient, assembled in Variable.id order and Cholesky-factorised as a full matrix (`newton.finite_difference_hessian`, `make_mode_hessian`); marginal blocks reach the messages through `MeanField.from_opt_state` -> `inv_hessian_blocks()`. A non-finite or non-concave Hessian is a BAD_PROJECTION that keeps the previous message. Factors above `hessian_max_size` (64) fall back to `hessian="quasi"`, whose `refine_state` now chains its secants and honours `n_refine=0`. The default path draws no random numbers: the two-variable analytic fixture is bit-equal across seeds and variable ids, and leg A of the analytic Gaussian benchmark gives mu 50.8596 +/- 4.1102 (closed form 4.1104) with 0 BAD_PROJECTION, identical across K = 1, 3, 6 throwaway priors. D3: `optimise_approx` returns the mean field unchanged when the optimiser did not succeed and `update_factor_mean_field` returns `last_dist` with `updated=False` (flag preserved), so a failed line search no longer projects the start point over the factor message. The per-variable `check_valid`/`update_invalid` backstop is kept and now names each reverted variable with precision(q*) and precision(cavity). `EPOptimiser` only counts a factor as updated when `status.updated`, so an always-skipped factor is reported by the stale-factor warning. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C49DeRHoKss4xBnz5F7gUm
…mode; skipped updates; the hierarchical-scatter caveat (#1561) Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01C49DeRHoKss4xBnz5F7gUm
This was referenced Sep 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1561. Found by the analytic Gaussian benchmark (autofit_workspace_test#91 / PR #92) and root-caused read-only; last of the three fixes in the EP campaign's wave (#1558 gradients, #1560 message support). Two coupled defects in the Laplace path:
make_posdef_hessianseeded the quasi-Newton matrix with the mean-field diagonal precision and no factor curvature; when the start was already the mode nothing ever touched it, and therefine_statesecant at random draws made the result depend on the RNG and, through the sampling order, on prior ids (the same factor projected to variances of 35, 30 or 90 for an analytic 83 depending on the seed). The projection now uses a real Hessian at the converged mode: central finite differences of the (post-fix(graphical): prior id 0 no longer collides with the FactorValue sentinel; cavity gradient added after the VJP (#1557) #1558) tilted gradient assembled in id-sorted order, factorised as a full matrix and projected through the per-variable marginal blocks (LaplaceOptimiser(hessian="fd"), the default;hessian="quasi"keeps the old path with its refinement chaining fixed andn_refine=0allowed; abovehessian_max_sizeflattened parameters the quasi path is used with a log line).update_factor_mean_fieldnever consultedstatus.success, which is how a parent mean's mean field drifted wider than its own prior. Failed optimisations, and modes whose Hessian is not finite or not negative-definite (boundary modes such as a scatter driven to zero), now return the previous message withupdated=Falseand a message naming the factor and reason; the existing per-variable invalid-projection backstop now names each reverted variable with both precisions; and a factor whose updates are skipped on every sweep is reported by the STALE FACTORS warning (previously only raising factors were).Referee after this PR (
autofit_workspace_test/scripts/graphical/analytic_*.py, canonical scripts, worktree library): leg A 18/18 PASS —mu50.8596 ± 4.1102 vs closed form 50.8595 ± 4.1104, everyx_ito four decimals, 0 BAD_PROJECTION, and the EP column is byte-identical across K = 1, 3, 6 throwaway priors created before the graph (was four distinct fixed points). Collapse configuration (phase 2 of the campaign): RECOVER 5/5 seeds, no silent verdicts — seed 0's scatter 9.24 ± 3.61 inside the closed-form [2.98, 12.15] where it previously collapsed to ~2e-4 or sat stale. Leg Bsigma9.37 ± 3.57 (closed form 6.57 ± 2.88, inside the interval, hard caps pass) andmu50.60 ± 3.48 PASS; the remaining scatter bias is the Laplace-on-scatter caveat now documented inautofit/graphical/README.md§3.5. The leg-A EP loop is faster (2.8 s → 1.5 s: 40 churning hierarchical updates → 14). Tables on #1561. Tolerances untouched.API Changes
LaplaceOptimisergains keyword argumentshessian="fd"|"quasi"(default"fd"),fd_step=1e-2(× model std),fd_min_step=1e-8,hessian_max_size=64;n_refinenow accepts0. Default Laplace projections change numerically (they are now the inverse Hessian at the mode) and are deterministic — nonp.randomdraw on the default path. Failed or non-concave updates return the previous message withupdated=Falseinstead of overwriting it;MeanField.from_opt_stateprojects throughinv_hessian_blocks(). New public helperautofit.graphical.laplace.newton.finite_difference_hessian. Workspace/tutorial prose numbers produced by Laplace EP (autofit_workspacefeatures/expectation_propagation.py, HowToFit chapter 3 EP tutorials) may shift; no signature they use changes.See full details below.
Test Plan
test_autofit/graphical/functionality/test_laplace_hessian.py(new, 10): projected covariance equals the analytic inverse Hessian to 1e-8 and is bit-equal across seeds (0, 1, 12345) and across K throwaway variables; FD matrix vs analytic; line-search failure returnslast_distis-identical withupdated=False;EPMeanFieldbyte-identical throughSimplerUpdater; convex factor →BAD_PROJECTION"not concave"; per-variable backstop reverts and names the variable;n_refine=0; secant chaining; option validationpytest test_autofit -q: 2440 passed, 2 skipped (no existing expectation moved;test_hierarchical.pycomment updated;finite_difference_hessianin the alive list)autofit/graphical/README.md§3.2 / §3.4 / new §3.5 describe the shipped behaviour and the scatter caveatFull API Changes (for automation & release notes)
Added
autofit.graphical.laplace.newton.finite_difference_hessian(state, step) -> (H, shapes)— central FD Hessian of the tilted log-density inVariable.idorder.LaplaceOptimiser.__init__(..., hessian="fd", fd_step=1e-2, fd_min_step=1e-8, hessian_max_size=64, n_refine=3)andLaplaceOptimiser.make_mode_hessian(state, mean_field).EPOptimiser._factors_skipped— factors whose updates were skipped; the stale-factor warning reports(raised | skipped) − updated.Changed Behaviour
LaplaceOptimiser.optimise_approx— onstatus.success == Falsereturns the unchanged mean field; withhessian="fd"sets the mode Hessian (or returnsBAD_PROJECTIONwhen it is non-finite / not negative-definite);refine_state/refine_approxchain their secant updates and honourn_refine=0.MeanField.from_opt_state— projects throughstate.inv_hessian_blocks()(dense marginal blocks) instead of subscripting the inverse operator.MeanField.update_factor_mean_field— returnslast_distwithupdated=Falsewhen the incoming status is not a success; the invalid-projection backstop's status names each reverted variable withprecision(q*)andprecision(cavity).EPOptimiser.run—_factors_updatedonly countsstatus.updatedupdates; STALE FACTORS warning text covers skipped updates.Migration
LaplaceOptimiser(hessian="quasi")(not deterministic).Generated by the PyAutoLabs agent workflow.