bundles / scipy latest / scipy / optimize / _optimize / approx_fprime
function
scipy.optimize._optimize:approx_fprime
Signature
def approx_fprime ( xk , f , epsilon = 1.4901161193847656e-08 , * args ) Summary
Finite difference approximation of the derivatives of a scalar or vector-valued function.
Extended Summary
If a function maps from to , its derivatives form an m-by-n matrix called the Jacobian, where an element is a partial derivative of f[i] with respect to xk[j].
Parameters
xk: array_likeThe coordinate vector at which to determine the gradient of
f.f: callableFunction of which to estimate the derivatives of. Has the signature
f(xk, *args)wherexkis the argument in the form of a 1-D array andargsis a tuple of any additional fixed parameters needed to completely specify the function. The argumentxkpassed to this function is an ndarray of shape (n,) (never a scalar even if n=1). It must return a 1-D array_like of shape (m,) or a scalar.Suppose the callable has signature
f0(x, *my_args, **my_kwargs), wheremy_argsandmy_kwargsare required positional and keyword arguments. Rather than passingf0as the callable, wrap it to accept onlyx; e.g., passfun=lambda x: f0(x, *my_args, **my_kwargs)as the callable, wheremy_args(tuple) andmy_kwargs(dict) have been gathered before invoking this function.epsilon: {float, array_like}, optionalIncrement to
xkto use for determining the function gradient. If a scalar, uses the same finite difference delta for all partial derivatives. If an array, should contain one value per element ofxk. Defaults tosqrt(np.finfo(float).eps), which is approximately 1.49e-08.\*args: args, optionalAny other arguments that are to be passed to
f.
Returns
jac: ndarrayThe partial derivatives of
ftoxk.
Notes
The function gradient is determined by the forward finite difference formula
f(xk[i] + epsilon[i]) - f(xk[i]) f'[i] = --------------------------------- epsilon[i]
Examples
import numpy as np from scipy import optimize def func(x, c0, c1): "Coordinate vector `x` should be an array of size two." return c0 * x[0]**2 + c1*x[1]**2✓
x = np.ones(2) c0, c1 = (1, 200) eps = np.sqrt(np.finfo(float).eps)✓
optimize.approx_fprime(x, func, [eps, np.sqrt(200) * eps], c0, c1)
✗See also
- check_grad
Check correctness of gradient function against approx_fprime.
Aliases
-
scipy.optimize.approx_fprime