Repository navigation
Description
Overview
A hierarchical EP fit can settle into a basin where the parent scale
hyperparameter of a HierarchicalFactor collapses to ~0 and is reported with an
over-confident ~0 error — a confident claim of "no scatter" that is simply wrong
(measured: 7% of 30 identical-problem runs on a clean known-answer toy, truth
σ=10). The F10 sigma-collapse guard passes this silently today, so the bad
value reaches the user with no warning at all.
This is leg 1 of PyAutoFit#1405 follow-up 2, and the honest bar from the parent
report: a collapsed parent scale must never be silently reported as a confident
answer. Curing the basin itself is deliberately not in scope here — see
"Not in scope" below.
Plan
- Establish why the existing F10 guard is silent: it misses on both limbs,
for structural reasons, not a mis-set threshold. - Record which mean-field variables are hierarchical parent scale
hyperparameters, reusing the graph accessor that already exists. - Add a relative, boundary-aware collapse test for those variables — the
pathology is a small scale reported confidently, which no absolute std test
can see. - Separately repair the F10 monotone limb, which is effectively dead code in any
multi-factor graph. - Pin the behaviour with tests built from the measured collapse and recover
values, so the thresholds are grounded rather than guessed.
Detailed implementation plan
Affected Repositories
- PyAutoFit (primary)
Branch Survey
| Repository | Current Branch | Dirty? |
|---|---|---|
| ./PyAutoFit | main | clean |
Suggested branch: feature/ep-hierarchical-scale-collapse-guard
Why the current guard is silent
Grounded in the toy's parent hyper-prior
sigma=af.TruncatedGaussianPrior(mean=10.0, sigma=5.0, …) (so stds[0] ≈ 5) and
the two measured COLLAPSE runs (mean=0.80, std=0.11; and 0.0030 ± ~0):
| limb | condition | collapsed run | verdict |
|---|---|---|---|
| A — floor | stds[-1] < 1e-8 |
std 0.11 |
misses by ~7 orders of magnitude |
| B — monotone | stds[-1] < 1e-3 × stds[0] = 5e-3 and np.all(np.diff(tail) < 0) over 5 steps |
std 0.11 > 5e-3 |
misses on magnitude |
Limb B is worse than mis-thresholded — it is effectively dead code in any
multi-factor graph. EPDiagnostics.snapshot appends a row for every variable
on every factor update, but a factor update only moves the marginals of the
variables adjacent to that factor. A variable's history is therefore dominated
by steps where it did not move, giving diff == 0 and failing the strict
< 0 test. Five consecutive strictly-decreasing updates is near unreachable in
practice. It fires in test_sigma_collapse_monotone only because that test
hand-builds a np.geomspace sequence directly into variable_rows.
The deeper mismatch: F10 was written for #1332, where every std shrinks to
zero around the starting means — so it is absolute and variable-agnostic.
This collapse has a different shape: the parent scale's mean goes to ~0 while
its std stays moderate in absolute terms and is over-confident only relative to
that mean. This resolves the parent prompt's open question ("is F10's threshold
wrong, or do parent scale hyperparameters need their own check?") in favour of
a dedicated parent-scale check.
Implementation Steps
autofit/graphical/expectation_propagation/diagnostics.py—
EPDiagnostics.register_hierarchical_scales(factor_graph)records the names of
parent scale variables. It reusesDeclarativeFactorGraph.hierarchical_factors
(already present) and picks the scale prior out of the distribution's
prior_tuplesby argument name (_SCALE_ARGUMENT_NAMES= sigma/scale/std/stddev).
No-ops on a plainFactorGraph— diagnostics must never kill a fit.optimiser.py— one line inEPOptimiser.__init__to register from the graph.diagnostics.py—check_sigma_collapsegains a limb for registered scale
variables: flag when the mean falls belowscale_mean_fraction(0.2) of its
initial value andstd / |mean|is belowscale_relative_error(0.5).
Separation on the measured data is clean — collapses at mean0.80(8% of the
10hyper-prior mean) and0.0030both fire; the recover band9.1–12.8 ± 0.9–2.4
does not.diagnostics.py—_drop_consecutive_repeatscollapses runs of identical
consecutive stds before the monotonicity test, restoring limb B to what it was
written to mean.- Tests use the measured values as fixtures, plus negative tests (an honestly
uncertain small scale is not a collapse; an unregistered variable is not
subject to the relative test).
No new call sites needed: EPOptimiser already invokes check_sigma_collapse at
optimiser.py:560 (into ep_diagnostics.results) and optimiser.py:575
(_warn_sigma_collapse, logged regardless of whether output paths are enabled).
Key Files
autofit/graphical/expectation_propagation/diagnostics.py— the guard, the
scale registry, and the monotone-limb repairautofit/graphical/expectation_propagation/optimiser.py— registration wiringtest_autofit/graphical/functionality/test_diagnostics.py— tests
Verification
test_autofit/graphical/functionality/test_diagnostics.py— 16 passed.- 5 of the new tests fail on unmodified
main, so they pin new behaviour
rather than passing either way. The 4 that pass on main are negative
(no-false-positive) guards, as expected. - Full
test_autofit/graphical— 240 passed, 1 failed. The failure
(test_messages.py::test_beta,ModuleNotFoundError: No module named 'jax') is
a pre-existing environment gap and fails identically on unmodifiedmain. - End-to-end on a real 5-drawn-variable hierarchical graph: the parent
sigmais
registered and the parentmeancorrectly is not, and the shallow collapse that
mainpassed silently now emits a specific warning.
Not in scope (deliberately)
- Curing the collapse basin (leg 2 of the prompt) — research-grade, kept
separate. The parent prompt's acceptance already sanctions converting it to a
documented methods caveat if the basin proves inherent to EP for scale
hyperparameters. - Revising the standing damping hint — already shipped on
main
(diagnostics.py:250,graphical/README.md:158); the prompt was stale on it.
This change reports; it never alters the fit.
Original Prompt
Click to expand starting prompt
Filed in PyAutoMind as draft/bug/autofit/ep_hierarchical_scale_collapse_moment_match.md
(planned 2026-08-11). Parent report: #1405. Sibling defect (the
InitializerException crash arm) shipped as #1454.
The prompt's § "The task" lists three legs:
- Make the collapse detectable before making it curable. The F10
sigma-collapse guard (check_sigma_collapse) passes a scatter≈0 / error≈0
parent silently today. "That is the most defensible first deliverable and it
is independently shippable." Decide whether F10's threshold is wrong or
whether parent scale hyperparameters need their own check. ← this issue - Then attack the basin — a damped
HierarchicalFactorscale moment-match,
or a deterministic per-factor optimiser in place of the per-factor nested
sampler. ← deferred, not this issue Revise the diagnostics' standing damping hint— already shipped onmain.
Measured over 30 identical-problem runs of a clean CPU toy (no JAX, no lensing;
parent scatter truth σ=10, far from the σ→0 boundary):
| outcome | freq | parent scatter (truth 10) |
|---|---|---|
| RECOVER | 70% | 9–13, sane errors — matches the joint sampler (12.8 [9.9, 16.3]) |
| COLLAPSE | 7% | 0.003–0.80 with an over-confident ≈0 error |
| CRASH | 23% | InitializerException (split out, shipped as #1454) |
The joint fit (Dynesty on factor_graph.global_prior_model for the same graph)
is stable and correct every time — the graph and the data are fine; EP is the
sole source of the instability.
Mechanism, already established: over-shrinkage feedback is the collapse basin.
Per-group drawn-variable posterior means cluster far tighter than truth
(spread ~0.6 vs true ±10); the parent factor then sees near-identical draws,
infers a tiny scatter, and tightens the shrinkage further — positive feedback to
a fixed point at scatter ≈ 0 with ≈0 reported error. Delta-method / boundary
artefact was refuted; under-convergence explains movement into and out of
basins, not the fixed point.
Acceptance (from the prompt): "The honest bar is not 'collapse never
happens' — it is that a collapsed parent scale is never silently reported as a
confident answer. F10 firing on the 7% is a pass; curing the basin outright is
the stretch goal."
A trap the prompt calls out, respected here: the tool must rank/report for
human review and never retire or alter the fit itself.
Repro and evidence: ep_toy_diagnostic.py + EP_TOY_FINDINGS.md under
PyAutoMind complete/2026/07/ep_scale_collapse_assets/. Numpy-only, CPU,
minutes. Collapse is only ~7% per run, so any verdict about the basin needs a
loop of runs, not one — which is part of why leg 1 is scoped to the guard, whose
thresholds are testable directly against the measured values.
Activity
Merge pull request #1465 from PyAutoLabs/feature/ep-hierarchical-scal…