Skip to content

fix(graphical): Laplace projection uses a finite-difference Hessian at the mode; failed updates are skipped, not written (#1561) - #1562

Merged
Jammy2211 merged 2 commits into
mainfrom
feature/ep-laplace-hessian
Sep 2, 2026
Merged

Jammy2211 merged 2 commits into
mainfrom
feature/ep-laplace-hessian

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

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:

  • D2 — the projected "covariance" was never the Hessian of the tilted distribution. make_posdef_hessian seeded 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 the refine_state secant 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 and n_refine=0 allowed; above hessian_max_size flattened parameters the quasi path is used with a log line).
  • D3 — a failed update still wrote the message. A failed line search projected the start point as if it were the mode and update_factor_mean_field never consulted status.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 with updated=False and 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 — mu 50.8596 ± 4.1102 vs closed form 50.8595 ± 4.1104, every x_i to 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 B sigma 9.37 ± 3.57 (closed form 6.57 ± 2.88, inside the interval, hard caps pass) and mu 50.60 ± 3.48 PASS; the remaining scatter bias is the Laplace-on-scatter caveat now documented in autofit/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

LaplaceOptimiser gains keyword arguments hessian="fd"|"quasi" (default "fd"), fd_step=1e-2 (× model std), fd_min_step=1e-8, hessian_max_size=64; n_refine now accepts 0. Default Laplace projections change numerically (they are now the inverse Hessian at the mode) and are deterministic — no np.random draw on the default path. Failed or non-concave updates return the previous message with updated=False instead of overwriting it; MeanField.from_opt_state projects through inv_hessian_blocks(). New public helper autofit.graphical.laplace.newton.finite_difference_hessian. Workspace/tutorial prose numbers produced by Laplace EP (autofit_workspace features/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 returns last_dist is-identical with updated=False; EPMeanField byte-identical through SimplerUpdater; convex factor → BAD_PROJECTION "not concave"; per-variable backstop reverts and names the variable; n_refine=0; secant chaining; option validation
  • pytest test_autofit -q: 2440 passed, 2 skipped (no existing expectation moved; test_hierarchical.py comment updated; finite_difference_hessian in the alive list)
  • Referee leg A 18/18, K-invariant; collapse config 5/5 RECOVER; leg B / priors tables on fix(graphical): Laplace projection uses a real Hessian at the mode; failed updates no longer overwrite the message (EP) #1561
  • autofit/graphical/README.md §3.2 / §3.4 / new §3.5 describe the shipped behaviour and the scatter caveat
  • CI green (unittest 3.12 / 3.13 / nojax, docs)
Full 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 in Variable.id order.
  • LaplaceOptimiser.__init__(..., hessian="fd", fd_step=1e-2, fd_min_step=1e-8, hessian_max_size=64, n_refine=3) and LaplaceOptimiser.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 — on status.success == False returns the unchanged mean field; with hessian="fd" sets the mode Hessian (or returns BAD_PROJECTION when it is non-finite / not negative-definite); refine_state/refine_approx chain their secant updates and honour n_refine=0.
  • MeanField.from_opt_state — projects through state.inv_hessian_blocks() (dense marginal blocks) instead of subscripting the inverse operator.
  • MeanField.update_factor_mean_field — returns last_dist with updated=False when the incoming status is not a success; the invalid-projection backstop's status names each reverted variable with precision(q*) and precision(cavity).
  • EPOptimiser.run — _factors_updated only counts status.updated updates; STALE FACTORS warning text covers skipped updates.

Migration

  • None required. To reproduce pre-change numbers: LaplaceOptimiser(hessian="quasi") (not deterministic).

Generated by the PyAutoLabs agent workflow.

Jammy2211 and others added 2 commits September 2, 2026 18:57
…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
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.

fix(graphical): Laplace projection uses a real Hessian at the mode; failed updates no longer overwrite the message (EP)

1 participant