Skip to content

Add Newton coverage: 3-model, rank-reduced, three-way - #284

Merged
neuromechanist merged 15 commits into
devfrom
feature/issue-272-newton-coverage
Aug 23, 2026
Merged

neuromechanist merged 15 commits into
devfrom
feature/issue-272-newton-coverage

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Aug 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Test-only PR closing the three Newton coverage gaps epic #260's reviews found (PR #271, PR #276). No production code changed.

#272 -- 3-model Newton

The curvature arrays are per-(source, model) and the direction/fallback loop iterates over models, so a 2-model fit cannot distinguish a correct per-model index from a transposition error that happens to line up for exactly two models.

torch: test_newton_three_model_finite_and_shaped added to tests/torch_tests/test_ng_backend.py -- curvature shapes (NW, 3), non-degenerate stop, the full A/W/mu/alpha/beta/rho/gm/c finiteness loop, all 3 unmixing matrices finite.

MLX: test_three_model_newton_mstep_and_fit_are_finite added to tests/mlx_tests/test_mlx_newton.py, mirroring the torch test.

numpy: the existing tests/test_numpy_newton_multimodel.py::test_multimodel_newton_fit_finalizes_curvature (issue #267) already parametrizes num_models up to 3 and asserted curvature shape/finiteness, but not a non-degenerate stop or the non-curvature parameters -- extended minimally with model.converged/stop_reason and full-parameter finiteness checks to match the depth of the new torch/MLX tests.

Cross-backend: added, since it was cheap -- test_three_model_newton_curvature_matches_float64_twin in tests/test_mlx_newton_cross_backend.py extends the existing 2-model MLX-vs-float64-torch curvature/M-step comparison to n_models=3 by only changing the _warm_model/_torch_twin argument (both already parametrize on n_models), and now also carries the n_newton_fallbacks parity and _ndtmpsum (rel=1e-4) checks its 2-model sibling has, so the two tests are at matched depth.

#273 -- rank-reduced Newton

No backend had ever run Newton on rank-reduced data: real EEG projected onto a rank-20 subspace (what Maxwell filtering does to MEG, the #221 report), fitted with automatic rank detection.

Per backend, one new test asserting completion, finite parameters, and a non-degenerate stop: test_rank_reduced_newton_fit_completes in tests/torch_tests/test_ng_rank_deficient.py (now also carries the full A/W/mu/alpha/beta/rho/gm/c finiteness loop, matching numpy/MLX), tests/mlx_tests/test_mlx_newton.py, and tests/test_numpy_newton_multimodel.py.

All three passed without any production change. I probed each backend by hand before committing to the tests (per the NO-MOCKS/real-repro standard): the Newton curvature and 2x2 direction solve are allocated at the model's working dimensionality (post rank-detection), not the input channel count, so nothing in the reduction touches the Newton arithmetic.

tests/test_numpy_share_comps.py::test_rank_reduced_numpy_sharing_completes also now runs with do_newton=True (newt_start=3), closing the numpy leg of the rank-reduction x sharing x Newton three-way as well. It previously kept Newton off behind a comment describing a shape crash on that exact combination -- that crash was issue #267 itself (a share pass collapsing components on rank-reduced data was the scenario the issue was filed from), fixed in PR #271 (dgm[None, :], matching the torch backend's dgm.unsqueeze(0)) and already regression-tested up to 3 models in tests/test_numpy_newton_multimodel.py. With the fix in place the fit completes and converges with finite parameters: a share pass collapses 7 components on the rank-16 sphere (comp_used 25 of 32), well inside the merges-fired invariant the test already checked.

#277 -- three-way: sharing x Newton x pdf family

The three features (share_comps=True, do_newton=True, a non-default pdftype) had only pairwise coverage: sharing x Newton, Newton x families, and sharing x switcher each live in separate tests.

MLX: test_sharing_newton_and_fixed_family_fit_completes (pdftype=2, n_mix=3) and test_sharing_newton_and_adaptive_switcher_fit_completes (pdftype=1, n_mix=1) added to tests/mlx_tests/test_mlx_pdf.py.

torch: the equivalent pair added to tests/torch_tests/test_ng_pdf_families.py, per the issue's note that torch also has only pairwise coverage.

All four assert: finite params, non-degenerate stop, merges fired (comp_used sum < n_comps), Newton fallbacks permitted (not asserted to zero).

Safe-zone config, not the pairwise tests' loose cutoff. All four three-way tests run at comp_thresh=0.99/seed=3, not the comp_thresh=0.9/seed=42 the first version used. .context/issue-264/newton_findings.md's "Non-criterion observation: an aggressive share_comps cutoff" section measures comp_thresh=0.9 on a 2-model fit merging 26-31 of 32 cross-model pairs -- near-total model collapse -- and documents that regime as trajectory chaos: Newton can drive a component to zero curvature there, seed- and config-dependently (seed 42 specifically produces nan_params on MLX at a nearby configuration; a fresh review reproduced the original config completing, but at full 32/32 collapse, inside the chaos zone rather than the two-near-collinear-pairs case the doc calls safe). comp_thresh=0.99 is the doc's verified stable zone, and seed=3 is the doc-verified stable seed the sibling test_low_rank_projected_data_share_fit_completes already uses. Verified locally before committing: at this config 11 of 32 pairs merge for the fixed-family variant and 8 of 32 for the adaptive switcher on both backends -- comfortably short of the chaos threshold, so the comp_used guard stays non-vacuous without courting the documented degeneracy.

Machine robustness

Every new test asserts behavioral invariants only -- finite, non-degenerate stop, shapes, merges-fired with a forgiving threshold -- never exact trajectories or machine-dependent decrease timing, per the epic's Phase 3 CI lesson (.context/issue-264/newton_findings.md). The three-way tests' comp_thresh/seed choice above is part of the same discipline: avoiding a documented chaos regime, not just avoiding hardcoded timing.

Also cleaned up while touching this pattern: test_ng_backend.py's two hardcoded ("nan_ll", "singular_ll") degenerate-stop tuples (the one this PR added and one pre-existing) now reference AMICATorchNG._DEGENERATE_STOP_REASONS instead of duplicating the literal.

Gates

  1. uv run pytest pamica/tests/mlx_tests/ pamica/tests/test_mlx_newton_cross_backend.py pamica/tests/torch_tests/test_ng_backend.py pamica/tests/test_numpy_newton_multimodel.py pamica/tests/torch_tests/test_ng_rank_deficient.py pamica/tests/torch_tests/test_ng_pdf_families.py pamica/tests/test_numpy_share_comps.py -q --no-cov: 199 passed, 5 skipped.
  2. uv run pytest -m "not slow" -n auto: 529 passed, 15 skipped, 1 xfailed.
  3. ruff check / ruff format --check / ty check (whole project): zero diagnostics.

Closes #272
Closes #273
Closes #277

Closes the #272 gap: MLX Newton tests only ran n_models<=2, which
cannot distinguish a correct per-model curvature index from a
transposition error that happens to line up for exactly two models.
The #267 regression test already covered num_models up to 3 but only
asserted curvature shape/finiteness. Add the non-degenerate stop and
full-parameter finiteness checks that the torch/MLX #272 tests carry,
verifying the numpy 3-model Newton path to the same depth.
The existing test_newton_multimodel_finite_and_shaped stops at
n_models=2, which cannot distinguish a correct per-model curvature
index from a transposition error that happens to line up for exactly
two models (issue #272).
Extends the existing 2-model MLX-vs-float64-torch curvature/M-step
comparison to a third model. Cheap: _warm_model/_torch_twin already
parametrize on n_models, so this closes the #272 cross-backend gap
with only the argument changed.
All existing MLX Newton tests fit full-rank 32-channel data. Cover the
#221/#223 Maxwell-filtered MEG route: real EEG projected onto a
rank-20 subspace, fitted with automatic rank detection and Newton on
(issue #273).
Real EEG projected onto a rank-20 subspace, fitted with automatic rank
detection and do_newton on -- the #221 MEG route, untested on the
numpy Newton path until now (issue #273). Passes: self.data_dim sizes
the curvature and the 2x2 direction loop alike, so the reduced rank
did not need a fix.
No test in this module sets do_newton, and rank reduction changes
n_channels/n_comps and the sphere shape mid-preprocess, which the
Newton curvature arrays and closed-form direction inherit
(issue #273, the #221/#223 MEG route).
The three features (share_comps, do_newton, a non-default pdftype)
only have pairwise coverage: sharing x Newton, Newton x families, and
sharing x switcher each live in separate tests. Pin the composition
directly with a fixed-family (pdftype=2) and an adaptive-switcher
(pdftype=1) variant (issue #277).
The torch equivalent of the MLX three-way test: share_comps=True,
do_newton=True, and a non-default pdftype together had only pairwise
coverage until now (issue #277).
The do_newton=False comment was stale: it described a shape crash
that was issue #267 itself, fixed in PR #271 (dgm[None, :]) and
already regression-tested up to 3 models. Turning Newton on here
(newt_start=3) closes the numpy leg of the rank-reduction x sharing x
Newton three-way gap noted in PR #284 -- a share pass collapses 7
components on the rank-16 sphere and the fit still completes finite
and converged.
test_newton_three_model_finite_and_shaped now checks the full
A/W/mu/alpha/beta/rho/gm/c finiteness loop the numpy/MLX #272 tests
already carry, instead of only c and A. Both hardcoded
("nan_ll", "singular_ll") tuples in this file (the one this PR added
and the pre-existing one in test_multimodel_rejection_keeps_bias_c_finite)
now reference AMICATorchNG._DEGENERATE_STOP_REASONS instead of
duplicating the literal.
test_rank_reduced_newton_fit_completes checked only A and the
unmixing matrix. Add the full W/mu/alpha/beta/rho/gm/c finiteness
loop so the torch #273 test carries the same depth as its numpy and
MLX counterparts.
test_three_model_newton_curvature_matches_float64_twin was missing
the n_newton_fallbacks parity check and the _ndtmpsum agreement
(rel=1e-4) that its 2-model sibling
(test_multimodel_newton_mstep_matches_float64_twin) already carries.
comp_thresh=0.9 + seed=42 sits in the near-total-collapse regime
.context/issue-264/newton_findings.md documents as trajectory chaos
(26-31 of 32 cross-model pairs merging on a 2-model fit, seed- and
config-dependent nan_params on MLX). Moved both three-way tests to
the doc's verified stable zone, comp_thresh=0.99/seed=3 (the same
seed test_low_rank_projected_data_share_fit_completes uses). Verified
locally that merges still fire well short of the chaos threshold: 11
of 32 pairs for the fixed-family variant, 8 of 32 for the adaptive
switcher, so the comp_used guard stays non-vacuous.
Same fix as the MLX three-way tests: comp_thresh=0.9/seed=42 sits in
the near-total-collapse regime .context/issue-264/newton_findings.md
documents as trajectory chaos. Moved to the doc's verified stable
zone, comp_thresh=0.99/seed=3. Verified locally: 11 of 32 pairs merge
for the fixed-family variant, 8 of 32 for the adaptive switcher.
@neuromechanist
neuromechanist merged commit ff1404c into dev Aug 23, 2026
8 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-272-newton-coverage branch August 23, 2026 18:30
@neuromechanist neuromechanist mentioned this pull request Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant