Logo

A physics toolbox for laser spectroscopy.

Static Badge Static Badge Static Badge

Static Badge Static Badge

© 2025 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 the qspec.curve_fit and the qspec.odr_fit routines to facilitate the use of all modular fit model features, such as *Priors* and Linked fitting.

Parameters:
modelqspec.models._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.

yndarray | Iterable

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

sigma_xndarray | Iterable

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_yndarray | Iterable | Callable

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

reportbool

Whether to print the fit results.

routineCallable | str

The routine to use for fitting. Currently supported are {'curve_fit', 'odr_fit'}. If None, curve_fit is used. See sigma_x for one exception.

guess_offsetbool

Guess initial parameters for Offset models. 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.

kwargsNone

Additional kwargs to pass to the fit routine.

Returns:
(popt, pcov, info)(ndarray, ndarray, dict)

The optimized parameters their covariance matrix and a dictionary containing info about the fit.

Raises:
(ValueError, TypeError)

If the specified routine is not supported (ValueError) or the specified model is not a qspec.models.Model (TypeError).

API navigation