Newton's Method
Gradient descent uses a linear model of and needs a step size to tell it how far to trust the model. Newton's method uses a quadratic model, whose minimum tells it both the direction and the distance. Near a minimum it converges quadratically, and it is invariant to how you scale the variables.
The Newton step
Approximate near by its second-order Taylor expansion:
If the Hessian is positive definite, the model's minimum is where its gradient vanishes:
In code you solve the linear system; you never invert the Hessian.
import numpy as np
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)])
hess = lambda p: np.array([[2 - 400 * p[1] + 1200 * p[0]**2, -400 * p[0]], [-400 * p[0], 200.0]])
x = np.array([-1.2, 1.0])
for k in range(1, 30):
g = grad(x)
p = np.linalg.solve(hess(x), -g)
t = 1.0
while f(x + t * p) > f(x) + 1e-4 * t * (g @ p): # backtracking (damped Newton)
t *= 0.5
x = x + t * p
print(f"{k:2d}: t = {t:<7} f = {f(x):.3e} |grad| = {np.linalg.norm(grad(x)):.1e}")
if np.linalg.norm(grad(x)) < 1e-10:
break 1: t = 1.0 f = 4.732e+00 |grad| = 4.6e+00
2: t = 0.125 f = 4.087e+00 |grad| = 2.9e+01
3: t = 1.0 f = 3.229e+00 |grad| = 1.2e+01
4: t = 1.0 f = 3.214e+00 |grad| = 3.0e+01
5: t = 1.0 f = 1.943e+00 |grad| = 3.6e+00
6: t = 0.25 f = 1.600e+00 |grad| = 9.2e+00
7: t = 1.0 f = 1.178e+00 |grad| = 4.9e+00
8: t = 1.0 f = 9.224e-01 |grad| = 8.7e+00
9: t = 1.0 f = 5.975e-01 |grad| = 1.8e+00
10: t = 0.5 f = 4.526e-01 |grad| = 5.9e+00
11: t = 1.0 f = 2.808e-01 |grad| = 2.2e+00
12: t = 1.0 f = 2.114e-01 |grad| = 9.4e+00
13: t = 1.0 f = 8.902e-02 |grad| = 4.9e-01
14: t = 0.5 f = 5.154e-02 |grad| = 3.9e+00
15: t = 1.0 f = 1.999e-02 |grad| = 1.2e+00
16: t = 1.0 f = 7.169e-03 |grad| = 2.5e+00
17: t = 1.0 f = 1.070e-03 |grad| = 2.4e-01
18: t = 1.0 f = 7.777e-05 |grad| = 3.5e-01
19: t = 1.0 f = 2.825e-07 |grad| = 3.9e-03
20: t = 1.0 f = 8.517e-12 |grad| = 1.2e-04
21: t = 1.0 f = 3.744e-21 |grad| = 4.5e-10
22: t = 1.0 f = 0.000e+00 |grad| = 0.0e+00
Compare with the 13,756 steps gradient descent needed in Line Search and Backtracking. Watch the last few lines: once the full step is accepted, the gradient norm roughly squares each iteration.
Why it is fast, and when it fails
Near a minimum with positive definite Hessian (and a Lipschitz Hessian), Newton's method converges quadratically: . Far from the minimum, three things can go wrong:
| Problem | Symptom | Fix |
|---|---|---|
| step too long | increases | line search (damped Newton), as above |
| Hessian indefinite | step points uphill or to a saddle | add (modified Newton) or use a trust region |
| Hessian singular | system cannot be solved | regularize, or use a trust region |
import numpy as np
# f(x, y) = x^4 + y^2 - 2x^2 ; at (0.3, 0.1) the Hessian is indefinite
grad = lambda p: np.array([4 * p[0]**3 - 4 * p[0], 2 * p[1]])
hess = lambda p: np.array([[12 * p[0]**2 - 4, 0.0], [0.0, 2.0]])
x = np.array([0.3, 0.1])
H, g = hess(x), grad(x)
p = np.linalg.solve(H, -g)
print("Hessian eigenvalues:", np.linalg.eigvalsh(H))
print("pure Newton step:", np.round(p, 4), " slope g^T p =", round(float(g @ p), 4), "(positive: uphill)")
lam = 1.0 - np.linalg.eigvalsh(H).min() # shift to make H + lam I positive definite
p2 = np.linalg.solve(H + lam * np.eye(2), -g)
print("modified step:", np.round(p2, 4), " slope =", round(float(g @ p2), 4))Hessian eigenvalues: [-2.92 2. ]
pure Newton step: [-0.374 -0.1 ] slope g^T p = 0.3884 (positive: uphill)
modified step: [ 1.092 -0.0338] slope = -1.1992
The pure Newton step heads for the saddle at , increasing ; the shifted Hessian gives a descent direction.
Affine invariance
If you change variables , gradient descent behaves completely differently, but Newton's iterates map exactly onto each other. Newton's method does not care about the condition number of the Hessian, only about how fast the Hessian changes. This is why badly scaled problems that cripple gradient descent are easy for Newton.
The cost
Each iteration needs the Hessian ( entries) and a linear solve ( for a dense Cholesky factorization). For up to a few thousand that is fine; beyond that, use Newton-CG (solve the Newton system approximately with conjugate gradient, needing only Hessian-vector products) or a quasi-Newton method.
In software
scipy.optimize.minimize(method="Newton-CG")and"trust-exact","trust-ncg"; logistic regression solvers (LogisticRegression(solver="newton-cg"), IRLS in statsmodels' GLMs) are Newton's method.- Interior-point solvers for LP, QP and convex problems take damped Newton steps on a barrier function (Interior-Point Methods).
Exercises
Exercise 1: One step on a quadratic
Show that Newton's method minimizes with in one step from any start.
Answer
The step lands on .
Solution
and , so , the unique minimizer. The quadratic model is the function itself.
Exercise 2: One dimension
Write Newton's update for minimizing and apply it from twice.
Answer
; .
Solution
and , so . Then : the error squares each step, quadratic convergence toward .
Exercise 3: Scaling invariance (code)
Minimize with one Newton step and with 100 gradient steps of size from . Compare.
Answer
Newton lands exactly on ; gradient descent is still far from .
Solution
import numpy as np
grad = lambda p: np.array([2 * (p[0] - 1), 2000 * (p[1] - 2)])
H = np.diag([2.0, 2000.0])
x0 = np.zeros(2)
print("Newton:", x0 - np.linalg.solve(H, grad(x0)))
x = x0.copy()
for _ in range(100):
x = x - grad(x) / 2000
print("100 gradient steps:", np.round(x, 4))Newton: [1. 2.]
100 gradient steps: [0.0952 2. ]
With step , the coordinate shrinks its error only by the factor per step.
Test yourself
The Newton step solves…
Near a minimum with positive definite Hessian, Newton's method converges…
- sublinearly
- linearly
- quadratically
- in exactly steps
What can go wrong with a pure Newton step when the Hessian is indefinite?
- Nothing
- The step may point uphill or toward a saddle point
- The gradient becomes zero
- The function becomes convex