GSL Routines¶
Fitters¶
lsqfit
uses routines from the GSL C-library provided it is
installed and lsqfit
is installed from source (not from a Python wheel);
GSL is the open-source Gnu Scientific Library. To install lsqfit
from source use
python -m pip install lsqfit --no-binary lsqfit --no-cache-dir
making sure that command-line command gsl-config
is on your PATH
.
There are two GSL fitters that are available for use by lsqfit.nonlinear_fit
:
- class lsqfit.gsl_multifit(n, f, tol=(1e-5, 0.0, 0.0), maxit=1000, alg='lm', solver='qr', scaler='more', factor_up=3.0, factor_down=2.0, avmax=0.75)¶
GSL fitter for nonlinear least-squares multidimensional fits.
gsl_multifit
is a function-class whose constructor does a least-squares fit by minimizingsum_i f_i(x)**2
as a function of vectorx
.gsl_multifit
is a wrapper for themultifit
GSL routine.- 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 parameters
x
. The parameters are a 1-dnumpy
array of either numbers orgvar.GVar
s.
- f (array-valued function):
- tol (float or tuple): Assigning
tol=(xtol, gtol, ftol)
causes the fit to stop searching for a minimum when any of
xtol >=
relative change in parameters between iterationsgtol >=
relative size of gradient ofchi**2
ftol >=
relative change inchi**2
between iterationsis statisfied. See the GSL documentation for detailed definitions of the stopping conditions. Typically one sets
xtol=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
. (Note:ftol
option is disabled in some versions of the GSL library.)
- tol (float or tuple): Assigning
- maxit (int): Maximum number of iterations in search for minimum;
default is 1000.
- alg (str): GSL algorithm to use for minimization. The following
options are supported (see GSL documentation for more information):
'lm'
Levenberg-Marquardt algorithm (default).
'lmaccel'
Levenberg-Marquardt algorithm with geodesic acceleration. Can be faster than
'lm'
but less stable. Stability is controlled by damping parameteravmax
; setting it to zero turns acceleration off.'subspace2D'
2D generalization of dogleg algorithm. This can be substantially faster than the two
'lm'
algorithms.'dogleg'
dogleg algorithm.
'ddogleg'
double dogleg algorithm.
- scaler (str): Scaling method used in minimization. The following
options are supported (see GSL documentation for more information):
'more'
More rescaling, which makes the problem scale invariant. Default.
'levenberg'
Levenberg rescaling, which is not scale invariant but may be more efficient in certain problems.
'marquardt'
Marquardt rescaling. Probably not as good as the other two options.
- solver (str): Method use to solve the linear equations for the
solution from a given step. The following options are supported (see GSL documentation for more information):
'qr'
QR decomposition of the Jacobian. Default.
'cholesky'
Cholesky decomposition of the Jacobian. Can be substantially faster than
'qr'
but not as reliable for singular Jacobians.'svd'
SVD decomposition. The most robust for singular situations, but also the slowest.
- factor_up (float): Factor by which search region is increased
when a search step is accepted. Values that are too large destablize the search; values that are too small slow down the search. Default is
factor_up=3
.
- factor_down (float): Factor by which search region is decreased
when a search step is rejected. Values that are too large destablize the search; values that are too small slow down the search. Default is
factor_up=2
.
- avmax (float): Damping parameter for geodesic acceleration. It
is the maximum allowed value for the acceleration divided by the velocity. Smaller values imply less acceleration. Default is
avmax=0.75
.
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.
- class lsqfit.gsl_v1_multifit(x0, n, f, tol=1e-5, maxit=1000, alg='lmsder', analyzer=None)¶
Fitter for nonlinear least-squares multidimensional fits. (GSL v1.)
gsl_v1_ multifit
is a function-class whose constructor does a least-squares fit by minimizingsum_i f_i(x)**2
as a function of vectorx
.gsl_v1_multifit
is a wrapper for the (older, v1)multifit
GSL routine (seenlin.h
). This package was used in earlier versions oflsqfit
(<9.0) and is typically not as effective asgsl_multifit
. It is included (for now) for legacy code.- 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 parameters
x
. The parameters are a 1-dnumpy
array of either numbers orgvar.GVar
s.
- f (array-valued function):
- tol (float or tuple): Assigning
tol=(xtol, gtol, ftol)
causes the fit to stop searching for a minimum when any of
xtol >=
relative change in parameters between iterationsgtol >=
relative size of gradient ofchi**2
ftol >=
relative change inchi**2
between iterationsis statisfied. See the GSL documentation for detailed definitions of the stopping conditions. Typically one sets
xtol=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
. (Note: theftol
option is disabled in some versions of the GSL library.)
- tol (float or tuple): Assigning
- maxit (int): Maximum number of iterations in search for minimum;
default is 1000.
- alg (str): GSL algorithm to use for minimization. Two options are
currently available:
"lmsder"
, the scaled LMDER algorithm (default); and"lmder"
, the unscaled LMDER algorithm. With version 2 of the GSL library, another option is"lmniel"
, which can be useful when there is much more data than parameters.
- analyzer (callable): Optional function of
x,
[...f_i(x)...],
[[..df_ij(x)..]]
which is called after each iteration. This can be used to inspect intermediate steps in the minimization, if needed.
- analyzer (callable): Optional function of
Attributes:
- x (array):
Location of the most recently computed (best) fit point.
- cov (array):
Covariance matrix at the minimum point.
- f (callable):
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.
Minimizer¶
The lsqfit.empbayes_fit()
uses a minimizer from the GSL library
to minimize logGBF
.
- class lsqfit.gsl_multiminex(x0, f, tol=1e-4, maxit=1000, step=1, alg='nmsimplex2', analyzer=None)¶
Minimizer for multidimensional functions.
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)
.multiminex
is a wrapper for themultimin
GSL routine.- Parameters:
x0 (array): Starting point for minimization search.
f (callable): Function
f(x)
to be minimized by varying vectorx
.- tol (float): Minimization stops when
x
has converged to with tolerance
tol
; default is1e-4
.
- tol (float): Minimization stops when
- maxit (int): Maximum number of iterations in search for minimum;
default is 1000.
- step (float): Initial step size to use in varying components of
x
; default is 1.
- step (float): Initial step size to use in varying components of
- alg (str): GSL algorithm to use for minimization. Three options are
currently available:
"nmsimplex"
, Nelder Mead Simplex algorithm;"nmsimplex2"
, an improved version of"nmsimplex"
(default); and"nmsimplex2rand"
, a version of"nmsimplex2"
with random shifts in the start position.
- analyzer (callable): Optional function of
x
, which is called after each iteration. This can be used to inspect intermediate steps in the minimization, if needed.
- analyzer (callable): Optional function of
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 (None or str):
None
if minimization successful; an error message otherwise.