npplus.lsqfit module¶
Linear and non-linear least squares fitters.
For a simple linear least squares fit:
p = regress(data, mod0, mod1, mod2, ..., errs=errs)
returns p model coefficients for best linear fit to data of form
data ~ p[0]*mod0 + [1]*mod1 + p[2]*mod2 + ...``
Use the
model=1
keyword to return more information, such as covariances.For a non-linear least squares fit to a parametrized function:
model = levmar(data, f, p0, arg0, arg1, ..., errs=errs)``
returns callable model with best fit to data of form
data ~ model(arg0, arg1, ...) = f(p, arg0, arg1, ...)
model.p
are the best fit parameters,model.pcov
their covariances.
- exception LevmarError[source]¶
Bases:
ValueError
Levenberg-Marquardt algorithm lambda runaway, a kind of ValueError.
- class ModelFit(f, p, pcov=None, chi2=None, ndof=None, isgenf=None, info=None, **kw)[source]¶
Bases:
object
Best fit model of a parametrized family of models.
If model is a ModelFit instance for a parametrized function
f(p, a1, a2, ...)
, thenmodel(a1, a2, ...)
is the function with p set to the fit values, whilemodel.p
are the parameters themselves.- f¶
The family of parameterized models.
- Type
function or generator function
- p¶
The best fit parameters.
- Type
1D ndarray
- pcov¶
Covariances of p. Note that these scale as
errs**2
,the specified errors in the data. If no errs were provided for the data, these covariances are relative; see chi2pcov below.- Type
2D ndarray
- chi2¶
The chi2 per degree of freedom of the best fit to the data. Again, this scales as
1/errs**2
.- Type
- ndof¶
The number of degrees of freedom in the fit (number of data points minus number of free parameters).
- Type
- info¶
More detailed information about the fit, such as the function that did it and how it was configured, the number of iterations, the initial guess and corresponding chi2, etc.
- Type
- property chi2pcov¶
Covariance of parameters, errors estimated from quality of fit.
Covariances estimated from the quality of this fit, assuming that no errs were provided to go with the data. This amount to assuming that the chi2 per degree of freedom of the fit is 1.0, and scaling errs retroactively to make that true. Every statistical package provides this but warns against taking it too seriously.
- chi2prob(chi2=None)[source]¶
Probability that chi2 per dof is greater than specified value.
- Parameters
chi2 (array_like, optional) – Chi square per degree of freedom. If not specified, will use
self.chi2
. Note that this is chi2 per degree of freedom.- Returns
prob – Probability that chi2 of fit will be greater than given chi2.
- Return type
ndarray
- property chiperr¶
Std deviations of params, errors estimated from quality of fit.
The square root of the diagonal of chi2pcov.
- property perr¶
Standard deviations of parameters.
The square root of the diagonal of pcov.
- levmar(data, f, p0, *args, **kwargs)[source]¶
Least squares fit a non-linear model to data.
model = levmar(data, f, p0, x)
is the function
f(p, x)
for the particular parameters p such thatmodel(x)
approximates dataas nearly as possible in a least squares sense. The model object has attributes you can use to retrieve the best fit parameter values, convariances, and other information about the fit.
If you can calculate the partial derivatives of f with respect to p, you should write f as a generator function with two yield statements. The first yield statement must return the function value, while the second and final yield must return the partial derivatives dfdp. The shape of dfdp must match those of f, which is the shape of data, plus a leading axis with the size and order of the parameter vector p.
If f is not a generator function, levmar will estimate its partial derivatives using finite differences. You may need to provide prel and pabs keywords to get appropriate step sizes for the finite differences of your parameters.
- Parameters
data (array_like) – The data to be fit.
f (function or generator function) –
A parametric function
f(p, a1, a2, ..., k1=k1, k2=k2, ...)
representing the model for the data. The first argument to f must be a 1D array of parameters. Any remaining positional or keyword arguments to f represent the independent variables of the model. The result of f must have the same dimensions as data.f may also be a generator function with two yield statements returning dfdp on the second as described above.
p0 (array_like) – The initial guess for the 1D parameters p needed to fit data.
a1 (arbitrary) – The model function f may have any number of postional arguments beyond its first argument p; any additional positional arguments to levmar will be passed unexamined to every call of f.
a2 (arbitrary) – The model function f may have any number of postional arguments beyond its first argument p; any additional positional arguments to levmar will be passed unexamined to every call of f.
... (arbitrary) – The model function f may have any number of postional arguments beyond its first argument p; any additional positional arguments to levmar will be passed unexamined to every call of f.
errs (array_like) – If provided, must be conformable with data, representing the standard deviation of each data point, default 1.
pfix (int or sequence of int) – An index or indices into p which are to remain fixed at their values in p0.
pmin (array_like) – Minimum and maximum bounds for p. If provided, must be conformable with p. The function f will not be called with p values outside these bounds.
pmax (array_like) – Minimum and maximum bounds for p. If provided, must be conformable with p. The function f will not be called with p values outside these bounds.
prel (array_like) –
Relative and absolute step sizes for computing finite difference partial derivatives of f. These must be conformable with p. The step size is
maximum(prel*absolute(p), pabs)
. The default values areprel=1.e-6
andpabs=1.e-9
.If f is a generator, these are ignored.
pabs (array_like) –
Relative and absolute step sizes for computing finite difference partial derivatives of f. These must be conformable with p. The step size is
maximum(prel*absolute(p), pabs)
. The default values areprel=1.e-6
andpabs=1.e-9
.If f is a generator, these are ignored.
cfg (dict) – Configuration options for levmar, overriding the default options in
levmar.config
.quiet (bool) – If levmar exceeds
levmar.config.itmax
iterations, indicating a failure to converge, it prints a warning message unless this keyword is present and True.k1=k1 (arbitrary) – Any other keyword arguments are passed unexamined to every call of f.
k2=k2 (arbitrary) – Any other keyword arguments are passed unexamined to every call of f.
... – Any other keyword arguments are passed unexamined to every call of f.
- Returns
model – The best fit model for the data. This callable object will invoke f with the best fit parameters, as well as permitting you to inspect the parameter values themselves, their covariances, and other information about the fit.
- Return type
Notes
You should scale the p values so that their values will not be very large or small. This prevents problems with ill-conditioned partial derivative matrices.
levmar.config
function attributeOptions to control the Levenberg-Marquardt algorithm:
itmax = maximum number of gradient recalculations (100) tol = tolerance, stop when chi2 changes by less than to (1.e-7) lambda0 = initial value of L-M lambda parameter (0.001) lambdax = maximum permitted value for lambda (1.e12) gain = factor by which to change lambda (10.) prel = relative step size for numerical_partials (1.e-6) pabs = absolute step size for numerical_partials (1.e-9)
You may set
levmar.config
to change the default settings, or use the cfg keyword to change any subset for a single call.
- regress(data, *mdl, **kwargs)[source]¶
Least squares fit a linear model to data.
p = regress(data, m1, m2, m3, ...)
finds 1D array of coefficients p such that:
einsum('i,i...', p, [m1, m2, m3, ...]) approximates data or simply p.dot([m1, m2, m3, ...]) if data is 1D
as nearly as possible in a least squares sense. You can use regress in underdetermined systems, that is, with more coefficients than data points, in which case it returns the exact solution p with the smallest Euclidean norm in p-space. The
model=1
statistics will not be useful in this case.- Parameters
data (array_like) – The data to be fit.
m1 (array_like) – Each of the mi must be conformable with data, representing a particular component of the linear model to explain the data.
m2 (array_like) – Each of the mi must be conformable with data, representing a particular component of the linear model to explain the data.
m3 (array_like) – Each of the mi must be conformable with data, representing a particular component of the linear model to explain the data.
... (array_like) – Each of the mi must be conformable with data, representing a particular component of the linear model to explain the data.
errs (array_like, optional keyword) – If provided, must be conformable with data, representing the standard deviation of each data point, default 1.
model (bool, optional keyword) – If not provided or false, the return value is the coefficients p. If true, the return value is a ModelFit instance.
rcond (float, optional keyword) – Reciprocal condition number, default 1e-9. Often the data do no permit a definitive choice of model – entire p-subspaces may produce indistinguishably good fits to the data, which makes the least squares matrix singular. The rcond parameter determines how small a singular value you are willing to consider relative to the largest singular value. Directions with smaller singular values are ignored when inverting the matrix.
- Returns
p – If the model parameter is not provided or false, p are coefficients of the best fit model. The length and order of p corresponds to the number and order of mi arguments.
If the model keyword is true, regress returns a ModelFit object with many attributes and methods, including:
model.p --> the model coefficients (the model=0 return) model.pcov --> covariances of p model.chi2 --> chi squared per degree of freedom model.chi2pcov --> pcov if no errs supplied in foot model.ndof --> number of degrees of freedom in fit model.s --> singular values for the model matrix model.u --> u[i] is unit vector in p-space corresponding to s[i] model() --> data produced by best fit model
- Return type
ndarray or ModelFit
Notes
The best fit polynomial of degree three has coefficients:
p = regress(y, 1., x, x**2, x**3)
To find the best fit to a set of data points
(x,y)
by any function of the form(a + b*x + c*exp(-x**2)*cos(x))
:p = regress(y, 1., x, exp(-x**2)*cos(x))