Skip to content

Apply the LL-decrease response before the update #339

Closed

Description

@neuromechanist

Symptom

When the log-likelihood (LL) decreases, all three backends (torch, NumPy, MLX) apply the reference's response (halve lrate, scale rholrate, count numdecs, and at maxdecs ratchet lrate0, rholrate0 and newtrate) one parameter update later than the reference does. This is on the default path; it does not need Newton or rejection. Measured against the pinned v0.3.3 binary, seeded from pamica's initialization (lrate=0.5, no Newton, 30 iterations): the LL deviation jumps from 5.4e-6 to 3.6e-3 on the iteration after the first decrease. A prototype that applies the response before the update stays at or below 1.0e-5 throughout.

Root cause

In the reference's main loop (pamica/amica15.f90:1055-1122), iteration iter computes LL(iter) and the sufficient statistics, runs the LL-decrease response and the stopping checks (1055-1097), exits on leave (1111), and only then calls update_params (1122), so the halved lrate scales the same iteration's update. pamica runs _update_parameters first and applies the response afterward, so the halved rate reaches only the next iteration's update. The stop has the same ordering difference: the reference exits before the update, while pamica breaks after it, so final_ll_ currently describes the pre-update parameters rather than the returned ones (keep_best usually masks this, since it restores the best iterate).

Fix

Split each backend's iteration so the E-step and the gradient-norm accumulation (the reference's ndtmpsum is needed by the decrease check) run first, then the LL-decrease response and stop checks, then the M-step with the updated rates, exactly in the reference's order. Do it identically in all three backends, preferably through the shared schedule helpers in pamica/schedule.py (#335). Decide and document the returned state on a stop so that final_ll_ matches the returned parameters. Gates: a seeded native-binary oracle over a trajectory with LL decreases (and one with a maxdecs ratchet) matching to float64 round-off, and an always-on cross-backend test of the response timing through real fits.

Found during epic #324 Phase 9 (#335, PR #338). Part of epic #324.

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