scijit.optimize.HessInv

class scijit.optimize.HessInv(*args, **kwargs)

Bases: HessInv

The inverse-Hessian estimate a minimize result carries.

Two backends produce one, and they produce different things. BFGS carries a dense (n, n) matrix. L-BFGS-B never forms a matrix: it keeps the (s, y) correction pairs and applies the operator through them, which is what makes it usable at large n. Both are reached the same way, and .todense() gives the matrix from either.

Apply the operator with op(v), op.matvec(v) or op.dot(v). The op @ v and op * v spellings and a dtype attribute are absent.

Attributes:
sk, ykndarray, shape (n_corrs, n)

The stored correction pairs. Both are (0, n) unless the method was 'L-BFGS-B'.

rhondarray, shape (n_corrs,)

1 / (yk[i] @ sk[i]) per pair.

shapetuple of int

(n, n).

n_corrsint

The number of stored correction pairs. 0 for the dense form.

matndarray, shape (n, n) or (0, 0)

The dense estimate. (0, 0) unless the method was 'BFGS'.

nint

The problem dimension.

Raises:
ValueError

If sk and yk do not have matching shape.

See also

scipy.optimize.LbfgsInvHessProduct

The operator scipy’s L-BFGS-B result carries.

Notes

scipy uses two types where this uses one: a plain ndarray on the BFGS result and scipy.optimize.LbfgsInvHessProduct on the L-BFGS-B result. A compiled function has one return type, so one class covers both here.

Only a 'BFGS' or 'L-BFGS-B' result carries a hess_inv. On every other method the field is absent, as it is in scipy, so an operator is reached only after one of those two.

A correction pair of zero curvature gives a rho entry of inf and a matrix of nan. numpy raises a RuntimeWarning on that division; this does not.

CONSTRUCTOR DEFAULTS ARE PYTHON-ONLY is the package-wide jitclass trap, but this constructor declares none, so all four arguments are always required, in Python and inside @njit alike.

Examples

>>> import numpy as np
>>> from numba import njit
>>> from scijit.optimize import minimize
>>> @njit
... def fg(x):
...     f = (x[0] - 1.0) ** 2 + (x[1] - 2.5) ** 2
...     return f, np.array([2.0 * (x[0] - 1.0), 2.0 * (x[1] - 2.5)])
>>> @njit
... def run():
...     op = minimize(fg, np.array([0.0, 0.0]), method='BFGS').hess_inv
...     return op.has_hess(), op.shape, op.todense().shape
>>> run()
(True, (2, 2), (2, 2))
__init__(sk, yk, mat, n)

Methods

__init__(sk, yk, mat, n)

dot(v)

Apply the operator, the LinearOperator.dot spelling.

ev(v)

Apply the operator.

has_hess()

Whether the method produced an estimate at all.

matvec(v)

Apply the operator, the LinearOperator.matvec spelling.

todense()

The operator as an (n, n) array.

Attributes

class_type = jitclass.HessInv#7fad152d5130<sk:array(float64, 2d, C),yk:array(float64, 2d, C),mat:array(float64, 2d, C),n:int64,n_corrs:int64,rho:array(float64, 1d, C),shape:UniTuple(int64 x 2)>