TExcelTAD
Home › Optimization › Newton's Method

Newton's Method

Gradient descent uses a linear model of ff 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 ff near xk\mathbf{x}_k by its second-order Taylor expansion:

mk(p)=f(xk)+∇f(xk)Tp+12pT∇2f(xk)pm_k(\mathbf{p}) = f(\mathbf{x}_k) + \nabla f(\mathbf{x}_k)^T\mathbf{p} + \frac{1}{2}\mathbf{p}^T\nabla^2 f(\mathbf{x}_k)\mathbf{p}

If the Hessian is positive definite, the model's minimum is where its gradient vanishes:

∇2f(xk) pk=−∇f(xk),xk+1=xk+pk\nabla^2 f(\mathbf{x}_k)\,\mathbf{p}_k = -\nabla f(\mathbf{x}_k), \qquad \mathbf{x}_{k+1} = \mathbf{x}_k + \mathbf{p}_k

In code you solve the linear system; you never invert the Hessian.

python · try it yourself
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 t=1t = 1 is accepted, the gradient norm roughly squares each iteration.

Why it is fast, and when it fails

Near a minimum x∗\mathbf{x}^* with positive definite Hessian (and a Lipschitz Hessian), Newton's method converges quadratically: ∥xk+1−x∗∥≤M∥xk−x∗∥2\lVert \mathbf{x}_{k+1} - \mathbf{x}^* \rVert \le M\lVert \mathbf{x}_k - \mathbf{x}^* \rVert^2. Far from the minimum, three things can go wrong:

Problem Symptom Fix
step too long ff increases line search (damped Newton), as above
Hessian indefinite step points uphill or to a saddle add λI\lambda I (modified Newton) or use a trust region
Hessian singular system cannot be solved regularize, or use a trust region
python · try it yourself
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 x=0x = 0, increasing ff; the shifted Hessian gives a descent direction.

Affine invariance

If you change variables x=Az\mathbf{x} = A\mathbf{z}, 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 (n2n^2 entries) and a linear solve (O(n3)O(n^3) for a dense Cholesky factorization). For nn 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 f(x)=12xTQx−bTxf(\mathbf{x}) = \frac{1}{2}\mathbf{x}^TQ\mathbf{x} - \mathbf{b}^T\mathbf{x} with Q≻0Q \succ 0 in one step from any start.

Answer

The step lands on x=Q−1b\mathbf{x} = Q^{-1}\mathbf{b}.

Solution

∇f=Qx0−b\nabla f = Q\mathbf{x}_0 - \mathbf{b} and ∇2f=Q\nabla^2 f = Q, so x1=x0−Q−1(Qx0−b)=Q−1b\mathbf{x}_1 = \mathbf{x}_0 - Q^{-1}(Q\mathbf{x}_0 - \mathbf{b}) = Q^{-1}\mathbf{b}, the unique minimizer. The quadratic model is the function itself.

Exercise 2: One dimension

Write Newton's update for minimizing f(x)=x−ln⁡xf(x) = x - \ln x and apply it from x0=0.5x_0 = 0.5 twice.

Answer

xk+1=2xk−xk2x_{k+1} = 2x_k - x_k^2; 0.5→0.75→0.93750.5 \to 0.75 \to 0.9375.

Solution

f′(x)=1−1/xf'(x) = 1 - 1/x and f′′(x)=1/x2f''(x) = 1/x^2, so x−f′/f′′=x−x2(1−1/x)=2x−x2x - f'/f'' = x - x^2(1 - 1/x) = 2x - x^2. Then 1−xk+1=(1−xk)21 - x_{k+1} = (1 - x_k)^2: the error squares each step, quadratic convergence toward x∗=1x^* = 1.

Exercise 3: Scaling invariance (code)

Minimize f(x,y)=(x−1)2+1000(y−2)2f(x, y) = (x - 1)^2 + 1000(y - 2)^2 with one Newton step and with 100 gradient steps of size 1/20001/2000 from (0,0)(0, 0). Compare.

Answer

Newton lands exactly on (1,2)(1, 2); gradient descent is still far from x=1x = 1.

Solution
python · try it yourself
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 1/L=1/20001/L = 1/2000, the xx coordinate shrinks its error only by the factor 1−2/20001 - 2/2000 per step.

Test yourself

The Newton step p\mathbf{p} solves…

  1. ∇f(x) p=−∇2f(x)\nabla f(\mathbf{x})\,\mathbf{p} = -\nabla^2 f(\mathbf{x})
  2. ∇2f(x) p=−∇f(x)\nabla^2 f(\mathbf{x})\,\mathbf{p} = -\nabla f(\mathbf{x})
  3. p=−∇f(x)\mathbf{p} = -\nabla f(\mathbf{x})
  4. pTp=1\mathbf{p}^T\mathbf{p} = 1

Near a minimum with positive definite Hessian, Newton's method converges…

  1. sublinearly
  2. linearly
  3. quadratically
  4. in exactly nn steps

What can go wrong with a pure Newton step when the Hessian is indefinite?

  1. Nothing
  2. The step may point uphill or toward a saddle point
  3. The gradient becomes zero
  4. The function becomes convex