qspec logo qspec logo

A physics toolbox for laser spectroscopy.

Static Badge Static Badge Static Badge

Static Badge Static Badge

© 2026 Patrick Müller. All rights reserved.

qspec.models. fit  (  model ,  x ,  y ,  sigma_x=None ,  sigma_y=None ,  report=False ,  routine=None ,  guess_offset=False ,  mc_sigma=0 ,  **kwargs  )[source]

Fit a Model from qspec.models to data. This fit routine encapsulates qspec.analyze.curve_fit and qspec.analyze.odr_fit to facilitate the use of all modular fit model features, such as priors, see Model.set_fix, and Linked fitting.

Parameters:
model_base.Model

The Model to fit.

xAny

The input data. Can be any object accepted by the model. If model is a Linked model, x should be a list of objects compatible with the linked models.

yarray_like

The y data. This has to be a 1d-array or a list of 1d-arrays if model is a Linked model.

sigma_xarray_like | None

The uncertainties of the x values. This is only compatible with Monte-Carlo sampling and the odr_fit routine. If sigma_x is not None, no routine is specified and mc_sigma == 0, the routine is automatically set to odr_fit.

sigma_yarray_like | Callable | None

The uncertainties of the y values. This has to be a 1d-array or a list of 1d-arrays if model is a Linked model and have the same shape as y. If the routine is curve_fit, sigma_y may be a function g such that g(x, y, model(x, *params), *params) -> sigma, e.g., sigma_poisson. g should accept the same x as the model while y and model(x, *params) should be 1d-arrays.

reportbool

Whether to print the fit results.

routineCallable | str | None

The routine to use for fitting. Can be either "curve_fit" or "odr_fit", see ROUTINES. If None, curve_fit is used. See sigma_x for one exception.

guess_offsetbool

Guess initial parameters for Offset models, see Offset.guess_offset. Currently, this is not working if x is not a 1d-array.

mc_sigmaint

The number of samples to generate. If 0, no Monte-Carlo sampling will be done. This is not available with linked fitting.

kwargs

Additional keyword arguments are passed to the fit routine.

Returns:
(popt, pcov, info)tuple[ndarray, ndarray, dict]

The optimized parameters, their covariance matrix and a dictionary with information about the fit. Its keys are "warn" (whether a warning occurred), "err" (whether the fit failed) and "chi2" (the reduced $\chi^2$, see reduced_chi2).

Raises:
ValueError

If the specified routine is not supported, if x or y is not iterable for a Linked model, or if sigma_y is callable and mc_sigma > 0.

TypeError

If model is not a Model or if model is a Linked model and mc_sigma > 0.