Skip to content

Reject: distinguish non-finite LL from too-aggressive rejsig (#127) - #129

Merged
neuromechanist merged 2 commits into
mainfrom
127-reject-nonfinite-ll-message
Jul 14, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
127-reject-nonfinite-ll-message

Conversation

@neuromechanist

@neuromechanist neuromechanist commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

Summary

Closes #127. Makes both backends' _reject_outliers report the real cause when
outlier rejection would drop every sample.

Builds on #126 (now merged); rebased onto main.

Problem

Both AMICATorchNG._reject_outliers and AMICA_NumPy._reject_outliers raised:

Outlier rejection removed all N samples (rejsig=X too aggressive for this data).

whenever the keep-mask was empty. But for finite log-likelihoods with the
construction-validated rejsig > 0, the maximum sample is always >= mean >= mean - rejsig*std, so it is always kept -- the good set can only empty when the
per-sample LL is non-finite (a single NaN poisons mean/std, making every
comparison False). That is upstream numerical instability (singular W /
overflow), which a user cannot fix by tuning rejsig; the old message
misdiagnosed it.

Change

On an empty keep-mask, both backends now count the non-finite entries and raise
an accurate instability message naming the real cause. The old rejsig message
is kept only as a defensive backstop (now unreachable for finite LL with
rejsig > 0). The two backends raise the same message style, fixed together to
avoid cross-backend divergence.

Tests

Both backends' degenerate-LL tests now inject a single NaN (demonstrating one
non-finite entry poisons the whole reject decision) and assert the non-finite
message:

  • test_numpy_reject.py::test_rejection_nonfinite_ll_raises_instability_error
  • test_ng_backend.py::test_rejection_degenerate_ll_raises_clear_error

Validation

  • uv run ty check . -> 0 diagnostics; ruff clean.
  • All reject tests pass (6 NumPy + 3 torch).

Both backends' _reject_outliers raised "removed all N samples (rejsig too
aggressive)" whenever every sample was dropped. But for finite log-likelihoods
with the construction-validated rejsig>0, the max sample is always kept, so the
only way the good set empties is a non-finite per-sample LL (one NaN poisons
mean/std, making every comparison False) -- i.e. upstream numerical instability
(singular W / overflow), which the user cannot fix by tuning rejsig.

On an empty keep-mask, both backends now count the non-finite entries and raise
an accurate instability message; the old rejsig message is kept only as a
defensive (now unreachable for finite LL) backstop. Tests updated in both
backends to inject a single NaN and assert the non-finite message.
Silent-failure review noted the new non-finite-LL branch is effectively
unreachable from a normal fit(): both backends' earlier aggregate
non-finite-LL guard (the LL sum is non-finite iff a per-sample term is)
stops the loop first, so this branch mainly serves direct callers of
_reject_outliers and is defense in depth. Note that scope in the comment.

Also make the message actionable (matching the sibling singular-W message's
house style): name a likely remedy (rank-deficient/average-referenced data,
lower learning rate) rather than only what is NOT wrong.
@neuromechanist

Copy link
Copy Markdown
Member Author

/review-pr summary (2 Sonnet reviewers: code + silent-failure)

No Critical/Important correctness issues in either review; both verified the change end-to-end (including +inf/overflow edge cases, single-NaN poisoning, raise-before-mutate, cross-backend consistency, and that the backstop raise is genuinely dead code for finite LL with rejsig>0). Two points addressed in commit 8f9bbdc:

  • Scope (silent-failure Medium): the new accurate branch is effectively unreachable from a real fit() -- both backends' earlier aggregate non-finite-LL guard (the LL sum is non-finite iff a per-sample term is) stops the loop first, so users see nan_ll/singular_ll (torch) or the non-finite convergence stop (numpy), not this message. So this fix is defense-in-depth for direct callers of _reject_outliers, not an observed-in-prod change. Noted explicitly in the code comment (and the PR framing).
  • Actionable message (Low): the message now names a likely remedy (rank-deficient/average-referenced data, lower learning rate), matching the sibling singular-W message's house style, rather than only stating what is not wrong.

Not changed (accepted): the mismatched test names (code review, confidence ~30, 'not worth a revision') and the theoretical astronomically-large-finite-LL overflow gap (silent-failure Low, not physically reachable in this pipeline).

Validation: ty check . 0 diagnostics, ruff clean, all reject tests pass (6 NumPy + torch degenerate).

@neuromechanist
neuromechanist merged commit b14ad14 into main Jul 14, 2026
6 checks passed
@neuromechanist
neuromechanist deleted the 127-reject-nonfinite-ll-message branch July 14, 2026 16:14
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 50.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.82%. Comparing base (9fdc76c) to head (8f9bbdc).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
pyAMICA/numpy_impl/core.py 50.00% 1 Missing and 1 partial ⚠️
pyAMICA/torch_impl/core.py 50.00% 1 Missing and 1 partial ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #129      +/-   ##
==========================================
- Coverage   80.34%   79.82%   -0.52%     
==========================================
  Files          14       14              
  Lines        2096     2102       +6     
  Branches      355      357       +2     
==========================================
- Hits         1684     1678       -6     
- Misses        300      311      +11     
- Partials      112      113       +1     
Files with missing lines Coverage Δ
pyAMICA/numpy_impl/core.py 83.16% <50.00%> (-1.60%) ⬇️
pyAMICA/torch_impl/core.py 92.10% <50.00%> (-0.25%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

Outlier rejection: distinguish non-finite per-sample LL from a too-aggressive rejsig

2 participants