Quasi-Newton Methods
Newton's method for systems needs a fresh Jacobian at every step, which costs function evaluations by finite differences, or much human effort by hand. Quasi-Newton methods start from one Jacobian approximation and improve it cheaply after each step using only the function values already computed. They are the multidimensional analogue of the secant method.
The secant condition
After a step we know the change . A good Jacobian approximation should reproduce it:
In one dimension this fixes (the secant slope). In dimensions it gives only equations for entries, so we also ask that change as little as possible.
Broyden's update
The smallest change (in the Frobenius norm) satisfying the secant condition is the rank-one Broyden update:
Each step costs one new evaluation of and a linear solve; convergence is superlinear.
import numpy as np
def f(v):
x, y = v
return np.array([x**2 + y**2 - 4, np.exp(x) + y - 1])
def broyden(f, x, A, tol=1e-12, maxit=50):
fx = f(x)
evals = 1
for k in range(maxit):
s = np.linalg.solve(A, -fx)
x_new = x + s
f_new = f(x_new); evals += 1
y = f_new - fx
A = A + np.outer(y - A @ s, s) / (s @ s)
x, fx = x_new, f_new
if np.linalg.norm(fx) < tol:
break
return x, k + 1, evals
x0 = np.array([1.0, -1.5])
A0 = np.array([[2.0, -3.0], [np.e, 1.0]]) # exact Jacobian at x0, computed once
x, steps, evals = broyden(f, x0, A0)
print(f"root {np.round(x, 12)} in {steps} steps, {evals} evaluations of f, |f| = {np.linalg.norm(f(x)):.1e}")root [ 1.00416874 -1.72963729] in 6 steps, 7 evaluations of f, |f| = 2.0e-13
Compare with Newton's Method for Systems, which needed fewer steps but a new Jacobian each time.
Quasi-Newton for optimization: BFGS
For minimization the "Jacobian" is the Hessian, which is symmetric and (near a minimum) positive definite. The BFGS update keeps both properties with a rank-two correction:
with now the change of the gradient. Implementations update the inverse directly so no system needs solving. L-BFGS stores only the last pairs instead of an matrix, which makes it usable for millions of variables.
import numpy as np
from scipy.optimize import minimize, rosen, rosen_der
x0 = np.array([-1.2, 1.0])
for method in ("BFGS", "L-BFGS-B", "Newton-CG"):
kw = {"jac": rosen_der}
if method == "Newton-CG":
from scipy.optimize import rosen_hess
kw["hess"] = rosen_hess
res = minimize(rosen, x0, method=method, **kw)
print(f"{method:9s}: x = {np.round(res.x, 6)}, iterations {res.nit}, gradient evaluations {res.njev}")BFGS : x = [1. 1.], iterations 32, gradient evaluations 39
L-BFGS-B : x = [0.999999 0.999998], iterations 36, gradient evaluations 44
Newton-CG: x = [0.999983 0.999965], iterations 83, gradient evaluations 105
In software
scipy.optimize.root(f, x0, method="broyden1")(and"broyden2","anderson","krylov"for large systems).scipy.optimize.minimize(method="BFGS")and"L-BFGS-B"(with bounds) are the default choices for smooth unconstrained optimization; L-BFGS is also used to train small neural networks and in classic ML (logistic regression in scikit-learn useslbfgsby default).
Exercises
Exercise 1: Secant condition in 1D
Show that in one dimension the secant condition gives the secant method.
Answer
.
Solution
with scalars gives , the slope of the secant line, and the next step is the secant step.
Exercise 2: Rank of the update
What is the rank of the Broyden correction matrix, and why does that matter?
Answer
Rank one; the factorization of can be updated in instead of recomputed in .
Solution
It is an outer product . The Sherman-Morrison formula updates (or a QR factorization) in operations.
Exercise 3: Broyden from the identity (code)
Run Broyden's method for the lesson's system starting from instead of the exact Jacobian. Does it still converge?
Answer
Yes, to the same root, but in more steps.
Solution
import numpy as np
f = lambda v: np.array([v[0]**2 + v[1]**2 - 4, np.exp(v[0]) + v[1] - 1])
x = np.array([1.0, -1.5]); A = np.eye(2); fx = f(x)
for k in range(100):
s = np.linalg.solve(A, -fx)
xn = x + s; fn = f(xn)
A = A + np.outer(fn - fx - A @ s, s) / (s @ s)
x, fx = xn, fn
if np.linalg.norm(fx) < 1e-12:
break
print(np.round(x, 10), "steps:", k + 1)[ 1.00416874 -1.72963729] steps: 12
A poor initial matrix costs extra steps, and from a bad start the method may wander to a different root or fail, which is why codes add line searches.
Test yourself
The secant condition for a Jacobian approximation is…
- symmetric
Why is L-BFGS used for very large problems?
- It converges quadratically
- It stores only a few vector pairs instead of an matrix
- It needs no gradient
- It is exact
Broyden's method needs per step…
- new function evaluations
- one new function evaluation
- the exact Jacobian
- the Hessian