calamari.fitting.engine.fit#

calamari.fitting.engine.fit(model, x, y, *, yerr=None, initial=None, limits=None, fixed=None, run_minos=False)#

Least-squares fit of model to (x, y) via iminuit MIGRAD + HESSE.

Parameters:
  • model (str | ModelDefinition) – A registered model name (see calamari.fitting.MODELS) or a ModelDefinition directly.

  • x (ArrayLike) – Finite one-dimensional independent and dependent data of equal length. Recast to float arrays; select valid observations explicitly.

  • y (ArrayLike) – Finite one-dimensional independent and dependent data of equal length. Recast to float arrays; select valid observations explicitly.

  • yerr (ArrayLike | None) – Per-point 1-sigma errors. If None (default), the scatter is assumed constant and estimated from the residuals, and the reported uncertainties are the unit-error ones rescaled by sqrt(chi2/dof) (curve_fit absolute_sigma=False equivalent); the result then has yerr_assumed=True. The minimisation itself runs against a sigma on the data’s own scale, not 1, so that data in small units (a susceptibility in emu/Oe) converge – see the module docstring. A scalar broadcasts to every point. Supplied values must be finite and strictly positive, with one value per point or a scalar.

  • initial (dict[str, float] | None) – Starting values overriding the model defaults / data guess.

  • limits (dict[str, tuple[float, float]] | None) – {name: (lower, upper)} box constraints; a bound may be None or -inf (lower) or +inf (upper) to leave that side unconstrained. Endpoints are inclusive, including equal bounds. For a parameter also in fixed, bounds must be ordered and contain no NaN.

  • fixed (dict[str, float] | None) – {name: value} parameters to hold constant. Fixed parameters are excluded from the degrees of freedom. A value also constrained by limits must be finite and lie within its inclusive interval.

  • run_minos (bool) – When True and the fit is valid, also run MINOS and report the asymmetric bands in minos_errors (display-only; HESSE stays canonical). Default False.

Return type:

FitResult

Returns:

calamari.fitting.engine.FitResult – The fit outcome. On a numerical fit failure (an arithmetic/linear-algebra exception or an invalid minimum) this is returned with success=False rather than propagating the error. So is a problem with no more points than free parameters (dof <= 0): it is not minimised, and the result carries the starting values, no uncertainties, and the true, unclamped dof. No warning is issued; success is the signal, as for every other failure.

Notes

Unexpected model/backend exceptions propagate, including during MINOS. ArithmeticError and numpy.linalg.LinAlgError during MINOS leave the valid HESSE result available with minos_errors=None.

Raises:
  • FitInputError – If observations are non-finite, not one-dimensional or misaligned, or supplied uncertainties are non-finite, non-positive or misaligned. Raised before minimisation, including for sparse inputs.

  • FitConstraintError – If a parameter in both fixed and limits has a fixed value outside its permitted interval or is non-finite, or the interval has reversed or NaN bounds. This caller error raises before minimisation, even for empty or underdetermined data. Correct the fixed value or its bounds; it never becomes success=False.

  • UnknownModelError – For an unknown model name (also a KeyError).

  • FitParameterError – For a name in initial, limits or fixed that is not one of the model’s parameters (also a KeyError). These are caller errors, not fit failures, so they raise whatever the data – including on a problem too sparse to fit – rather than coming back as success=False.