Skip to content

Normalize numpy_impl log-likelihood to Fortran's scale - #215

Merged
neuromechanist merged 2 commits into
mainfrom
212-numpy-ll-normalization
Aug 10, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
212-numpy-ll-normalization

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Addresses the log-likelihood half of #212. Found while implementing #207, which had assumed numpy_impl was the Fortran-faithful reference for the convergence criteria.

The bug

Fortran stores a normalized per-iteration log-likelihood (amica15.f90:1752):

LL(iter) = LLtmp2 / dble(numgoodsum*nw)

numpy_impl stored the raw sum. On the bundled sample AMICA_NumPy reported -3317862.78 where Fortran reports about -3.3, a factor of roughly 1e6 (30504 samples x 32 channels). Since min_dll defaults to 1e-9 and is compared against ll[-1] - ll[-2], use_min_dll could never fire from genuine convergence: the maxincs counter around it was correct but unreachable.

After this change, the same fit reports ll[-1] = -3.443.

Why it survived

Four call sites were hand-compensating, each locally correct:

  • pamica/tests/torch_tests/test_ng_backend.py divided by (n_samples * NW) before comparing backends
  • benchmarks/benchmark_dimsweep.py divided by (data.shape[0] * data.shape[1]) before reporting
  • pamica/tests/test_sample_data.py divided by num_samples before checking against the written LLt

All three now compare directly. That pattern is the reason the wrong convention was invisible: nothing ever saw the raw value in a context where it looked wrong.

Deliberately not fixed here

self.nd (weight-gradient norm) has the same class of defect and is worse: after this change it still reads ~5.4e3 where Fortran reports ~1e-5. Fortran divides dAk by zeta before squaring and masks by comp_used (amica15.f90:1739-1743); numpy_impl does neither. AMICATorchNG does both, so this is a NumPy-only divergence. Consequence: min_nd/use_grad_norm is unreachable there too, so both Fortran convergence criteria are dead in this backend rather than one.

A first attempt at the comp_used mask alone was reverted. It did not address the scale, which is the actual problem, and it regressed test_newton_mstep_matches_numpy_reference by asserting comp_used is not None on a path where the M-step runs before that attribute is set. A comment at the site now records the divergence instead of leaving it silently wrong. Remaining work is tracked in #212.

Verification

  • pamica/tests/torch_tests/test_ng_backend.py: 38 passed, including the cross-backend LL comparison that previously needed the manual rescale.
  • pamica/tests/test_numpy_reject.py: passed; the do_reject reasoning still holds, since dropping below-average samples raises a mean just as it raised a sum.
  • No PyTorch-side behaviour changes. The only edit to torch_impl/core.py is a comment correcting a now-stale claim that the NumPy backend compares un-normalized log-likelihood.

numpy_impl stored a raw sum over samples and channels while Fortran
stores LL(iter) = LLtmp2 / dble(numgoodsum*nw) (amica15.f90:1752), so
AMICA_NumPy reported -3317862.78 where Fortran reports about -3.3 on the
bundled sample. min_dll defaults to 1e-9 and is compared against that
delta, so use_min_dll could never fire from real convergence.

Four call sites had been hand-compensating for the wrong scale and now
stop: the cross-backend LL comparison in test_ng_backend.py, the results
column in benchmark_dimsweep.py, and the LLt check in test_sample_data.py.

The gradient norm self.nd has the same class of defect and is NOT fixed
here; a comment at the site records why. See issue #212.

Closes #212 partially
@neuromechanist neuromechanist linked an issue Aug 10, 2026 that may be closed by this pull request
4 tasks
@neuromechanist
neuromechanist merged commit a0ef243 into main Aug 10, 2026
7 checks passed
@neuromechanist
neuromechanist deleted the 212-numpy-ll-normalization branch August 10, 2026 00:52
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.

numpy_impl log-likelihood is un-normalized, so min_dll can never fire

1 participant