Repository navigation
Phase 4: Newton + adaptive PDF + outlier rejection - #22
Merged
neuromechanist merged 4 commits intoJul 3, 2026
Merged
neuromechanist merged 4 commits into
neuromechanist merged 4 commits into
Conversation
Newton curvature stats used the density derivative dpdf where Fortran uses the score fp; dsigma2 was inflated num_mix-fold inside the mixture loop; kappa lacked the sbeta^2 factor and lambda the baralpha-weighted mu^2 term. Correct all four to amica17.f90 semantics and add the positive-definiteness fallback (whole-model natural gradient when a pair fails sk1*sk2>1), so the reference is a valid Newton parity target.
Port the Fortran Newton preconditioner (per-source-pair 2x2 solve with positive-definiteness fallback), the score-based curvature statistics (sigma2/lambda/kappa), and outlier rejection (per-sample LL threshold, one-directional good-sample mask, good-count normalization) into AMICATorchNG. Replace the ad-hoc backtracking with Fortran's convergence control (anneal lrate/rholrate on LL decrease; ratchet the lrate ceiling and newtrate down after maxdecs consecutive decreases). Wire do_newton through the public AMICA(backend='ng') interface. Newton is unit-verified against the reference to ~1e-16 but does not yet close the >0.95 correlation gate; the NG backend converges to a different fixed point than Fortran (issue #21).
Newton stat parity (sigma2/lambda/kappa vs NumPy reference), full Newton M-step A-update parity, the Newton direction formula and its positive-definiteness guard, and outlier-rejection sample shrinkage on real data. Update the >0.95 end-to-end gate to xfail with an accurate reason (fixed-point gap, issue #21) and enable Newton in it so it exercises the full path.
- Reject using the pre-update per-sample LL (Fortran order), not post-update. - Guard empty/degenerate good sets: validate reject params at construction, raise a clear ValueError instead of a downstream None/TypeError crash. - Port the dalpha epsilon floor from the NumPy reference (do_reject makes the zero-responsibility divide more likely). - Expose Newton fallback observability: n_newton_fallbacks counter + warning (mirrors Fortran's print), stop_reason attribute, lrate-floor break promoted to warning; log the NumPy reference fallback too. - Fix stale/overclaiming docstrings (lrate control, do_newton wiring, tests). - Add tests: positive-definite Newton M-step composition, rho=1/2 boundary stat parity, multi-model finiteness, degenerate-LL rejection error, reject param validation, and LL good-count normalization.
Member
Author
/review-pr summary (3 agents, Sonnet)Ran code-review, test-coverage, and silent-failure reviewers on the diff. No findings on the (intentional, tracked) >0.95 gate. Findings addressed in ec84388: Correctness / parity
Robustness (no silent failures)
Docs
Coverage added
Full fast suite: 52 passed, 4 xfailed, 0 failures. Reviewers explicitly noted the diagonal Newton The unmet >0.95 gate is a real, deeper NG-vs-Fortran fixed-point discrepancy (issue #21), not a Newton bug; the gate test stays xfail. |
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
Ports the Fortran Newton preconditioner, adaptive-PDF (rho) confirmation, and outlier rejection
into the natural-gradient backend
AMICATorchNG, and adds Fortran-style learning-rate control.All three features are implemented and unit-parity-verified against the (Fortran-corrected) NumPy
reference to float64 precision.
Important: this does NOT yet close the epic's >0.95 correlation gate. The NG backend converges
to a different fixed point than Fortran (~0.68 correlation, LL -3.47 vs -3.41), and Newton only
accelerates convergence to that same NG fixed point. The root cause is upstream of Newton and is
tracked separately in #21. The end-to-end gate test is therefore kept as xfail with an accurate
reason.
Closes #13. Part of epic #9.
What changed
torch_impl/amica_torch_ng.pyper-source-pair 2x2 direction solve, positive-definiteness fallback to natural gradient,
newtrate-capped ramp. Matches
amica17.f90.mean - rejsig*std), one-directionalgood-sample mask, good-count normalization of
gmand reported LL.(anneal on LL decrease; ratchet the lrate ceiling and newtrate after
maxdecsdecreases). Thisfixes a pre-existing divergence where the natural gradient drifted the LL down at lrate=0.05.
_forwardE-step helper (used by block updates and the rejection LL).pyAMICA.py: corrected the NumPy reference Newton math to Fortran spec (scorefpnot thedensity derivative;
dsigma2accumulated once per source;sbeta^2on kappa; baralpha-weightedmu^2term in lambda; positive-definiteness fallback), so it is a valid Newton parity target.amica.py:do_newtonnow plumbs throughbackend="ng"instead of raising.Test plan
test_newton_stats_match_numpy_reference- finalized sigma2/lambda/kappa vs NumPy, <1e-8.test_newton_mstep_matches_numpy_reference- one full Newton M-step A-update vs NumPy, <1e-8.test_newton_direction_matches_formula- direction formula + posdef guard on controlled input.test_rejection_shrinks_good_sample_set- real-data rejection drops samples, stays finite.test_end_to_end_correlation_vs_fortran- xfail (fixed-point gap, NG backend converges to a different fixed point than Fortran (~0.68 corr, blocks >0.95 gate) #21).