Skip to content

Add full-fit NumPy<->NG parity test (#37) - #38

Merged
neuromechanist merged 2 commits into
mainfrom
37-lock-full-fit-numerical-parity-between-amica_numpy-and-amicatorchng
Jul 4, 2026
Merged

neuromechanist merged 2 commits into
mainfrom
37-lock-full-fit-numerical-parity-between-amica_numpy-and-amicatorchng

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Summary

Locks the numerical relationship between the two kept implementations (AMICA_NumPy and AMICATorchNG). We keep both — NumPy is NG's fast, readable oracle (the per-block parity tests copy params between them) and the CLI backend — so this adds the missing full-fit head-to-head check.

What it does

test_full_fit_parity_numpy_vs_ng (slow) fits both backends independently on the real sample EEG data with a matched single-model config, then asserts they converge to the same solution:

  • Hungarian-matched total-spatial-filter (unmixing @ sphere) correlation, bijective 32/32, min > 0.99.
  • Same per-sample-per-channel LL (|diff| < 1e-2).

Measured (thresholds set from data, not guessed)

  • Hungarian-matched correlation: min = mean = 1.000000, 32/32 bijective.
  • LL: NumPy reports the un-normalized total (−3,322,634); normalized by n_samples * n_channels that is −3.40389, matching NG's −3.403887 to ~1e-5.

This complements the existing per-block sufficient-stat parity tests (which pin the update math to 1e-8) with an end-to-end convergence check. A full fit is intentionally not asserted bit-identical (float reduction order / BLAS vs ATen differ across NumPy and PyTorch); the bar is same converged solution.

Related

Testing

test_full_fit_parity_numpy_vs_ng passes (150s, two full 150-iter fits). ruff clean.

Independently fits AMICA_NumPy and AMICATorchNG on the real sample data
(single model, matched config) and asserts they converge to the same
solution: Hungarian-matched total-spatial-filter correlation ~1.0 (32/32
bijective) and the same per-sample-per-channel LL. Locks the NumPy<->NG
relationship end-to-end, on top of the existing per-block sufficient-stat
tests (1e-8). Marked slow (two full 150-iter fits, ~2.5 min).

Note: NumPy reports the un-normalized total LL; NG reports it normalized
by (n_samples * n_channels), so the NumPy value is normalized before the
comparison. Thresholds set from a measured run (min corr 1.000000, LL diff
~1e-5), not guessed.
- Pass do_opt_block=False to AMICA_NumPy so the shared block_size=512
  actually takes effect. NumPy defaults do_opt_block=True, which retunes
  block_size from data timing at fit() (to 128 here), overriding the
  constructor value; NG has no auto-tune, so the two backends were not
  block-matched and NumPy's block size was machine-dependent.
- Remove the tautological bijective assert (linear_sum_assignment on a
  square matrix always returns a full permutation); the min-correlation
  gate is the real check.

Still passes (min matched corr ~1.0, LL within 1e-2).
@neuromechanist

Copy link
Copy Markdown
Member Author

Review (code-reviewer, Sonnet) + resolutions

Ran an independent reviewer + two live runs. Findings addressed in the follow-up commit:

  • Configs weren't block-matched (Important): AMICA_NumPy defaults do_opt_block=True, so fit() auto-retunes block_size from data timing (to 128 on this machine), overriding the shared block_size=512; NG has no auto-tune. Fixed by passing do_opt_block=False so both backends genuinely run at 512 (and NumPy's block size is no longer machine-dependent). Re-ran: still passes.
  • Tautological bijective assert (Minor): linear_sum_assignment on a square matrix always returns a full permutation, so len(np.unique(col)) == NW couldn't fail. Removed it (replaced with a clarifying comment); the matched.min() > 0.99 correlation gate is the real check.

Reviewer confirmed the compared quantities are apples-to-apples (get_weights() @ sphere vs get_unmixing_matrix() @ sphere, both W.T per the #24 convention) and the LL normalization is correct (NumPy total / (n_samples*n_channels) == NG's normalized LL). Real data only, correctly marked slow + skipif.

@neuromechanist
neuromechanist merged commit 60022dd into main Jul 4, 2026
@neuromechanist
neuromechanist deleted the 37-lock-full-fit-numerical-parity-between-amica_numpy-and-amicatorchng branch July 4, 2026 18: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.

Lock full-fit numerical parity between AMICA_NumPy and AMICATorchNG

1 participant