scipy Routines — lsqfit 11.3 documentation

# `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_least_squares` is a function-class whose constructor does a least-squares fit by minimizing `sum_i f_i(x)**2` as a function of vector `x`.

  `scipy_least_squares` is a wrapper for the `scipy.optimize.least_squares`.

  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-d `numpy` array of either numbers or `gvar.GVar`s.

    - 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 iterations

      > `gtol >=` relative size of gradient of `chi**2`

      > `ftol >=` relative change in `chi**2` between iterations

      is statisfied. See the `scipy.optimize.least_squares` documentation detailed definitions of the stopping conditions. Typically one sets `xtol=1/10**d` where `d` is the number of digits of precision desired in the result, while `gtol<<1` and `ftol<<1`. Setting `tol=eps` where `eps` is a number is equivalent to setting `tol=(eps,1e-10,1e-10)`. Setting `tol=(eps1,eps2)` is equivlent to setting `tol=(eps1,eps2,1e-10)`. Default is `tol=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 for `scipy.optimize.least_squares` for information about these and other options.

  `lsqfit.scipy_least_squares` objects have the following attributes.

  `x`

    Location of the most recently computed (best) fit point.

    Type

      array

  `cov`

    Covariance matrix at the minimum point.

    Type

      array

  `description`

    Short description of internal fitter settings.

    Type

      str

  `f`

    Fit function value `f(x)` at the minimum in the most recent fit.

    Type

      array

  `J`

    Gradient `J_ij = df_i/dx[j]` for most recent fit.

    Type

      array

  `nit`

    Number of function evaluations used in last fit to find the minimum.

    Type

      int

  `stopping_criterion`

    Criterion used to stop fit:

    > 0. didn’t converge

    > 1. `xtol >=` relative change in parameters between iterations

    > 2. `gtol >=` relative size of gradient of `chi**2`

    > 3. `ftol >=` relative change in `chi**2` between iterations

    Type

      int

  `error`

    `None` if fit successful; an error message otherwise.

    Type

      str or None

  `results`

    Results returned by `scipy.optimize.least_squares`.

    Type

      dict

## 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 function `f(x)` by varying vector `x`. This routine does not use user-supplied information about the gradient of `f(x)`.

  `scipy_multiminex` is a wrapper for the `minimize` `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 vector `x`.

    - tol (float) – Minimization stops when `x` has converged to with tolerance `tol`; default is `1e-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`

    Location of the minimum.

    Type

      array

  `f`

    Value of function `f(x)` at the minimum.

    Type

      float

  `nit`

    Number of iterations required to find the minimum.

    Type

      int

  `error`

    `None` if fit successful; an error message otherwise.

    Type

      Noe or str