- 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
fromqspec.models
to data. This fit routine encapsulates theqspec.curve_fit
and theqspec.odr_fit
routines to facilitate the use of all modular fit model features, such as *Priors* andLinked
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 notNone
, no routine is specified andmc_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 iscurve_fit
, sigma may be a functiong
such thatg(x, y, model(x, *params), *params) -> sigma
.g
should accept the samex
as themodel
whiley
andmodel(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. Seesigma_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 specifiedmodel
is not aqspec.models.Model
(TypeError
).