TExcelTAD
Home › Optimization › Quasi-Newton Methods and BFGS

Quasi-Newton Methods and BFGS

Newton's method converges in a handful of iterations but needs the Hessian. Quasi-Newton methods build an approximation of the Hessian (or its inverse) from the gradients they already compute, one cheap update per iteration. BFGS, named after Broyden, Fletcher, Goldfarb and Shanno, is the most successful of them and the default smooth optimizer in many libraries.

The secant equation

After a step sk=xk+1−xk\mathbf{s}_k = \mathbf{x}_{k+1} - \mathbf{x}_k, the gradient changes by yk=∇fk+1−∇fk\mathbf{y}_k = \nabla f_{k+1} - \nabla f_k. For a quadratic, yk=∇2f sk\mathbf{y}_k = \nabla^2 f\,\mathbf{s}_k exactly. A quasi-Newton method asks its next Hessian approximation Bk+1B_{k+1} to reproduce this:

Bk+1sk=ykB_{k+1}\mathbf{s}_k = \mathbf{y}_k

This is nn equations for n(n+1)/2n(n+1)/2 unknowns, so many updates satisfy it. BFGS picks the one closest to the previous approximation (in a weighted norm) that stays symmetric and positive definite.

The BFGS update

Working with the inverse approximation Hk≈∇2f−1H_k \approx \nabla^2 f^{-1} avoids solving a system. With ρk=1/(ykTsk)\rho_k = 1/(\mathbf{y}_k^T\mathbf{s}_k):

Hk+1=(I−ρkskykT) Hk (I−ρkykskT)+ρkskskTH_{k+1} = (I - \rho_k\mathbf{s}_k\mathbf{y}_k^T)\,H_k\,(I - \rho_k\mathbf{y}_k\mathbf{s}_k^T) + \rho_k\mathbf{s}_k\mathbf{s}_k^T

The search direction is pk=−Hk∇fk\mathbf{p}_k = -H_k\nabla f_k, followed by a Wolfe line search, which guarantees ykTsk>0\mathbf{y}_k^T\mathbf{s}_k > 0 and so keeps Hk+1H_{k+1} positive definite.

python · try it yourself
import numpy as np
from scipy.optimize import line_search

f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
grad = lambda p: np.array([-2 * (1 - p[0]) - 400 * p[0] * (p[1] - p[0]**2), 200 * (p[1] - p[0]**2)])

def bfgs(f, grad, x, tol=1e-8, max_iter=200):
    n = len(x); H = np.eye(n); g = grad(x)
    for k in range(max_iter):
        if np.linalg.norm(g) < tol:
            return x, k
        p = -H @ g
        t = line_search(f, grad, x, p, gfk=g)[0] or 1e-3
        s = t * p; x_new = x + s; g_new = grad(x_new); y = g_new - g
        rho = 1.0 / (y @ s)
        I = np.eye(n)
        H = (I - rho * np.outer(s, y)) @ H @ (I - rho * np.outer(y, s)) + rho * np.outer(s, s)
        x, g = x_new, g_new
    return x, max_iter

x, iters = bfgs(f, grad, np.array([-1.2, 1.0]))
print("x =", np.round(x, 8), " iterations:", iters)
x = [1. 1.]  iterations: 35

No Hessian was ever computed, yet the iteration count is close to Newton's.

The approximation learns the Hessian

On a quadratic with exact line searches, BFGS terminates in at most nn steps and HnH_n equals the true inverse Hessian. Even with inexact searches, HkH_k approaches the inverse Hessian along the directions explored:

python · try it yourself
import numpy as np

Q = np.array([[4.0, 1.0, 0.0], [1.0, 3.0, 1.0], [0.0, 1.0, 2.0]]); b = np.array([1.0, 2.0, 3.0])
grad = lambda x: Q @ x - b
x = np.zeros(3); H = np.eye(3); g = grad(x)
for k in range(3):
    p = -H @ g
    t = -(g @ p) / (p @ Q @ p)                   # exact line search on a quadratic
    s = t * p; g_new = grad(x + s); y = g_new - g
    rho = 1 / (y @ s); I = np.eye(3)
    H = (I - rho * np.outer(s, y)) @ H @ (I - rho * np.outer(y, s)) + rho * np.outer(s, s)
    x, g = x + s, g_new
    fmt = lambda v: f"{v:.1e}" if v > 1e-12 else "below 1e-12"
    print(f"after step {k + 1}: |grad| = {fmt(np.linalg.norm(g))}, |H - Q^-1| = {fmt(np.linalg.norm(H - np.linalg.inv(Q)))}")
after step 1: |grad| = 1.3e+00, |H - Q^-1| = 8.2e-01
after step 2: |grad| = 5.9e-01, |H - Q^-1| = 6.4e-01
after step 3: |grad| = below 1e-12, |H - Q^-1| = below 1e-12

Convergence

On smooth, strongly convex functions BFGS with Wolfe line searches converges superlinearly: faster than any linear rate, though not quite quadratically. It also has a self-correcting property: a bad approximation is repaired within a few iterations. On non-convex functions it usually works well in practice, although global convergence theory is weaker.

Cost and limits

Each iteration costs one gradient plus O(n2)O(n^2) arithmetic for the update and matrix-vector product, and HkH_k needs n2n^2 numbers of memory. That is fine up to nn in the low thousands; for larger problems the dense matrix becomes the bottleneck, which L-BFGS removes.

In software

  • scipy.optimize.minimize(method="BFGS") is SciPy's default for unconstrained problems with gradients; it returns res.hess_inv, the final HkH_k, often used as a rough covariance estimate in curve fitting.
  • R's optim(method = "BFGS") and MATLAB's fminunc (quasi-Newton option) implement the same update.

Exercises

Exercise 1: Secant equation in 1D

In one dimension, what does the secant equation say about Bk+1B_{k+1}?

Answer

Bk+1=f′(xk+1)−f′(xk)xk+1−xkB_{k+1} = \frac{f'(x_{k+1}) - f'(x_k)}{x_{k+1} - x_k}, a finite-difference second derivative.

Solution

Bk+1sk=ykB_{k+1}s_k = y_k with scalars has the unique solution yk/sky_k/s_k, the slope of f′f' between the two points. That makes 1D quasi-Newton the secant method applied to f′f'.

Exercise 2: Positive curvature

Why must ykTsk>0\mathbf{y}_k^T\mathbf{s}_k > 0 for Hk+1H_{k+1} to be positive definite?

Answer

Because Hk+1yk=skH_{k+1}\mathbf{y}_k = \mathbf{s}_k, so ykTHk+1yk=ykTsk\mathbf{y}_k^TH_{k+1}\mathbf{y}_k = \mathbf{y}_k^T\mathbf{s}_k, which must be positive.

Solution

The update satisfies the inverse secant equation Hk+1yk=skH_{k+1}\mathbf{y}_k = \mathbf{s}_k. A positive definite matrix gives vTHv>0\mathbf{v}^TH\mathbf{v} > 0 for every v≠0\mathbf{v} \ne 0; with v=yk\mathbf{v} = \mathbf{y}_k that is ykTsk>0\mathbf{y}_k^T\mathbf{s}_k > 0.

Exercise 3: Compare with SciPy (code)

Run scipy.optimize.minimize(method="BFGS") on Rosenbrock from (−1.2,1)(-1.2, 1) with the gradient and compare its iteration count with ours.

Answer

Both converge to (1,1)(1, 1) in a few dozen iterations.

Solution
python · try it yourself
import numpy as np
from scipy.optimize import minimize
f = lambda p: (1 - p[0])**2 + 100 * (p[1] - p[0]**2)**2
grad = lambda p: np.array([-2 * (1 - p[0]) - 400 * p[0] * (p[1] - p[0]**2), 200 * (p[1] - p[0]**2)])
r = minimize(f, [-1.2, 1.0], jac=grad, method="BFGS", options={"gtol": 1e-8})
print(np.round(r.x, 6), r.nit, "iterations")
[1. 1.] 34 iterations

Small differences come from the initial H0H_0 scaling and line-search details.

Test yourself

The secant equation for quasi-Newton methods is…

  1. Bk+1yk=skB_{k+1}\mathbf{y}_k = \mathbf{s}_k
  2. Bk+1sk=ykB_{k+1}\mathbf{s}_k = \mathbf{y}_k
  3. sk=−∇fk\mathbf{s}_k = -\nabla f_k
  4. Bk+1=∇2fk+1B_{k+1} = \nabla^2 f_{k+1}

BFGS typically converges…

  1. sublinearly
  2. superlinearly
  3. only on quadratics
  4. in one step

What limits full BFGS on problems with millions of variables?

  1. It needs the exact Hessian
  2. It stores an n×nn \times n matrix
  3. It cannot use gradients
  4. It requires integer variables