optimize.minpack

Module Contents

Functions

_check_func(checker,argname,thefunc,x0,args,numinputs,output_shape=None)
fsolve(func,x0,args=tuple,fprime=None,full_output=0,col_deriv=0,xtol=1.49012e-08,maxfev=0,band=None,epsfcn=None,factor=100,diag=None) Find the roots of a function.
_root_hybr(func,x0,args=tuple,jac=None,col_deriv=0,xtol=1.49012e-08,maxfev=0,band=None,eps=None,factor=100,diag=None,**unknown_options) Find the roots of a multivariate function using MINPACK’s hybrd and
leastsq(func,x0,args=tuple,Dfun=None,full_output=0,col_deriv=0,ftol=1.49012e-08,xtol=1.49012e-08,gtol=0.0,maxfev=0,epsfcn=None,factor=100,diag=None) Minimize the sum of squares of a set of equations.
_wrap_func(func,xdata,ydata,transform)
_wrap_jac(jac,xdata,transform)
_initialize_feasible(lb,ub)
curve_fit(f,xdata,ydata,p0=None,sigma=None,absolute_sigma=False,check_finite=True,bounds=tuple,method=None,jac=None,**kwargs) Use non-linear least squares to fit a function, f, to data.
check_gradient(fcn,Dfcn,x0,args=tuple,col_deriv=0) Perform a simple check on the gradient for correctness.
_del2(p0,p1,d)
_relerr(actual,desired)
_fixed_point_helper(func,x0,args,xtol,maxiter,use_accel)
fixed_point(func,x0,args=tuple,xtol=1e-08,maxiter=500,method=”del2”) Find a fixed point of the function.
_check_func(checker, argname, thefunc, x0, args, numinputs, output_shape=None)
fsolve(func, x0, args=tuple, fprime=None, full_output=0, col_deriv=0, xtol=1.49012e-08, maxfev=0, band=None, epsfcn=None, factor=100, diag=None)

Find the roots of a function.

Return the roots of the (non-linear) equations defined by func(x) = 0 given a starting estimate.

func : callable f(x, *args)
A function that takes at least one (possibly vector) argument.
x0 : ndarray
The starting estimate for the roots of func(x) = 0.
args : tuple, optional
Any extra arguments to func.
fprime : callable f(x, *args), optional
A function to compute the Jacobian of func with derivatives across the rows. By default, the Jacobian will be estimated.
full_output : bool, optional
If True, return optional outputs.
col_deriv : bool, optional
Specify whether the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation).
xtol : float, optional
The calculation will terminate if the relative error between two consecutive iterates is at most xtol.
maxfev : int, optional
The maximum number of calls to the function. If zero, then 100*(N+1) is the maximum where N is the number of elements in x0.
band : tuple, optional
If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for fprime=None).
epsfcn : float, optional
A suitable step length for the forward-difference approximation of the Jacobian (for fprime=None). If epsfcn is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision.
factor : float, optional
A parameter determining the initial step bound (factor * || diag * x||). Should be in the interval (0.1, 100).
diag : sequence, optional
N positive entries that serve as a scale factors for the variables.
x : ndarray
The solution (or the result of the last iteration for an unsuccessful call).
infodict : dict

A dictionary of optional outputs with the keys:

nfev
number of function calls
njev
number of Jacobian calls
fvec
function evaluated at the output
fjac
the orthogonal matrix, q, produced by the QR factorization of the final approximate Jacobian matrix, stored column wise
r
upper triangular matrix produced by QR factorization of the same matrix
qtf
the vector (transpose(q) * fvec)
ier : int
An integer flag. Set to 1 if a solution was found, otherwise refer to mesg for more information.
mesg : str
If no solution is found, mesg details the cause of failure.

root : Interface to root finding algorithms for multivariate functions. See the ‘hybr’ method in particular.

fsolve is a wrapper around MINPACK’s hybrd and hybrj algorithms.

_root_hybr(func, x0, args=tuple, jac=None, col_deriv=0, xtol=1.49012e-08, maxfev=0, band=None, eps=None, factor=100, diag=None, **unknown_options)

Find the roots of a multivariate function using MINPACK’s hybrd and hybrj routines (modified Powell method).

col_deriv : bool
Specify whether the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation).
xtol : float
The calculation will terminate if the relative error between two consecutive iterates is at most xtol.
maxfev : int
The maximum number of calls to the function. If zero, then 100*(N+1) is the maximum where N is the number of elements in x0.
band : tuple
If set to a two-sequence containing the number of sub- and super-diagonals within the band of the Jacobi matrix, the Jacobi matrix is considered banded (only for fprime=None).
eps : float
A suitable step length for the forward-difference approximation of the Jacobian (for fprime=None). If eps is less than the machine precision, it is assumed that the relative errors in the functions are of the order of the machine precision.
factor : float
A parameter determining the initial step bound (factor * || diag * x||). Should be in the interval (0.1, 100).
diag : sequence
N positive entries that serve as a scale factors for the variables.
leastsq(func, x0, args=tuple, Dfun=None, full_output=0, col_deriv=0, ftol=1.49012e-08, xtol=1.49012e-08, gtol=0.0, maxfev=0, epsfcn=None, factor=100, diag=None)

Minimize the sum of squares of a set of equations.

x = arg min(sum(func(y)**2,axis=0))
         y
func : callable
should take at least one (possibly length N vector) argument and returns M floating point numbers. It must not return NaNs or fitting might fail.
x0 : ndarray
The starting estimate for the minimization.
args : tuple, optional
Any extra arguments to func are placed in this tuple.
Dfun : callable, optional
A function or method to compute the Jacobian of func with derivatives across the rows. If this is None, the Jacobian will be estimated.
full_output : bool, optional
non-zero to return all optional outputs.
col_deriv : bool, optional
non-zero to specify that the Jacobian function computes derivatives down the columns (faster, because there is no transpose operation).
ftol : float, optional
Relative error desired in the sum of squares.
xtol : float, optional
Relative error desired in the approximate solution.
gtol : float, optional
Orthogonality desired between the function vector and the columns of the Jacobian.
maxfev : int, optional
The maximum number of calls to the function. If Dfun is provided then the default maxfev is 100*(N+1) where N is the number of elements in x0, otherwise the default maxfev is 200*(N+1).
epsfcn : float, optional
A variable used in determining a suitable step length for the forward- difference approximation of the Jacobian (for Dfun=None). Normally the actual step length will be sqrt(epsfcn)*x If epsfcn is less than the machine precision, it is assumed that the relative errors are of the order of the machine precision.
factor : float, optional
A parameter determining the initial step bound (factor * || diag * x||). Should be in interval (0.1, 100).
diag : sequence, optional
N positive entries that serve as a scale factors for the variables.
x : ndarray
The solution (or the result of the last iteration for an unsuccessful call).
cov_x : ndarray
Uses the fjac and ipvt optional outputs to construct an estimate of the jacobian around the solution. None if a singular matrix encountered (indicates very flat curvature in some direction). This matrix must be multiplied by the residual variance to get the covariance of the parameter estimates – see curve_fit.
infodict : dict

a dictionary of optional outputs with the key s:

nfev
The number of function calls
fvec
The function evaluated at the output
fjac
A permutation of the R matrix of a QR factorization of the final approximate Jacobian matrix, stored column wise. Together with ipvt, the covariance of the estimate can be approximated.
ipvt
An integer array of length N which defines a permutation matrix, p, such that fjac*p = q*r, where r is upper triangular with diagonal elements of nonincreasing magnitude. Column j of p is column ipvt(j) of the identity matrix.
qtf
The vector (transpose(q) * fvec).
mesg : str
A string message giving information about the cause of failure.
ier : int
An integer flag. If it is equal to 1, 2, 3 or 4, the solution was found. Otherwise, the solution was not found. In either case, the optional output variable ‘mesg’ gives more information.

“leastsq” is a wrapper around MINPACK’s lmdif and lmder algorithms.

cov_x is a Jacobian approximation to the Hessian of the least squares objective function. This approximation assumes that the objective function is based on the difference between some observed target data (ydata) and a (non-linear) function of the parameters f(xdata, params)

func(params) = ydata - f(xdata, params)

so that the objective function is

  min   sum((ydata - f(xdata, params))**2, axis=0)
params
_wrap_func(func, xdata, ydata, transform)
_wrap_jac(jac, xdata, transform)
_initialize_feasible(lb, ub)
curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=tuple, method=None, jac=None, **kwargs)

Use non-linear least squares to fit a function, f, to data.

Assumes ydata = f(xdata, *params) + eps

f : callable
The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.
xdata : An M-length sequence or an (k,M)-shaped array for functions with k predictors
The independent variable where the data is measured.
ydata : M-length sequence
The dependent data — nominally f(xdata, …)
p0 : None, scalar, or N-length sequence, optional
Initial guess for the parameters. If None, then the initial values will all be 1 (if the number of parameters for the function can be determined using introspection, otherwise a ValueError is raised).
sigma : None or M-length sequence or MxM array, optional

Determines the uncertainty in ydata. If we define residuals as r = ydata - f(xdata, *popt), then the interpretation of sigma depends on its number of dimensions:

  • A 1-d sigma should contain values of standard deviations of errors in ydata. In this case, the optimized function is chisq = sum((r / sigma) ** 2).

  • A 2-d sigma should contain the covariance matrix of errors in ydata. In this case, the optimized function is chisq = r.T @ inv(sigma) @ r.

    New in version 0.19.

None (default) is equivalent of 1-d sigma filled with ones.

absolute_sigma : bool, optional

If True, sigma is used in an absolute sense and the estimated parameter covariance pcov reflects these absolute values.

If False, only the relative magnitudes of the sigma values matter. The returned parameter covariance matrix pcov is based on scaling sigma by a constant factor. This constant is set by demanding that the reduced chisq for the optimal parameters popt when using the scaled sigma equals unity. In other words, sigma is scaled to match the sample variance of the residuals after the fit. Mathematically, pcov(absolute_sigma=False) = pcov(absolute_sigma=True) * chisq(popt)/(M-N)

check_finite : bool, optional
If True, check that the input arrays do not contain nans of infs, and raise a ValueError if they do. Setting this parameter to False may silently produce nonsensical results if the input arrays do contain nans. Default is True.
bounds : 2-tuple of array_like, optional

Lower and upper bounds on independent variables. Defaults to no bounds. Each element of the tuple must be either an array with the length equal to the number of parameters, or a scalar (in which case the bound is taken to be the same for all parameters.) Use np.inf with an appropriate sign to disable bounds on all or some parameters.

New in version 0.17.

method : {‘lm’, ‘trf’, ‘dogbox’}, optional

Method to use for optimization. See least_squares for more details. Default is ‘lm’ for unconstrained problems and ‘trf’ if bounds are provided. The method ‘lm’ won’t work when the number of observations is less than the number of variables, use ‘trf’ or ‘dogbox’ in this case.

New in version 0.17.

jac : callable, string or None, optional

Function with signature jac(x, ...) which computes the Jacobian matrix of the model function with respect to parameters as a dense array_like structure. It will be scaled according to provided sigma. If None (default), the Jacobian will be estimated numerically. String keywords for ‘trf’ and ‘dogbox’ methods can be used to select a finite difference scheme, see least_squares.

New in version 0.18.

kwargs
Keyword arguments passed to leastsq for method='lm' or least_squares otherwise.
popt : array
Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized
pcov : 2d array

The estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters use perr = np.sqrt(np.diag(pcov)).

How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.

If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.

ValueError
if either ydata or xdata contain NaNs, or if incompatible options are used.
RuntimeError
if the least-squares minimization fails.
OptimizeWarning
if covariance of the parameters can not be estimated.

least_squares : Minimize the sum of squares of nonlinear functions. scipy.stats.linregress : Calculate a linear least squares regression for

two sets of measurements.

With method='lm', the algorithm uses the Levenberg-Marquardt algorithm through leastsq. Note that this algorithm can only deal with unconstrained problems.

Box constraints can be handled by methods ‘trf’ and ‘dogbox’. Refer to the docstring of least_squares for more information.

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from scipy.optimize import curve_fit
>>> def func(x, a, b, c):
...     return a * np.exp(-b * x) + c

Define the data to be fit with some noise:

>>> xdata = np.linspace(0, 4, 50)
>>> y = func(xdata, 2.5, 1.3, 0.5)
>>> np.random.seed(1729)
>>> y_noise = 0.2 * np.random.normal(size=xdata.size)
>>> ydata = y + y_noise
>>> plt.plot(xdata, ydata, 'b-', label='data')

Fit for the parameters a, b, c of the function func:

>>> popt, pcov = curve_fit(func, xdata, ydata)
>>> popt
array([ 2.55423706,  1.35190947,  0.47450618])
>>> plt.plot(xdata, func(xdata, *popt), 'r-',
...          label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))

Constrain the optimization to the region of 0 <= a <= 3, 0 <= b <= 1 and 0 <= c <= 0.5:

>>> popt, pcov = curve_fit(func, xdata, ydata, bounds=(0, [3., 1., 0.5]))
>>> popt
array([ 2.43708906,  1.        ,  0.35015434])
>>> plt.plot(xdata, func(xdata, *popt), 'g--',
...          label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
>>> plt.xlabel('x')
>>> plt.ylabel('y')
>>> plt.legend()
>>> plt.show()
check_gradient(fcn, Dfcn, x0, args=tuple, col_deriv=0)

Perform a simple check on the gradient for correctness.

_del2(p0, p1, d)
_relerr(actual, desired)
_fixed_point_helper(func, x0, args, xtol, maxiter, use_accel)
fixed_point(func, x0, args=tuple, xtol=1e-08, maxiter=500, method="del2")

Find a fixed point of the function.

Given a function of one or more variables and a starting point, find a fixed-point of the function: i.e. where func(x0) == x0.

func : function
Function to evaluate.
x0 : array_like
Fixed point of function.
args : tuple, optional
Extra arguments to func.
xtol : float, optional
Convergence tolerance, defaults to 1e-08.
maxiter : int, optional
Maximum number of iterations, defaults to 500.
method : {“del2”, “iteration”}, optional
Method of finding the fixed-point, defaults to “del2” which uses Steffensen’s Method with Aitken’s Del^2 convergence acceleration [1]. The “iteration” method simply iterates the function until convergence is detected, without attempting to accelerate the convergence.
[1]Burden, Faires, “Numerical Analysis”, 5th edition, pg. 80
>>> from scipy import optimize
>>> def func(x, c1, c2):
...    return np.sqrt(c1/(x+c2))
>>> c1 = np.array([10,12.])
>>> c2 = np.array([3, 5.])
>>> optimize.fixed_point(func, [1.2, 1.3], args=(c1,c2))
array([ 1.4920333 ,  1.37228132])