calamari.fitting.engine.FitResult#

class calamari.fitting.engine.FitResult(success, model, parameters, uncertainties, covariance, covariance_parameters, chi_squared, reduced_chi_squared, dof, n_points, fixed, yerr_assumed, minos_errors=None, _function=None, _param_order=())#

Bases: object

Outcome of a single fit() call.

success#

Whether MIGRAD converged to a valid minimum. False is a normal return value (e.g. too few points, a degenerate model), not an error. “Too few” means no more points than free parameters (dof <= 0): that problem is never minimised, and parameters holds the starting values.

Type:

bool

model#

Name of the fitted model.

Type:

str

parameters#

Best-fit value for every parameter (free and fixed).

Type:

dict of str to float

uncertainties#

HESSE 1-sigma uncertainties for the free parameters (rescaled when yerr_assumed; see the module docstring). Empty if HESSE failed; fixed parameters are reported with 0.0.

Type:

dict of str to float

covariance#

Covariance matrix over the free parameters (rescaled to match uncertainties), or None if unavailable. Row/column order is given by covariance_parameters.

Type:

numpy.ndarray or None

covariance_parameters#

Free-parameter names labelling covariance’s axes.

Type:

tuple of str

chi_squared#

Least-squares cost at the minimum. With supplied yerr this is the weighted chi-squared; with assumed errors it is the raw sum of squared residuals.

Type:

float

reduced_chi_squared#

chi_squared / dof; NaN when the problem was not minimised.

Type:

float

dof#

Degrees of freedom, n_points minus the number of free parameters (fixed parameters do not consume a degree of freedom). Reported as is, not clamped: an underdetermined problem shows zero or a negative number, which is why it failed. Every successful fit has dof >= 1.

Type:

int

n_points#

Number of data points fitted.

Type:

int

fixed#

Parameters that were held fixed, and the values they were held at.

Type:

dict of str to float

yerr_assumed#

True when no yerr was supplied and uncertainties were rescaled by sqrt(chi2/dof) (curve_fit absolute_sigma=False equivalent).

Type:

bool

minos_errors#

Asymmetric (lower, upper) MINOS offsets per free parameter when run_minos=True and the fit is valid, else None. Display-only – HESSE (uncertainties) remains the canonical error estimate.

Type:

dict or None

curve(x)#

Evaluate the best-fit model at x.

Parameters:

x (ArrayLike) – Points at which to evaluate the fitted model.

Return type:

NDArray[double]

Returns:

numpy.ndarray – The model with its best-fit parameters at each x.

Raises:

RuntimeError – If the result carries no model function (should not happen for a result produced by fit()).

derive(func)#

A quantity derived from the fitted parameters, with its propagated error.

The fit reports C and theta; the physics usually wants something built from them, such as the effective moment. Computing that by hand drops its uncertainty, and computing the uncertainty by hand from uncertainties alone ignores the correlations between parameters. This evaluates func at the best fit and propagates covariance through it, with no refit.

The quantity is the caller’s to define, deliberately. The fit does not know the mole basis of the chi it was given, so a “built-in” mu_eff would happily turn a raw emu/Oe Curie constant into a value with the wrong normalisation. Convert chi to the basis you mean (e.g. with chi_to_chi_molar_per_ion()) before fitting, then say what to derive.

Parameters:

func (Callable[[Mapping[str, float]], float]) – Called as func(parameters) with a fresh dict of every parameter, free and fixed (a copy of parameters, so it may be modified freely), and must return a single real number, e.g. lambda p: mu_eff_from_curie_constant(p["C"]).

Return type:

tuple[float, float]

Returns:

  • value (float) – func at the best fit.

  • err (float) – Its first-order (delta-method) 1-sigma uncertainty, sqrt(J C J^T), with C the covariance over covariance_parameters and J the gradient of func with respect to those parameters. It inherits the fit’s error convention: absolute with supplied yerr, residual-scaled when yerr_assumed.

Raises:
  • TypeError – If func returns something that is not a single number.

  • Exception – Whatever func itself raises is propagated unchanged: that is a fault in the caller’s code, not a fit that failed.

Notes

J is taken by central differences, stepping each free parameter by 1e-3 of its own HESSE sigma: scale-free (the step for a chi0 of 1e-4 and a theta of 1e2 are each sized to that parameter’s precision) and always inside the region the covariance describes. A free parameter with zero sigma contributes no variance whatever the slope, so it is not stepped; nor is one whose step is lost in the rounding of its value (sigma below ~1e-13 of it), whose variance is equally beyond representing. Fixed parameters are not in the covariance and contribute nothing either: their values are exact by assumption.

What comes back when there is nothing to propagate:

  • an unsuccessful fit gives (nan, nan) without calling func: where MIGRAD stopped is not an estimate to derive anything from, matching the NaN failure rows of fit_windows();

  • a non-finite value (sqrt of a negative C, say) gives (nan, nan). NumPy’s own warning about it, if any, is let through: it is the caller’s function reporting on the best fit;

  • no covariance (HESSE failed) gives (value, nan);

  • a non-finite func at a probe point – the best fit sits within a small fraction of a sigma of a domain edge – gives (value, nan): there is no slope to propagate. Floating-point warnings raised at the probe points are silenced, since the caller never asked about those points and the NaN says it all.

First order only. The propagation linearises func over the parameters’ uncertainty. That is exact for a linear func and good whenever the relative errors are small, but it breaks down when they are large and func is strongly non-linear on that scale – 1/x or sqrt(x) of a parameter known to 30 %, or near zero. The symmetric error it returns is then misleading. Use a profile-likelihood interval (MINOS, via run_minos=True on a reparametrised model whose parameter is the derived quantity) or a Monte Carlo over the covariance instead.

Examples

>>> import numpy as np
>>> from calamari import fit, mu_eff_from_curie_constant
>>> from calamari.simulate import curie_weiss_chi
>>> T = np.linspace(20, 300, 57)
>>> rng = np.random.default_rng(3)
>>> chi = curie_weiss_chi(T, 0.5, -20.0, 2e-4) * (1 + 0.01 * rng.standard_normal(T.size))
>>> result = fit("curie_weiss", T, chi)
>>> mu, mu_err = result.derive(lambda p: mu_eff_from_curie_constant(p["C"]))
>>> print(f"mu_eff = {mu:.3f} +/- {mu_err:.3f} muB")
mu_eff = 1.982 +/- 0.014 muB
summary()#

Return an aligned, human-readable summary block.

Lists the fit status and goodness-of-fit, then one line per parameter: free parameters as value +/- uncertainty (with the MINOS band appended when present), fixed parameters marked (fixed). Every free parameter name appears, so the block doubles as a quick check that a fit touched the parameters you expected.

Return type:

str