calamari.fitting.engine#
Least-squares fitting driver (iminuit) and its result object.
A thin, deterministic wrapper over iminuit.Minuit driving
iminuit.cost.LeastSquares, matching the asymmetry toolkit’s house
pattern: MIGRAD to the minimum, then an explicit HESSE for canonical symmetric
uncertainties, with MINOS available on request as display-only asymmetric
errors (HESSE stays authoritative). The single entry point is fit(); it
returns success=False for recognised numerical failures; unexpected model or
backend exceptions propagate. That includes a problem with no more
points than free parameters, which is refused before any minimisation: with
zero degrees of freedom the points fix the parameters exactly (or not at all),
so there is no fit to judge and no scatter to estimate uncertainties from.
Error convention (important, and deliberately scipy-compatible)#
When the caller supplies per-point yerr, those errors are taken at face
value and the reported uncertainties are absolute (the
scipy.optimize.curve_fit absolute_sigma=True convention). When yerr
is None, the data are assumed to scatter about the model by an unknown
constant sigma that is estimated from the fit residuals, and the parameter
uncertainties are the unit-error HESSE errors rescaled by sqrt(chi2/dof).
That is exactly curve_fit’s default absolute_sigma=False behaviour, and
it is flagged on the result as yerr_assumed=True so a caller can tell which
regime produced the error bars. chi_squared is then the raw sum of squared
residuals, in the units of y squared.
Why assumed errors are not unit errors#
The obvious implementation – minimise with yerr = 1 and rescale
afterwards – is a trap, because MIGRAD’s convergence test is absolute: it
stops when the estimated distance to the minimum (EDM) falls below ~2e-4 in
units of the cost. With unit errors the cost is the raw sum of squared
residuals, so its size is set by the units y happens to be in. A
susceptibility in emu/Oe is 1e-5 to 1e-7, the cost at the starting point is
already ~1e-9, and MIGRAD declares convergence before it has moved: the fit
reports success=True with the starting guess, or near it. Noise-free
curie_weiss(T, 0.5, -20, 2e-4) over 50-300 K came back as C = 0.575,
theta = -29.7 K, chi0 = -2.6e-5, with no sign that anything was wrong. Very
large data fail the other way: the goal becomes unreachable below the cost’s
own round-off, and a good fit is reported as a failure.
So with assumed errors the fit is minimised against a constant sigma on the
data’s own scale, in two passes. The first uses std(y), which is enough to
bring the cost off the floor. The second uses the first pass’s residual
scatter, sqrt(sum(r^2)/dof) – by definition the sigma
absolute_sigma=False assumes – so the cost sits at ~dof and MIGRAD
converges as it would on honestly weighted data. A constant sigma does not
move the least-squares minimum, and HESSE errors scale with sigma while the
rescaling factor scales with its inverse, so the reported uncertainties are
exactly those of the unit-error recipe; only now the minimum they describe
has actually been reached, and nothing about the fit depends on the units of
y.
Functions
Least-squares fit of model to |
Classes