accelmo.fitting.lm_loop

The Levenberg-Marquardt iteration shared by every closed-orbit / closed-twiss fitter.

class accelmo.fitting.lm_loop.LMPoint(loss, grad, hessian, failed=False, extra=None)[source]

Bases: object

The objective at one point: loss, gradient and Gauss-Newton Hessian.

failed marks a point where the closed orbit was lost. extra is whatever the evaluation wants handed back to on_accept.

Parameters:
  • loss (float)

  • grad (numpy.ndarray)

  • hessian (numpy.ndarray)

  • failed (bool)

  • extra (Any)

loss: float
grad: ndarray
hessian: ndarray
failed: bool = False
extra: Any = None
class accelmo.fitting.lm_loop.LMProblem[source]

Bases: object

What run_levenberg_marquardt() asks of a fit; subclasses override the hooks they use.

loss_only_trials = False

Score each trial point by trial_loss() first and evaluate it in full only if it improves on the best loss, so a rejected step costs a cheap loss evaluation.

evaluate(params, iteration)[source]

The full objective at params.

Return type:

LMPoint

Parameters:
  • params (numpy.ndarray)

  • iteration (int)

trial_loss(params)[source]

The loss alone at params, None when the orbit is lost.

Return type:

float | None

Parameters:

params (numpy.ndarray)

on_accept(params, point)[source]

Sees every accepted point.

Return type:

None

Parameters:
  • params (numpy.ndarray)

  • point (LMPoint)

after_step(params, optimiser)[source]

Runs once the next point is chosen.

Return type:

None

Parameters:
refresh(params)[source]

Runs at every newly accepted point; True if it changed the objective.

E.g. the tracking start conditions were rebuilt from the fitted model. The old losses then no longer compare: the optimiser forgets its best point and the same point is evaluated again before the next step is taken.

Return type:

bool

Parameters:

params (numpy.ndarray)

accelmo.fitting.lm_loop.run_levenberg_marquardt(params, problem, config, *, run_log=None, param_names=None, dashboard=None)[source]

Minimise with Levenberg-Marquardt steps (H + lam·diag H) delta = -g.

A rejected step is not a no-op: the optimiser retries from its best point with more damping, so the next point still needs evaluating. Only the terminal reasons (no curvature to retry from, or damping driven to the ceiling) end the solve early.

With run_log, every accepted iteration’s loss is logged, and its parameters too when param_names names them. dashboard (already in its fit stage) shows each accepted iteration.

Returns the optimiser, whose best_params are the solution, and how the fit ended.

Return type:

tuple[LevenbergMarquardtOptimiser, FitDiagnostics]

Parameters: