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:
objectThe objective at one point: loss, gradient and Gauss-Newton Hessian.
failedmarks a point where the closed orbit was lost.extrais whatever the evaluation wants handed back toon_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:
objectWhat
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:
- Parameters:
params (numpy.ndarray)
iteration (int)
- trial_loss(params)[source]¶
The loss alone at
params,Nonewhen 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:
params (numpy.ndarray)
optimiser (LevenbergMarquardtOptimiser)
- 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 whenparam_namesnames them.dashboard(already in its fit stage) shows each accepted iteration.Returns the optimiser, whose
best_paramsare the solution, and how the fit ended.- Return type:
- Parameters:
params (np.ndarray)
problem (LMProblem)
config (LevenbergMarquardtConfig)
run_log (RunLog | None)
param_names (Sequence[str] | None)
dashboard (FitDashboard | None)