Skip to content

do_sphere=False: reference variance-normalizes channels; pamica uses identity #328

Open

Description

@neuromechanist

Found during the epic #324 Phase 1 review (PR #326). This is an existing divergence, not introduced by the epic, and it is not recorded in docs/guides/amica-differences.md.

Reference behavior

With do_sphere 0, the reference does not skip preprocessing: it normalizes each channel by its standard deviation and accumulates a log-determinant term (pamica/amica15.f90:516-526, the else branch of if (do_sphere)):

!--- just normalize by the channel variances (don't sphere)
call DCOPY(nx*nx,S,1,Stmp,1)
call DSCAL(nx*nx,dble(0.0),S,1)
sldet = dble(0.0)
do i = 1,nx
   if (Stmp(i,i) .gt. dble(0.0)) then
      S(i,i) = dble(1.0) / sqrt(Stmp(i,i))
      sldet = sldet + dble(0.5)*log(S(i,i))
   end if
end do
numeigs = nx

Stmp holds the data covariance at that point, so S = diag(1/sqrt(var_i)) for every channel with positive variance, and numeigs = nx (explicit pcakeep is ignored, which pamica already matches after PR #326).

pamica behavior

All three array backends (torch, NumPy, MLX) use an identity sphere with sldet = 0 when do_sphere=False, so the data are fitted unscaled.

Consequences

  • The optimization runs on differently scaled data than the reference, so fits with do_sphere=False are not trajectory-comparable to the binary (the natural-gradient EM is not scale-invariant through its initialization A = I + noise and step sizes).
  • The reported log-likelihood differs by the Jacobian term.
  • The reference's own term looks inconsistent: log|det S| for S = diag(1/sqrt(var_i)) is sum_i log(1/sqrt(var_i)) = -0.5 * sum_i log(var_i), but the reference accumulates 0.5*log(S(i,i)) = -0.25 * log(var_i) per channel, half of that. Because the term is a constant added to every model's per-sample log-likelihood, it cancels in model posteriors and does not affect the fitted parameters, only the reported likelihood.

Decision needed

  • Port the reference's per-channel variance normalization for do_sphere=False to torch, NumPy and MLX (a shared decision; cross-backend test against the binary with do_sphere 0).
  • Decide the log-determinant term: reproduce the reference's 0.5*log(S(i,i)) for likelihood parity, or use the correct log(S(i,i)) and record that divergence (with an escape hatch, per .rules/backend_parity.md).
  • Until then, record the current divergence in docs/guides/amica-differences.md (epic Epic: make MLX a first-class, end-to-end backend #324 Phase 6 does this).

Activity

  1. added
    bug
    on Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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