Skip to content

Add Amari distance metric and Fortran/Apple-CPU perf rows - #120

Merged
neuromechanist merged 4 commits into
mainfrom
issue-116-table2-amari-distance
Jul 13, 2026
Merged

neuromechanist merged 4 commits into
mainfrom
issue-116-table2-amari-distance

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Closes #116.

Amari distance

amari_distance() in validate_implementations.py is a permutation- and
scale-invariant unmixing-matrix comparison metric (Amari, Cichocki & Yang,
1996), used alongside (not instead of) the existing Hungarian-matched
correlation as a second, assignment-free conformity check. Recomputed from
the already-saved .context/issue-27/ensemble.npz matrices (real EEG fits
from the #27 study, no re-fitting needed):

  • Single-model: ~0.008 (matches the ~0.997 correlation).
  • Multi-model: within-Fortran 0.174, within-pyAMICA 0.155, between 0.161
    (sd ~0.02), run-level permutation p=0.998 -- agrees with the
    correlation-based "between not worse than within-Fortran" conclusion.

Per-run detail (each of the 40 runs' own within/between mean for both
metrics) is a new table in docs/guides/validation.md, plus
.context/issue-27/per_run_detail.csv (regenerable via
.context/issue-27/amari_distance.py).

Table 2 / performance tables

Table 2 (paper.md) had no Fortran row. Added native Fortran rows (Intel
24-core workstation, Apple Silicon 8-core) from the existing #86 CPU
core-scaling sweep, plus the full core-scaling comparison table (both
platforms, matching Fortran against torch-CPU/NumPy at the same core counts)
in the docs.

Formatting

Both paper.md and docs/guides/validation.md are reflowed to semantic line
breaks per the documented convention. Content is unchanged -- verified via
whitespace-collapsed diff before and after, plus a pandoc PDF build and an
mkdocs build --strict for the docs page.

Test plan

  • uv run pytest pyAMICA/tests/test_amari_distance.py (new: self-distance
    zero, permutation/scale invariance, nonzero for unrelated matrices) passes
  • uv run ruff check / ruff format --check pass
  • uv run ty check passes on all touched Python files
  • pandoc --citeproc paper.md builds a PDF with all citations resolving
  • mkdocs build --strict builds the docs site with no warnings

Closes #116.

amari_distance() in validate_implementations.py is a permutation- and
scale-invariant unmixing-matrix comparison metric (Amari, Cichocki &
Yang 1996), complementary to the existing Hungarian-matched
correlation since it needs no assignment step. Recomputed from the
already-saved #27 ensemble matrices (no re-fitting): single-model
~0.008, multi-model within-Fortran/within-pyAMICA/between
0.174/0.155/0.161, run-level permutation p=0.998, consistent with the
correlation-based equivalence finding.

Table 2 (paper.md) and the docs performance tables gained native
Fortran rows (Intel 24-core workstation, Apple Silicon 8-core) and the
full core-scaling comparison from the #86 sweep, alongside the
existing GPU/torch numbers.

Both paper.md and docs/guides/validation.md are reflowed to semantic
line breaks (content unchanged, verified via whitespace-collapsed
diff).
Review on PR #120 found amari_distance(Wa, Wb) was not symmetric
under argument swap, and the three call sites in
.context/issue-27/amari_distance.py used inconsistent argument
orders, so the JSON summary and per-run CSV didn't reconcile
(between mean differed by ~0.004 depending on which table you read).

amari_distance now averages both directions, making it a proper
(symmetric) distance and fixing the reconciliation for free. Also
added an explicit guard for degenerate all-zero rows/columns and
n<2 matrices, which previously produced a silent NaN.

Recomputed the ensemble numbers with the fixed function: within-
Fortran 0.174, within-pyAMICA 0.154, between 0.163 (was 0.161),
permutation p>0.999 (was 0.998); single-model 0.006 (was 0.008).
Updated paper.md and docs accordingly, and added a caveat that the
model-pairing correction in model_amari contributes some of the
within/between spread.

New tests: symmetry, degenerate-input rejection, and coverage for
the previously-untested model_amari pairing selection and
perm_test_not_worse sign-flip logic (both verified correct on
synthetic data).
@neuromechanist

Copy link
Copy Markdown
Member Author

Review findings and fixes

Ran code-review + test-coverage-analysis on this PR. Summary:

Critical (fixed): amari_distance(Wa, Wb) was not symmetric under argument
swap (verified: ~0.03 difference for swapped arguments on random matrices).
.context/issue-27/amari_distance.py called it in three different argument
orders across pairwise(), per_run_detail(), and perm_test_not_worse(),
so the JSON summary and the per-run CSV didn't reconcile (the "between" mean
differed by ~0.004 depending on which table you read). Fixed by making
amari_distance average both directions (a proper symmetric distance);
recomputed all downstream numbers (paper.md, docs, JSON, CSV) with the fix.
New values: within-Fortran 0.174, within-pyAMICA 0.154, between 0.163
(was 0.161), permutation p>0.999 (was 0.998); single-model 0.006 (was 0.008).
Verified the CSV and JSON now reconcile exactly.

Important (fixed): amari_distance silently produced NaN for a
degenerate all-zero row/column or an n<2 matrix (no guard, no exception).
Added explicit ValueErrors for both cases, plus tests.

Important (documented): model_amari's try-both-pairings model-label
resolution has a non-uniform bias magnitude across groups (~0.02-0.03,
comparable to the within-Fortran/within-pyAMICA gap). Added a caveat sentence
in docs/guides/validation.md noting this contributes to some of the observed
spread, rather than treating the gap as purely a convergence-speed effect.

Test coverage (fixed): added pyAMICA/tests/test_amari_ensemble_script.py
covering model_amari's pairing selection and perm_test_not_worse's
higher_is_worse sign-flip logic on synthetic data (previously untested;
a sign bug there could have silently inverted the reported conclusion).

Not changed (false positive / out of scope): duplication between
.context/issue-27/amari_distance.py's local xcorr/pairwise and
multimodel_ensemble.py's versions -- low-risk, point-in-time analysis
scripts, consistent with existing repo convention.

All tests pass (uv run pytest pyAMICA/tests/test_amari_distance.py pyAMICA/tests/test_amari_ensemble_script.py), ruff/ty clean, pandoc PDF and
mkdocs build --strict both verified with the corrected numbers.

Review on PR #120 flagged that the single-model "Amari distance
~0.008" (now ~0.006) claim in paper.md/docs had no backing
computation anywhere in the repo, unlike every other number in this
PR, which traces to a committed, regenerable artifact.

test_end_to_end_correlation_vs_fortran already runs the real
Fortran-vs-NG comparison this figure needs; added an amari_distance
assertion there (< 0.05, actual ~0.006) alongside the existing
correlation assertion. Also added an explicit None-check on the
Fortran run result, surfaced by ty once run_fortran_amica's return
type was correctly Optional[Dict] (issue #117).
@neuromechanist

Copy link
Copy Markdown
Member Author

Follow-up: single-model Amari distance now has a real test

A second review pass caught that the single-model "Amari distance ~0.008"
claim (now ~0.006, since the symmetry fix above) had no backing computation
anywhere in the repo -- unlike every other number in this PR, which traces to
a committed, regenerable artifact.

Fixed: added an `amari_distance` assertion (`< 0.05`, actual ~0.006) to the
existing `test_end_to_end_correlation_vs_fortran` in
`pyAMICA/tests/torch_tests/test_ng_backend.py`, right alongside the existing
correlation assertion that already backs the ~0.997 figure. This also
surfaced (via `ty`, now that `run_fortran_amica` is correctly typed
`Optional[Dict]` per #117) a missing None-check before subscripting the
Fortran result -- added an explicit `assert fortran is not None`.

Verified: `uv run pytest pyAMICA/tests/torch_tests/test_ng_backend.py::test_end_to_end_correlation_vs_fortran -m slow` passes locally (macOS, real Fortran binary).

@neuromechanist

Copy link
Copy Markdown
Member Author

Skipped (intentional, not a false positive)

Both reviewers flagged that `.context/issue-27/amari_distance.py`'s local
`xcorr`/`pairwise` duplicate the same functions in the pre-existing
`.context/issue-27/multimodel_ensemble.py`, and that the `importlib`-based
loader pattern is repeated across the new script and its test. Skipping:
both are one-off, point-in-time analysis scripts under `.context/`, not
package code (no import path exists between them without adding one), this
matches the repo's existing convention for this class of script (no shared
module for `.context/` analysis helpers today), and the low-risk assessment
from both reviews (confidence ~40-50) agrees this isn't worth the churn of
introducing a new shared module for two files.

Table 1 had grown a near-duplicate row per metric (correlation and
Amari distance each got their own "single run"/"ensemble
equivalence" row), and the surrounding prose restated every number
that was already in the table. Condensed to one row per regime
carrying both metrics side by side, and cut the prose down to the
qualitative narrative (what was tested, why, what it means), leaving
the table as the single source for the actual figures.
@neuromechanist
neuromechanist merged commit 7feda81 into main Jul 13, 2026
7 checks passed
@neuromechanist
neuromechanist deleted the issue-116-table2-amari-distance branch July 13, 2026 22:28
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.

paper+docs: Fortran/Apple-CPU rows in Table 2 + Amari-distance detail

1 participant