Skip to content

doscaling normalizes stored columns, not components (all backends) #333

Closed

Description

@neuromechanist

Found during epic #324 (component-orientation investigation). Affects every default fit on all three array backends (torch, NumPy, MLX); doscaling=True is the default.

Symptom

With doscaling on, pamica's per-iteration scaling is not the reference's. Seeded from the same initialization and compared with the native reference binary (arm64 build, parameters loaded through Fortran's load_*), the first iteration already differs: A by 7.2e-5 and mu by 6.6e-5 after 1 iteration, 1.4e-3 / 6.7e-3 after 3. With doscaling off, pamica matches the binary to 1e-15 (A) and 2e-11 (mu). Endpoint parity metrics hide it because they are blind to per-component scale: after 100 seeded iterations with Newton the log-likelihood differs by 2.3e-4 and the Hungarian-matched correlation is 0.999987, but the fitted A column norms range over [0.940, 1.070] (the reference: exactly 1) and mu/sbeta differ by 0.057 / 0.085. In two-model fits the column norms reach [0.43, 1.96].

Root cause

pamica stores each model's mixing block transposed relative to the reference (issue #24 convention: get_mixing_matrix returns A[:, comp_list[:, h]].T), so component i of model h is row i of the stored block, not stored column comp_list[i, h]. The reference normalizes component columns (amica15.f90:1843-1851: A(:,k) /= ||A(:,k)||, mu(:,k) *= ||...||, sbeta(:,k) /= ||...||), which is an exact change of scale. pamica normalizes stored columns (torch pamica/torch_impl/core.py _update_parameters, scale = torch.sqrt((self.A**2).sum(dim=0)); same in NumPy _update_parameters and MLX _update_parameters), which is not a change of scale of any component and perturbs the fit every iteration.

Independent check (lead, real sample EEG, single model, 30 iterations): a compensated rescale of stored row 5 (A[5,:] /= s, mu[:,5] *= s, beta[:,5] /= s) changes the log-likelihood by exactly 0.0; the same compensated rescale of stored column 5 changes it by -3.9e-2.

Fix (epic #324 Phase 7)

  • Normalize the component rows of each model block (with the matching mu/beta rescale) in torch, NumPy and MLX; identical decision in all three.
  • Native-binary oracle test (gated like the other Fortran tests): seeded, doscaling on, a few iterations, A/mu/sbeta match the reference to float64 round-off.
  • An exact-invariance test that runs everywhere: one doscaling pass leaves the log-likelihood unchanged.
  • ADR amending ADR 0001 (orientation convention), changelog entry (default trajectories change; scale-blind results do not), re-measure the per-backend parity rows in docs/guides/validation.md.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions