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 , the gradient changes by . For a quadratic, exactly. A quasi-Newton method asks its next Hessian approximation to reproduce this:
This is equations for 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 avoids solving a system. With :
The search direction is , followed by a Wolfe line search, which guarantees and so keeps positive definite.
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 steps and equals the true inverse Hessian. Even with inexact searches, approaches the inverse Hessian along the directions explored:
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 arithmetic for the update and matrix-vector product, and needs numbers of memory. That is fine up to 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 returnsres.hess_inv, the final , often used as a rough covariance estimate in curve fitting.- R's
optim(method = "BFGS")and MATLAB'sfminunc(quasi-Newton option) implement the same update.
Exercises
Exercise 1: Secant equation in 1D
In one dimension, what does the secant equation say about ?
Answer
, a finite-difference second derivative.
Solution
with scalars has the unique solution , the slope of between the two points. That makes 1D quasi-Newton the secant method applied to .
Exercise 2: Positive curvature
Why must for to be positive definite?
Answer
Because , so , which must be positive.
Solution
The update satisfies the inverse secant equation . A positive definite matrix gives for every ; with that is .
Exercise 3: Compare with SciPy (code)
Run scipy.optimize.minimize(method="BFGS") on Rosenbrock from with the gradient and compare its iteration count with ours.
Answer
Both converge to in a few dozen iterations.
Solution
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 scaling and line-search details.
Test yourself
The secant equation for quasi-Newton methods is…
BFGS typically converges…
- sublinearly
- superlinearly
- only on quadratics
- in one step
What limits full BFGS on problems with millions of variables?
- It needs the exact Hessian
- It stores an matrix
- It cannot use gradients
- It requires integer variables