Repository navigation
Add restart-on-NaN mechanism to NumPy backend (#39) - #42
Merged
neuromechanist merged 3 commits intoJul 5, 2026
Conversation
Ports Fortran's NaN recovery (amica17.f90:1027-1060): if the log-likelihood goes non-finite at iter <= restartiter (default 10), reinitialize from a fresh RNG draw and start over, up to maxrestarts (default 3); a later NaN stops the fit (Fortran exits too). The NumPy backend previously had no restart, so an unlucky init that diverged just ran into a non-finite LL. - New params restartiter/maxrestarts/numrestarts; _reinitialize_for_restart redraws A/mixture params and resets lrate + LL/nd history. - Restart check in _optimize after the parameter update; restart window is restartiter iters after each restart. - Fix _check_convergence to guard the ll[-2] comparison on len(self.ll), not self.iter (which keeps counting across restarts), so a post-restart first iteration no longer IndexErrors. - test_sample_data_cli: pin --seed 0 (a known-stable basin) and un-xfail it; an integration test must not depend on a random init that can hit the shared stochastic late-NaN divergence. Add fault-injection tests for the restart-recover and give-up-after-maxrestarts paths. Tested: non-slow suite 36 passed, 1 xfailed; restart tests green.
The #39 NaN is handled (pinned seed=0 + restart complete all 2000 iters, finite LL, no early stop), but the full CLI-vs-Fortran match still fails on a separate long-run drift: seed=0 matches Fortran 32/32 at ~150 iters but degrades (LL -3.404 -> -3.409) by 2000 iters. Keep test_sample_data_cli xfail with an accurate reason pointing at the new issue #41 (the seed pin and restart stay, since the run now completes without NaN).
- _reinitialize_for_restart now redraws ONLY the mixing matrix A (matching Fortran amica17.f90:1032-1053, which keeps the last-successful mixture params); the previous cold-reset of mu/alpha/beta/rho/gm/c diverged from Fortran's restart trajectory. - Surface terminal failure instead of a silent success: AMICA.fit sets self.converged / self.stop_reason; the CLI logs an error and exits non-zero (and no longer claims "Results saved") when the fit ends on a non-finite LL. Restart is logged at WARNING, terminal divergence at ERROR. - Reset numrestarts per fit() (was only set in __init__, so a refit lost the restart budget). - Use Fortran's absolute restartiter window (iter <= restartiter) and drop the more-lenient per-restart offset; comment no longer over-claims parity. - Fix _check_convergence to guard the ll[-2] comparison on len(self.ll). - Tests assert converged and that the restart is logged (not silent). Tested: restart tests + non-slow suite green (36 passed, 1 xfailed).
Member
Author
Review (code-reviewer + silent-failure-hunter, Sonnet) + resolutionsFindings addressed in the latest commit: Addressed
Verified / not changed
Non-slow suite: 36 passed, 1 xfailed. The full CLI integration test remains xfail on the separate #41 long-run drift (as documented). |
neuromechanist
deleted the
39-amica_numpy-long-fit-hits-non-finite-likelihood-nan-iter-687-on-sample-data
branch
July 5, 2026 03:29
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 Fortran's NaN-recovery to the NumPy backend. The observed failure (CLI diverging to a non-finite likelihood ~iter 687) is a stochastic, init-dependent instability: with a fixed seed the fit is stable, and NG does the identical unfloored exact-EM
mu/betaupdate, so it is a shared algorithm property, not a NumPy-specific bug. Fortran's only mitigation is a restart-on-NaN, which the NumPy backend lacked entirely.Changes
amica17.f90:1027-1060): if the LL goes non-finite atiter <= restartiter(default 10), reinitialize from a fresh RNG draw and start over, up tomaxrestarts(default 3); a later NaN stops the fit (Fortran exits too). Newrestartiter/maxrestarts/numrestartsparams and a_reinitialize_for_restarthelper._check_convergenceguarded itsll[-2]comparison onself.iter, which keeps counting across restarts; guard onlen(self.ll)instead so a post-restart first iteration doesn'tIndexError.--seed 0intest_sample_data_cliso the integration test doesn't depend on a random draw.Verification
Honest status on the CLI integration test
test_sample_data_clistays xfail — this PR is progress, not a pass. The NaN is handled (seed pin + restart => all 2000 iters, no early stop), but a separate long-run drift remains: at ~150 iters seed=0 matches Fortran 32/32 (min corr 0.92), yet by 2000 iters the LL degrades (-3.404 -> -3.409) and some component correlations drop below the 0.8 gate. Fortran stays converged over the same run, so this is a convergence-schedule/stability parity gap, filed as #41. The xfail reason points at #41.Related