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:
objectOutcome of a single
fit()call.- success#
Whether MIGRAD converged to a valid minimum.
Falseis 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, andparametersholds the starting values.- Type:
- 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.
- covariance#
Covariance matrix over the free parameters (rescaled to match
uncertainties), orNoneif unavailable. Row/column order is given bycovariance_parameters.- Type:
- chi_squared#
Least-squares cost at the minimum. With supplied
yerrthis is the weighted chi-squared; with assumed errors it is the raw sum of squared residuals.- Type:
- dof#
Degrees of freedom,
n_pointsminus 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 hasdof >= 1.- Type:
- fixed#
Parameters that were held fixed, and the values they were held at.
- yerr_assumed#
Truewhen noyerrwas supplied and uncertainties were rescaled bysqrt(chi2/dof)(curve_fitabsolute_sigma=Falseequivalent).- Type:
- minos_errors#
Asymmetric
(lower, upper)MINOS offsets per free parameter whenrun_minos=Trueand the fit is valid, elseNone. Display-only – HESSE (uncertainties) remains the canonical error estimate.
- curve(x)#
Evaluate the best-fit model at x.
- Parameters:
x (
ArrayLike) – Points at which to evaluate the fitted model.- Return type:
- Returns:
numpy.ndarray– The model with its best-fit parameters at eachx.- 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
Candtheta; 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 fromuncertaintiesalone ignores the correlations between parameters. This evaluatesfuncat the best fit and propagatescovariancethrough it, with no refit.The quantity is the caller’s to define, deliberately. The fit does not know the mole basis of the
chiit was given, so a “built-in”mu_effwould happily turn a raw emu/Oe Curie constant into a value with the wrong normalisation. Convertchito the basis you mean (e.g. withchi_to_chi_molar_per_ion()) before fitting, then say what to derive.- Parameters:
func (
Callable[[Mapping[str,float]],float]) – Called asfunc(parameters)with a freshdictof every parameter, free and fixed (a copy ofparameters, 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:
- Returns:
value (
float) –funcat the best fit.err (
float) – Its first-order (delta-method) 1-sigma uncertainty,sqrt(J C J^T), withCthecovarianceovercovariance_parametersandJthe gradient offuncwith respect to those parameters. It inherits the fit’s error convention: absolute with suppliedyerr, residual-scaled whenyerr_assumed.
- Raises:
Notes
Jis taken by central differences, stepping each free parameter by1e-3of its own HESSE sigma: scale-free (the step for achi0of 1e-4 and athetaof 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 callingfunc: where MIGRAD stopped is not an estimate to derive anything from, matching the NaN failure rows offit_windows();a non-finite value (
sqrtof a negativeC, 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
funcat 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
funcover the parameters’ uncertainty. That is exact for a linearfuncand good whenever the relative errors are small, but it breaks down when they are large andfuncis strongly non-linear on that scale –1/xorsqrt(x)of a parameter known to 30 %, or near zero. The symmetric error it returns is then misleading. Use a profile-likelihood interval (MINOS, viarun_minos=Trueon 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: