GSL Routines¶
Fitters¶
lsqfit uses routines from the GSL C-library provided it is
installed; GSL is the open-source Gnu Scientific Library. There are two
fitters that are available for use by lsqfit.nonlinear_fit.
-
class
lsqfit.gsl_multifit¶ GSL fitter for nonlinear least-squares multidimensional fits.
gsl_multifitis a function-class whose constructor does a least-squares fit by minimizingsum_i f_i(x)**2as a function of vectorx.gsl_multifitis a wrapper for themultifitGSL 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)**2is minimized by varying parametersx. The parameters are a 1-dnumpyarray of either numbers orgvar.GVars. - 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**2ftol >=relative change inchi**2between iterationsis statisfied. See the GSL documentation for detailed definitions of the stopping conditions. Typically one sets
xtol=1/10**dwheredis the number of digits of precision desired in the result, whilegtol<<1andftol<<1. Settingtol=epswhereepsis 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:ftoloption is disabled in some versions of the GSL library.) - 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.
lsqfit.gsl_multifitobjects 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**2ftol >=relative change inchi**2between iterations
-
error¶ str or None
Noneif fit successful; an error message otherwise.
-
class
lsqfit.gsl_v1_multifit¶ Fitter for nonlinear least-squares multidimensional fits. (GSL v1.)
gsl_v1_ multifitis a function-class whose constructor does a least-squares fit by minimizingsum_i f_i(x)**2as a function of vectorx.gsl_v1_multifitis a wrapper for the (older, v1)multifitGSL routine (seenlin.h). This package was used in earlier versions oflsqfit(<9.0).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)**2is minimized by varying parametersx. The parameters are a 1-dnumpyarray of either numbers orgvar.GVars. - 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**2ftol >=relative change inchi**2between iterationsis statisfied. See the GSL documentation for detailed definitions of the stopping conditions. Typically one sets
xtol=1/10**dwheredis the number of digits of precision desired in the result, whilegtol<<1andftol<<1. Settingtol=epswhereepsis 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: theftoloption is disabled in some versions of the GSL library.) - 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 – 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.
lsqfit.gsl_v1_multifitobjects have the following 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**2ftol >=relative change inchi**2between iterations
-
error¶ str or None
Noneif 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.
multiminexis 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).multiminexis a wrapper for themultiminGSL 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
xhas converged to with tolerancetol; default is1e-4. - 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. - 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 – Optional function of
x, which is called after each iteration. This can be used to inspect intermediate steps in the minimization, if needed.
lsqfit.gsl_multiminexobjects 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¶ None or str
Noneif minimization successful; an error message otherwise.