numdifftools.core.Hessian¶
-
class
numdifftools.core.
Hessian
(f, step=None, method='central', full_output=False)[source][source]¶ Calculate Hessian with finite difference approximation
Parameters: f : function
function of one array f(x, *args, **kwds)
step : float, array-like or StepGenerator object, optional
Defines the spacing used in the approximation. Default is MinStepGenerator(base_step=step, step_ratio=None) if step or method in in [‘complex’, ‘multicomplex’], otherwise MaxStepGenerator(step_ratio=None, num_extrap=14) The results are extrapolated if the StepGenerator generate more than 3 steps.
method : {‘central’, ‘complex’, ‘multicomplex’, ‘forward’, ‘backward’}
defines the method used in the approximation
full_output : bool, optional
If full_output is False, only the derivative is returned. If full_output is True, then (der, r) is returned der is the derivative, and r is a Results object.
Returns: hess : ndarray
array of partial second derivatives, Hessian
See also
Notes
Complex methods are usually the most accurate provided the function to differentiate is analytic. The complex-step methods also requires fewer steps than the other methods and can work very close to the support of a function. The complex-step derivative has truncation error O(steps**2) for n=1 and O(steps**4) for n larger, so truncation error can be eliminated by choosing steps to be very small. Especially the first order complex-step derivative avoids the problem of round-off error with small steps because there is no subtraction. However, this method fails if f(x) does not support complex numbers or involves non-analytic functions such as e.g.: abs, max, min. Central difference methods are almost as accurate and has no restriction on type of function. For this reason the ‘central’ method is the default method, but sometimes one can only allow evaluation in forward or backward direction.
For all methods one should be careful in decreasing the step size too much due to round-off errors.
Computes the Hessian according to method as: ‘forward’ (1), ‘central’ (2) and ‘complex’ (3):
(1)
(2)
(3)
where
is a vector with element
is one and the rest are zero and
is a scalar spacing
.
Methods
__init__
(f[, step, method, full_output])