Next: Least-square fitting (minimize.leastsq) Up: Newton-Conjugate-Gradient (optimize.fmin_ncg) Previous: Full Hessian example:

Hessian product example:

For larger minimization problems, storing the entire Hessian matrix can consume considerable time and memory. The Newton-CG algorithm only needs the product of the Hessian times an arbitrary vector. As a result, the user can supply code to compute this product rather than the full Hessian by setting the fhess_p keyword to the desired function. The fhess_p function should take the minimization vector as the first argument and the arbitrary vector as the second argument. Any extra arguments passed to the function to be minimized will also be passed to this function. If possible, using Newton-CG with the hessian product option is probably the fastest way to minimize the function.

In this case, the product of the Rosenbrock Hessian with an arbitrary vector is not difficult to compute. If \( \mathbf{p} \) is the arbitrary vector, then \( \mathbf{H}\left( \mathbf{x}\right) \mathbf{p} \) has elements:

\begin{displaymath}
\mathbf{H}\left( \mathbf{x}\right) \mathbf{p}=\left[ \begin{...
...
\vdots \\
-400x_{N-2}p_{N-2}+200p_{N-1}
\end{array}\right] .\end{displaymath}

Code which makes use of the fhess_p keyword to minimize the Rosenbrock function using fmin_ncg follows:


>>> from scipy.optimize import fmin_ncg
>>> def rosen_hess_p(x,p):
        x = asarray(x)
        Hp = zeros(len(x),x.typecode())
        Hp[0] = (1200*x[0]**2 - 400*x[1] + 2)*p[0] - 400*x[0]*p[1]
        Hp[1:-1] = -400*x[:-2]*p[:-2]+(202+1200*x[1:-1]**2-400*x[2:])*p[1:-1] \
                   -400*x[1:-1]*p[2:]
        Hp[-1] = -400*x[-2]*p[-2] + 200*p[-1]
        return Hp

>>> x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
>>> xopt = fmin_ncg(rosen, x0, rosen_der, fhess_p=rosen_hess_p)
Optimization terminated successfully.
         Current function value: 0.000000
         Iterations: 20
         Function evaluations: 42
         Gradient evaluations: 20
         Hessian evaluations: 44
>>> print xopt
[ 1.      1.      1.      0.9999  0.9999]



2001-07-27