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,

num_extrap=0, **step_options)

if step or method in in [‘complex’, ‘multicomplex’], otherwise

MaxStepGenerator(step_ratio=None, num_extrap=14, **step_options)

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.

**step_options:

options to pass on to the XXXStepGenerator used.

Returns:

hess : ndarray

array of partial second derivatives, Hessian

See also

Derivative, Hessian

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)\quad ((f(x + d_j e_j + d_k e_k) - f(x + d_j e_j))) / (d_j d_k)

(2)\quad  ((f(x + d_j e_j + d_k e_k) - f(x + d_j e_j - d_k e_k)) -  (f(x - d_j e_j + d_k e_k) - f(x - d_j e_j - d_k e_k)) / (4 d_j d_k)

(3)imag(f(x + i d_j e_j + d_k e_k) - f(x + i d_j e_j - d_k e_k)) /(2 d_j d_k)

where e_j is a vector with element j is one and the rest are zero and d_j is a scalar spacing steps_j.

__init__(f, step=None, method='central', full_output=False)[source][source]

Methods

__init__(f[, step, method, full_output])