neutronpy.lsfit.Fitter¶
-
class
neutronpy.lsfit.Fitter(residuals, derivatives=None, data=None, params0=None, parinfo=None, ftol=1e-10, xtol=1e-10, gtol=1e-10, epsfcn=None, stepfactor=100.0, covtol=1e-14, maxiter=200, maxfev=None, nofinitecheck=False, nan_policy='omit')[source]¶ Wrapper for LMFIT, which is a high-level extension for scipy.optimize.leastsq. Performs Non-Linear Least Squares fitting using the Levenberg-Marquardt method.
Parameters: - residuals : func
The residuals function, see description below.
- derivatives : func, optional
Derivatives function, to compute the Jacobian of the residuals function with derivatives across the rows. If this is None, the Jacobian will be estimated.
- data : tuple, optional
Default: None
- params0 : list, optional
Default: None
- parinfo : list, optional
Default: None
- ftol : float, optional
Default: 1e-10
- xtol : float, optional
Default: 1e-10
- epsfcn : float, optional
Default: 2.2204460492503131e-16
- stepfactor : float, optional
Default: 100.0
- covtol : float, optional
Default: 1e-14
- maxiter : int, optional
Default: 200
- maxfev : int, optional
Default: 0
- nofinitecheck : bool, optional
Default: False
- nan_policy : str, optional
Default: ‘omit’. Determines how NaN values are handled: ‘raise’, ‘propagate’ or ‘omit’.
Notes
Objects of this class are callable, returning the fitted parameters.
Residuals function The residuals function must return an ndarray with weighted deviations between the model and the data. It takes two arguments, a list of the parameter values and a reference for the attribute
data, a tuple e.g.(x, y, err). In a typical scientific problem the residuals should be weighted so that each deviate has a Gaussian sigma of 1.0. Ifxrepresents the independent variable,yrepresents an intensity for each value ofx, anderrrepresents the error, then the deviates could be calculated as follows:\[d = (y - f(x)) / err\]where f is the model function. If err are 1-sigma uncertainties in
y, then\[\sum d^2\]is the total chi-squared.
Fitter.fitwill minimize this value.x,yanderrare passed to the residuals function fromdata.Attributes: parinfoA list of dicts with parameter constraints, one dict per parameter, or None if not given.
params0Required attribute.
dataRequired attribute.
ftolRelative \(\chi^2\) convergence criterium.
xtolRelative parameter convergence criterium.
gtolOrthogonality convergence criterium.
epsfcnFinite derivative step size.
stepfactorInitial step bound.
covtol(DEPRECIATED) Range tolerance for covariance calculation.
maxiter(DEPRECIATED) Maximum number of iterations. Default: 200
maxfevMaximum number of function evaluations.
paramsThe fitted parameters.
xerrorParameter uncertainties (\(1 \sigma\))
covarParameter covariance matrix
chi2_minFinal \(\chi^2\)
orignormInitial \(\chi^2\).
rchi2_minMinimum reduced \(\chi^2\).
stderrStandard errors estimated from
nparNumber of parameters
nfreeNumber of free parameters
npeggedNumber of fixed parameters
dofDegrees of freedom
residResiduals
niterNumber of iterations
nfevNumber of function evaluations
statusStatus code of fit passed from scipy.optimize.leastsq
messageSuccess/error message
- residuals
nofinitecheck(DEPRECIATED) Does not check for finite values. Default: False
Methods
fit(params0)Perform a fit with the provided parameters. plot(fit_function[, function_str, …])Plots the data and the results of a fit build_param_table([function_str])Builds a table of parameters for including in a plot or for use with print __call__([params0])Call self as a function.