scipy
Routines¶
Fitter¶
lsqfit
uses routines from the open-source scipy
Python module
provided it is installed. These routines are used in place of GSL routines
if the latter are not installed. There is one fitter available for use by
lsqfit.nonlinear_fit
.
-
class
lsqfit.
scipy_least_squares
(x0, n, f, tol=(1e-08, 1e-08, 1e-08), maxit=1000, **extra_args)¶ scipy
fitter for nonlinear least-squares multidimensional fits.scipy_leastq
is a function-class whose constructor does a least-squares fit by minimizingsum_i f_i(x)**2
as a function of vectorx
.scipy_leastsq
is a wrapper for thescipy.optimize.leastsq
.Parameters: - x0 (array of floats) – Starting point for minimization.
- n (positive int) – Length of vector returned by the fit function
f(x)
. - f (array-valued function) –
sum_i f_i(x)**2
is minimized by varying parametersx
. The parameters are a 1-dnumpy
array of either numbers orgvar.GVar
s. - tol (float or tuple) –
Assigning
tol=(xtol, gtol, ftol)
causes the fit to stop searching for a minimum when any ofxtol >=
relative change in parameters between iterationsgtol >=
relative size of gradient ofchi**2
ftol >=
relative change inchi**2
between iterationsis statisfied. See the
scipy.optimize.least_squares
documentation detailed definitions of the stopping conditions. Typically one setsxtol=1/10**d
whered
is the number of digits of precision desired in the result, whilegtol<<1
andftol<<1
. Settingtol=eps
whereeps
is a number is equivalent to settingtol=(eps,1e-10,1e-10)
. Settingtol=(eps1,eps2)
is equivlent to settingtol=(eps1,eps2,1e-10)
. Default istol=1e-5
. - method (str or None) –
Minimization algorithm. Options include:
'trf'
- Trusted Region Reflective algorithm (default). Best choice with bounded parameters.
'dogbox'
- dogleg algorithm adapted for bounded parameters.
'lm'
- Levenberg-Marquardt algorithm as implemented in MINPACK. Best for smaller problems. Does not work with bounded parameters (bounds are ignored).
Setting
method=None
implies the default'trf'
. - maxit (int) – Maximum number of function evaluations in search for minimum; default is 1000.
Other arguments include:
x_jac
,loss
,tr_solver
,f_scale
,tr_options
,bounds
. See the documentation forscipy.optimize.least_squares
for information about these and other options.lsqfit.scipy_least_squares
objects have the following attributes.-
x
¶ array
Location of the most recently computed (best) fit point.
-
cov
¶ array
Covariance matrix at the minimum point.
-
description
¶ str
Short description of internal fitter settings.
-
f
¶ array
Fit function value
f(x)
at the minimum in the most recent fit.
-
J
¶ array
Gradient
J_ij = df_i/dx[j]
for most recent fit.
-
nit
¶ int
Number of function evaluations used in last fit to find the minimum.
-
stopping_criterion
¶ int
Criterion used to stop fit:
- didn’t converge
xtol >=
relative change in parameters between iterationsgtol >=
relative size of gradient ofchi**2
ftol >=
relative change inchi**2
between iterations
-
error
¶ str or None
None
if fit successful; an error message otherwise.
-
results
¶ dict
Results returned by
scipy.optimize.least_squares
.
Minimizer¶
The lsqfit.empbayes_fit()
uses a minimizer from the scipy
module to minimize logGBF
.
-
class
lsqfit.
scipy_multiminex
(x0, f, tol=1e-4, maxit=1000, step=1, alg='nmsimplex2', analyzer=None)¶ scipy
minimizer for multidimensional functions.scipy_multiminex
is a function-class whose constructor minimizes a multidimensional functionf(x)
by varying vectorx
. This routine does not use user-supplied information about the gradient off(x)
.scipy_multiminex
is a wrapper for theminimize
scipy
function. It gives access to only part of that function.Parameters: - x0 (array of floats) – Starting point for minimization search.
- f – Function
f(x)
to be minimized by varying vectorx
. - tol (float) – Minimization stops when
x
has converged to with tolerancetol
; default is1e-4
. - maxit (positive int) – Maximum number of iterations in search for minimum; default is 1000.
- analyzer (function) – Optional function of the current
x
. This can be used to inspect intermediate steps in the minimization, if needed.
lsqfit.scipy_multiminex
objects have the following attributes.-
x
¶ array
Location of the minimum.
-
f
¶ float
Value of function
f(x)
at the minimum.
-
nit
¶ int
Number of iterations required to find the minimum.
-
error
¶ Noe or str
None
if fit successful; an error message otherwise.