- 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
Modelfromqspec.modelsto data. This fit routine encapsulatesqspec.analyze.curve_fitandqspec.analyze.odr_fitto facilitate the use of all modular fit model features, such as priors, seeModel.set_fix, andLinkedfitting.- Parameters:
-
- model_base.Model
The
Modelto fit.- xAny
The input data. Can be any object accepted by the model. If
modelis aLinkedmodel,xshould 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
modelis aLinkedmodel.- sigma_xarray_like | None
The uncertainties of the x values. This is only compatible with Monte-Carlo sampling and the
odr_fitroutine. Ifsigma_xis notNone, no routine is specified andmc_sigma == 0, the routine is automatically set toodr_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
modelis aLinkedmodel and have the same shape asy. If the routine iscurve_fit,sigma_ymay be a functiongsuch thatg(x, y, model(x, *params), *params) -> sigma, e.g.,sigma_poisson.gshould accept the samexas themodelwhileyandmodel(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", seeROUTINES. IfNone,curve_fitis used. Seesigma_xfor one exception.- guess_offsetbool
Guess initial parameters for
Offsetmodels, seeOffset.guess_offset. Currently, this is not working ifxis 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$, seereduced_chi2).
- Raises:
-
- ValueError
If the specified routine is not supported, if
xoryis not iterable for aLinkedmodel, or ifsigma_yis callable andmc_sigma > 0.- TypeError
If
modelis not aModelor ifmodelis aLinkedmodel andmc_sigma > 0.