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. If x represents the independent variable, y represents an intensity for each value of x, and err represents 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.fit will minimize this value. x, y and err are passed to the residuals function from data.

Attributes:
parinfo

A list of dicts with parameter constraints, one dict per parameter, or None if not given.

params0

Required attribute.

data

Required attribute.

ftol

Relative \(\chi^2\) convergence criterium.

xtol

Relative parameter convergence criterium.

gtol

Orthogonality convergence criterium.

epsfcn

Finite derivative step size.

stepfactor

Initial step bound.

covtol

(DEPRECIATED) Range tolerance for covariance calculation.

maxiter

(DEPRECIATED) Maximum number of iterations. Default: 200

maxfev

Maximum number of function evaluations.

params

The fitted parameters.

xerror

Parameter uncertainties (\(1 \sigma\))

covar

Parameter covariance matrix

chi2_min

Final \(\chi^2\)

orignorm

Initial \(\chi^2\).

rchi2_min

Minimum reduced \(\chi^2\).

stderr

Standard errors estimated from

npar

Number of parameters

nfree

Number of free parameters

npegged

Number of fixed parameters

dof

Degrees of freedom

resid

Residuals

niter

Number of iterations

nfev

Number of function evaluations

status

Status code of fit passed from scipy.optimize.leastsq

message

Success/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.