- qspec.analyze. curve_fit ( f , x , y , p0=None , p0_fixed=None , sigma=None , absolute_sigma=False , check_finite=True , bounds=(-inf, inf) , method=None , jac=None , full_output=False , report=False , **kwargs )[source]
-
Use non-linear least squares to fit a function $f$ to data. Assumes
y = f(x, *params) + eps. This is a reimplementation ofscipy.optimize.curve_fitwith the additional features of fixing parameters withp0_fixedand the option to use the parametersigmadynamically as a functiong(x, y, f(x, *params), *params) -> sigma.- Parameters:
-
- fCallable
The model function $f$ to fit to the data.
- xarray_like | object
The $x$ data.
- yarray_like
The $y$ data.
- p0array_like | None
A numpy array or an Iterable of the initial guesses for the parameters. Must have at least the same length as the minimum number of parameters required by the function
f. Ifp0isNone, 1 is taken as an initial guess for all non-keyword parameters.- p0_fixedarray_like | None
A numpy array or an Iterable of
boolvalues specifying whether to fix a parameter. Must have the same length asp0.- sigmaarray_like | Callable | None
The $1\sigma$ uncertainty of the
ydata. This can also be a function $g$ such thatg(x, y, f(x, *params), *params) -> sigma.- absolute_sigmabool
Whether
sigmais used in an absolute sense (True) and the covariance matrix reflects these absolute values, or only the relative magnitudes ofsigmamatter (False) and the covariance matrix is scaled by the reduced $\chi^2$.- check_finitebool
- boundstuple[array_like, array_like] | Bounds
- methodLiteral["trf", "dogbox", "lm"] | None
- jacCallable | Literal["2-point", "3-point", "cs"] | None
See
scipy.optimize.curve_fit. Must not be callable ifsigmais callable.- full_outputbool
- reportbool
Whether to print the result of the fit.
- kwargs
- Returns:
-
- (popt, pcov)Any
The optimal parameters and their covariance matrix. Additional output if
full_outputisTrue. Seescipy.optimize.curve_fit. AnOptimizeWarningis issued if the covariance of the parameters cannot be estimated.
- Raises:
-
- ValueError
If either
xorycontain NaNs, ifsigmahas an invalid shape or if incompatible options are used.- RuntimeError
If the least-squares minimization fails.
- TypeError
If the number of parameters exceeds the number of data points for the method
"lm".