Course contents 12 / 16

12 / 16 · Optimization

Unconstrained optimization

Finding the minimum of a smooth function, and how fast: optimality conditions, gradient descent rates and the condition number, Armijo line search, Newton's method and its quadratic convergence, and BFGS.

On this page 8

Fit a logistic regression with an L2 penalty to 40 points with two features, the second ten times larger than the first (the Code section builds the data). The loss is

f(w)=140∑i=140log⁡(1+e−yiw⊤xi)+0.012∥w∥2,f(w) = \frac1{40}\sum_{i=1}^{40} \log\big(1 + e^{-y_i w^\top x_i}\big) + \frac{0.01}{2}\lVert w\rVert^2 ,

where xix_i are the points and yi=±1y_i = \pm1 their labels. In the language of the previous note, the penalty makes ff strongly convex with μ=0.01\mu = 0.01, and the bound p(1−p)≤14p(1 - p) \le \frac14 makes it LL-smooth with L=λmax⁡(X⊤X)/160+0.01=7.898L = \lambda_{\max}(X^\top X)/160 + 0.01 = 7.898.

Gradient descent with the safe step 1/L1/L, started at w=0w = 0, lowers the excess loss f−f∗f - f^* from 0.380.38 to 0.230.23 in 10 steps, to 0.0690.069 in 100 and to 3.8⋅10−53.8 \cdot 10^{-5} in 1,000. It needs 2,727 steps to reach 10−1010^{-10}. Every step decreases the loss, and nothing is wrong with the code. Newton’s method solves the same problem in 5 steps.

Why does gradient descent need thousands of steps for two unknowns, and what does it guarantee on a loss that is not a quadratic? The answers come from the constants μ\mu and LL, and the cure from the curvature, which the gradient ignores.

Optimality conditions#

Before an algorithm, a test: what must hold at a minimizer, and what is enough to be sure of one? Write x∗x^* for a candidate and, for ff with continuous second derivatives, expand it to second order, as in the gradients note:

f(x∗+h)=f(x∗)+∇f(x∗)⊤h+12h⊤∇2f(x∗) h+o(∥h∥2).\begin{aligned} f(x^* + h) ={}& f(x^*) + \nabla f(x^*)^\top h \\ &+ \tfrac12 h^\top\nabla^2 f(x^*)\,h + o\big(\lVert h\rVert^2\big) . \end{aligned}

Each condition reads one term of the expansion:

ConditionStatementReason
first-order, necessarylocal minimum ⇒∇f(x∗)=0\Rightarrow \nabla f(x^*) = 0otherwise h=−t∇f(x∗)h = -t\nabla f(x^*) lowers ff by t∥∇f∥2+o(t)t\lVert\nabla f\rVert^2 + o(t)
second-order, necessarylocal minimum ⇒∇2f(x∗)⪰0\Rightarrow \nabla^2 f(x^*) \succeq 0otherwise some vv with v⊤∇2f v<0v^\top\nabla^2 f\, v \lt 0 lowers ff along tvtv
second-order, sufficient∇f=0\nabla f = 0 and ∇2f≻0⇒\nabla^2 f \succ 0 \Rightarrow strict local minimumthe quadratic term is at least 12λmin⁡∥h∥2\frac12\lambda_{\min}\lVert h\rVert^2 (Rayleigh quotient), and for small hh the o(∥h∥2)o(\lVert h\rVert^2) term is below half of that
convex ff∇f(x∗)=0⇒\nabla f(x^*) = 0 \Rightarrow global minimumthe tangent bound

The gap between necessary and sufficient is real. f(x1,x2)=x12+x24f(x_1, x_2) = x_1^2 + x_2^4 and f(x1,x2)=x12−x24f(x_1, x_2) = x_1^2 - x_2^4 both have zero gradient and the Hessian diag⁡(2,0)\operatorname{diag}(2, 0) at the origin, positive semidefinite but singular. The first has a minimum there and the second a saddle: along x2x_2 the quadratic term vanishes and the quartic decides. An algorithm that only drives the gradient to zero can stop at any critical point unless the function is convex.

Gradient descent on a smooth function#

Gradient descent moves against the gradient, xk+1=xk−η gkx_{k+1} = x_k - \eta\, g_k, where gk=∇f(xk)g_k = \nabla f(x_k) and η\eta is the step. How much does one step gain? An LL-smooth function lies below the parabola of curvature LL that touches it at the current point, the upper bound of the previous note. Put y−x=−ηgy - x = -\eta g into it, with g=∇f(x)g = \nabla f(x):

f(x−ηg)≤f(x)−η∥g∥2+Lη22∥g∥2=f(x)−η(1−Lη2)∥g∥2.\begin{aligned} f(x - \eta g) &\le f(x) - \eta\lVert g\rVert^2 + \frac{L\eta^2}{2}\lVert g\rVert^2 \\ &= f(x) - \eta\Big(1 - \frac{L\eta}{2}\Big)\lVert g\rVert^2 . \end{aligned}

This is the descent lemma, the many-variable form of the one-variable bound in the derivatives note. Every η∈(0,2/L)\eta \in (0, 2/L) decreases ff while the gradient is nonzero, because then 1−Lη/2>01 - L\eta/2 \gt 0. The guaranteed decrease η−Lη2/2\eta - L\eta^2/2 has derivative 1−Lη1 - L\eta in η\eta, so it is largest at η=1/L\eta = 1/L, where it equals 1/(2L)1/(2L):

f(xk+1)≤f(xk)−12L∥gk∥2.f(x_{k+1}) \le f(x_k) - \frac{1}{2L}\lVert g_k\rVert^2 .

Summed over KK steps, the decreases telescope, and the smallest of KK numbers is at most their average:

K2Lmin⁡k<K∥gk∥2≤12L∑k=0K−1∥gk∥2≤f(x0)−f(xK)≤f(x0)−inf⁡f.\begin{aligned} \frac{K}{2L}\min_{k \lt K}\lVert g_k\rVert^2 &\le \frac{1}{2L}\sum_{k=0}^{K-1}\lVert g_k\rVert^2 \\ &\le f(x_0) - f(x_K) \le f(x_0) - \inf f . \end{aligned}

Without convexity this is all that holds: the smallest gradient among the first KK steps satisfies

min⁡k<K∥gk∥≤2L (f(x0)−inf⁡f)K.\min_{k \lt K} \lVert g_k\rVert \le \sqrt{\frac{2L\,\big(f(x_0) - \inf f\big)}{K}} .

The iterates approach a critical point at the rate 1/K1/\sqrt K in the gradient norm, with no promise about which kind. The gradient descent note of the machine learning course quotes the same three regimes this section proves.

Convex and smooth: the excess loss falls like 1/k#

Convexity bounds how far the loss is above its minimum in terms of the gradient and the distance to x∗x^*. Each step then trades that excess for a decrease of the squared distance. Assume a minimizer x∗x^* exists, write ek=xk−x∗e_k = x_k - x^* for the error of an iterate and take η=1/L\eta = 1/L:

f(xk+1)≤f(xk)−12L∥gk∥2≤f∗+gk⊤ek−12L∥gk∥2=f∗+L2(∥ek∥2−∥ek−1Lgk∥2)=f∗+L2(∥ek∥2−∥ek+1∥2).\begin{aligned} f(x_{k+1}) &\le f(x_k) - \tfrac{1}{2L}\lVert g_k\rVert^2 \\ &\le f^* + g_k^\top e_k - \tfrac{1}{2L}\lVert g_k\rVert^2 \\ &= f^* + \tfrac L2\big(\lVert e_k\rVert^2 - \lVert e_k - \tfrac1L g_k\rVert^2\big) \\ &= f^* + \tfrac L2\big(\lVert e_k\rVert^2 - \lVert e_{k+1}\rVert^2\big) . \end{aligned}
  1. The descent lemma.
  2. The tangent bound at xkx_k towards x∗x^*, f∗≥f(xk)−gk⊤ekf^* \ge f(x_k) - g_k^\top e_k.
  3. Expanding the square, ∥ek−gk/L∥2=∥ek∥2−2gk⊤ek/L+∥gk∥2/L2\lVert e_k - g_k/L\rVert^2 = \lVert e_k\rVert^2 - 2g_k^\top e_k/L + \lVert g_k\rVert^2/L^2, gives back the previous line.
  4. The step itself, ek+1=ek−1Lgke_{k+1} = e_k - \tfrac1L g_k.

Summing over k=0,…,K−1k = 0, \dots, K - 1 telescopes the right side to at most L2∥e0∥2\frac L2\lVert e_0\rVert^2. The left side is a sum of KK excesses f(xk+1)−f∗f(x_{k+1}) - f^*, each at least the last one, because the loss never increases. Hence

f(xK)−f∗≤L ∥x0−x∗∥22K.f(x_K) - f^* \le \frac{L\,\lVert x_0 - x^*\rVert^2}{2K} .

On the way, the chain shows ∥ek+1∥≤∥ek∥\lVert e_{k+1}\rVert \le \lVert e_k\rVert, because its left side minus f∗f^* is non-negative: the iterates never move away from the minimizer. Merkulov (2024), lecture 9, proves the bound the same way.

For the opening problem, ∥w∗∥2=3.4682+0.3802=12.17\lVert w^*\rVert^2 = 3.468^2 + 0.380^2 = 12.17, so the bound is 7.898⋅12.17/(2k)=48.1/k7.898 \cdot 12.17/(2k) = 48.1/k, at most 0.0480.048 after 1,000 steps. The run reaches 3.8⋅10−53.8 \cdot 10^{-5}. The bound is valid and pessimistic, because it uses neither the penalty nor the curvature near the minimum.

Strongly convex: a linear rate set by κ#

Strong convexity keeps the gradient large while the loss is far from its minimum, through the Polyak–Łojasiewicz inequality ∥g∥2≥2μ(f(x)−f∗)\lVert g\rVert^2 \ge 2\mu\big(f(x) - f^*\big) of the previous note. Put it into the descent lemma, after subtracting f∗f^* from both sides:

f(xk+1)−f∗≤f(xk)−f∗−12L∥gk∥2≤f(xk)−f∗−2μ2L(f(xk)−f∗)=(1−μL)(f(xk)−f∗).\begin{aligned} f(x_{k+1}) - f^* &\le f(x_k) - f^* - \tfrac{1}{2L}\lVert g_k\rVert^2 \\ &\le f(x_k) - f^* - \tfrac{2\mu}{2L}\big(f(x_k) - f^*\big) \\ &= \Big(1 - \frac{\mu}{L}\Big)\big(f(x_k) - f^*\big) . \end{aligned}

The second line replaces ∥gk∥2\lVert g_k\rVert^2 by the smaller 2μ(f(xk)−f∗)2\mu\big(f(x_k) - f^*\big).

Applying this kk times, with the condition number κ=L/μ\kappa = L/\mu:

  f(xk)−f∗≤(1−1κ)k(f(x0)−f∗)  \boxed{\; f(x_k) - f^* \le \Big(1 - \frac1\kappa\Big)^k \big(f(x_0) - f^*\big) \;}

Since 1−u≤e−u1 - u \le e^{-u}, the tangent bound of the convex e−ue^{-u} at 0, the right side is at most e−k/κ(f(x0)−f∗)e^{-k/\kappa}\big(f(x_0) - f^*\big). To shrink the excess loss by a factor ε\varepsilon it is enough that e−k/κ≤εe^{-k/\kappa} \le \varepsilon, that is, k≥κln⁡(1/ε)k \ge \kappa\ln(1/\varepsilon) steps: the number of steps is linear in the condition number. For κ=100\kappa = 100 and ε=10−6\varepsilon = 10^{-6} that is 1,375 steps by the first bound, ln⁡106/(−ln⁡0.99)=1374.6\ln 10^6/(-\ln 0.99) = 1374.6, and 1,382 by the second, 100ln⁡106=1381.6100 \ln 10^6 = 1381.6. Each step costs one gradient, so on badly conditioned problems gradient descent is slow however cheap its steps are. The proof used only the Polyak–Łojasiewicz inequality, not convexity, so the linear rate holds for every smooth function that satisfies it (Merkulov, 2024, lecture 9; Karimi, Nutini and Schmidt, 2016, Theorem 1).

Why the condition number? Near the minimum ff is close to a quadratic with Hessian ∇2f(x∗)\nabla^2 f(x^*). In its eigenvector coordinates the level set 12∑iλizi2=c\tfrac12\sum_i \lambda_i z_i^2 = c has half-axes 2c/λi\sqrt{2c/\lambda_i}, in the ratio L/μ=κ\sqrt{L/\mu} = \sqrt\kappa. The gradient is perpendicular to the level set, so it points at the minimum only when the level sets are round. On a quadratic, a step η\eta multiplies the error along an eigenvector with eigenvalue λ\lambda by 1−ηλ1 - \eta\lambda. The safe step is limited by the steep direction, η≤1/L\eta \le 1/L, and along the flat direction it then removes only a fraction μ/L\mu/L of the error. Strang (2018) draws this as a zigzag down a narrow valley.

The exact analysis on a quadratic gives the best constant step 2/(L+μ)2/(L + \mu), with the error factor (κ−1)/(κ+1)(\kappa - 1)/(\kappa + 1) per step: the same linear dependence on κ\kappa with a better constant. Momentum reduces it to κ\sqrt\kappa, and no better dependence is possible for methods that move within the span of past gradients. Bubeck (2015), Theorem 3.15, builds an LL-smooth, μ\mu-strongly convex function on the infinite-dimensional space ℓ2\ell_2 on which every such method has f(xt)−f∗≥μ2((κ−1)/(κ+1))2(t−1)∥x1−x∗∥2f(x_t) - f^* \ge \tfrac\mu2\big((\sqrt\kappa - 1)/(\sqrt\kappa + 1)\big)^{2(t - 1)}\lVert x_1 - x^*\rVert^2.

For the opening problem, κ=7.898/0.01=790\kappa = 7.898/0.01 = 790, and the bound promises f−f∗≤10−10f - f^* \le 10^{-10} after 790ln⁡(0.38/10−10)=17,431790 \ln(0.38/10^{-10}) = 17{,}431 steps. The run needed 2,727. The global μ\mu and LL are worst cases over the whole space. Near the minimum the Hessian has eigenvalues 0.0290.029 and 3.223.22, a local condition number of 110. With the step 1/L1/L, the error along the flat eigenvector shrinks by 1−0.0293/7.8981 - 0.0293/7.898 per step, and the excess loss, quadratic in the error, by the square of that, 0.992600.99260. That predicts 22.07/0.00743=2,97022.07/0.00743 = 2{,}970 steps from 0.380.38 to 10−1010^{-10}; the run needs 2,727 because its early steps are faster, and its late steps shrink the excess by 0.992590.99259, as predicted.

Line search: the Armijo condition#

LL is rarely known, and 1/L1/L is often far shorter than the steps the function allows. A line search chooses the step length at every iteration, along a search direction dkd_k that points downhill, gk⊤dk<0g_k^\top d_k \lt 0. The slope at the start promises a decrease of t gk⊤dkt\, g_k^\top d_k for a step tt; the Armijo condition accepts the step if it achieves at least a fixed fraction c∈(0,1)c \in (0, 1) of that:

f(xk+t dk)≤f(xk)+c t gk⊤dk.f(x_k + t\,d_k) \le f(x_k) + c\, t\, g_k^\top d_k .

Backtracking tries t=1,12,14,…t = 1, \frac12, \frac14, \dots until the condition holds. Armijo (1966), Corollary 2, proposed exactly this halving with c=12c = \frac12; a much smaller cc, about 10−410^{-4}, is typical in practice (Merkulov, 2024, lecture 2). The search always ends: by Taylor, f(xk+tdk)=f(xk)+t gk⊤dk+o(t)f(x_k + t d_k) = f(x_k) + t\,g_k^\top d_k + o(t), and since c<1c \lt 1 and gk⊤dk<0g_k^\top d_k \lt 0, the actual decrease beats the required one for all small enough tt.

How long is the accepted step? For gradient descent, d=−gd = -g. The descent lemma gives the first inequality below, and the second is the Armijo test:

f(x−tg)≤f(x)−t(1−Lt2)∥g∥2≤f(x)−c t ∥g∥2.\begin{aligned} f(x - tg) &\le f(x) - t\big(1 - \tfrac{Lt}{2}\big)\lVert g\rVert^2 \\ &\le f(x) - c\,t\,\lVert g\rVert^2 . \end{aligned}

The second inequality holds whenever 1−Lt/2≥c1 - Lt/2 \ge c, that is, t≤2(1−c)/Lt \le 2(1 - c)/L. Halving therefore stops at some t≥min⁡(1,(1−c)/L)=tmin⁡t \ge \min\big(1, (1 - c)/L\big) = t_{\min}: the first trial step below 2(1−c)/L2(1 - c)/L is more than half of it. Every accepted step lowers ff by at least c tmin⁡∥g∥2c\,t_{\min}\lVert g\rVert^2, and the Polyak–Łojasiewicz argument above, with 12L\tfrac1{2L} replaced by c tmin⁡c\,t_{\min}, gives f(xk+1)−f∗≤(1−2μc tmin⁡)(f(xk)−f∗)f(x_{k+1}) - f^* \le (1 - 2\mu c\, t_{\min})\big(f(x_k) - f^*\big), without knowing LL. On the opening problem, gradient descent with backtracking (c=10−4c = 10^{-4}) needs 526 steps instead of 2,727, because it takes steps longer than 1/L1/L wherever the local curvature allows.

A step can satisfy Armijo and still be too short to make progress. The Wolfe conditions add a curvature condition that rules out steps after which the slope has hardly changed:

∇f(xk+tdk)⊤dk≥c2 gk⊤dk,\nabla f(x_k + t d_k)^\top d_k \ge c_2\, g_k^\top d_k ,

with c<c2<1c \lt c_2 \lt 1, typically c2=0.9c_2 = 0.9 (Merkulov, 2024, lecture 2). Quasi-Newton methods need it, as shown below.

Newton’s method#

Gradient descent uses a linear model of ff, and a linear model has no minimum, so it needs a step size. Newton’s method uses the quadratic model instead:

mk(d)=f(xk)+gk⊤d+12d⊤∇2f(xk) d.m_k(d) = f(x_k) + g_k^\top d + \tfrac12d^\top\nabla^2 f(x_k)\,d .

When ∇2f(xk)≻0\nabla^2 f(x_k) \succ 0, mkm_k is a strictly convex quadratic, and its minimum is where its gradient gk+∇2f(xk) dg_k + \nabla^2 f(x_k)\,d vanishes. That gives a complete step, length and direction included:

∇2f(xk) dk=−gk,xk+1=xk+dk.\begin{aligned} \nabla^2 f(x_k)\,d_k &= -g_k , \\ x_{k+1} &= x_k + d_k . \end{aligned}

The same step solves the linearized optimality condition ∇f(xk+d)≈gk+∇2f(xk) d=0\nabla f(x_k + d) \approx g_k + \nabla^2 f(x_k)\,d = 0: Newton’s method for minimization is Newton’s root-finding method applied to ∇f\nabla f. A third reading explains why the condition number disappears. Gradient descent takes the steepest direction in the Euclidean norm. Measured in the norm ∥v∥H=v⊤Hv\lVert v\rVert_H = \sqrt{v^\top H v} of the local Hessian H≻0H \succ 0, the steepest direction becomes −H−1∇f-H^{-1}\nabla f, the Newton direction, and in that norm the level sets near the minimum are round (Boyd and Vandenberghe, EE364a, lecture 9, the interpretation of the Newton step). To see it, substitute u=H1/2vu = H^{1/2}v: the slope g⊤v=(H−1/2g)⊤ug^\top v = (H^{-1/2}g)^\top u over ∥u∥≤1\lVert u\rVert \le 1 is most negative for uu opposite to H−1/2gH^{-1/2}g, by Cauchy–Schwarz, that is, for vv along −H−1g-H^{-1}g. The quadratic model’s level sets 12h⊤Hh=c\tfrac12 h^\top H h = c are spheres ∥h∥H=2c\lVert h\rVert_H = \sqrt{2c} in that norm.

The Newton direction is computed by solving a linear system, by a Cholesky factorization when the Hessian is positive definite, never by forming the inverse. On a quadratic f(x)=12x⊤Ax−b⊤xf(x) = \tfrac12x^\top A x - b^\top x the model is the function itself, and one step from any point lands on x∗=A−1bx^* = A^{-1}b, whatever κ\kappa is. When nn is too large to factor AA, the iterative methods note minimizes the same quadratic by conjugate gradients, which need only products with AA.

Newton’s method is affine invariant. In coordinates x=Azx = Az with an invertible AA, the chain rule turns the Hessian into A⊤∇2fAA^\top\nabla^2 f A and the gradient into A⊤∇fA^\top\nabla f, and the Newton step in zz is exactly the Newton step in xx mapped back:

dz=−(A⊤HA)−1A⊤g=−A−1H−1A−⊤A⊤g=A−1(−H−1g).\begin{aligned} d_z &= -\big(A^\top H A\big)^{-1}A^\top g \\ &= -A^{-1}H^{-1}A^{-\top}A^\top g = A^{-1}\big(-H^{-1}g\big) . \end{aligned}

Rescaling or rotating the features, which changes κ\kappa and the speed of gradient descent, changes nothing for Newton. Logistic regression is fitted this way, as iteratively reweighted least squares.

Quadratic convergence#

The Newton step solves a linearized equation, so its error comes only from the change of the Hessian between xkx_k and x∗x^*, which is proportional to the distance. Assume that on a ball around x∗x^* the Hessian satisfies ∇2f⪰μI\nabla^2 f \succeq \mu I and is MM-Lipschitz, ∥∇2f(x)−∇2f(y)∥≤M∥x−y∥\lVert\nabla^2 f(x) - \nabla^2 f(y)\rVert \le M\lVert x - y\rVert, and that xkx_k lies in that ball. Write Hk=∇2f(xk)H_k = \nabla^2 f(x_k) and use ∇f(x∗)=0\nabla f(x^*) = 0. The difference ∇f(xk)−∇f(x∗)\nabla f(x_k) - \nabla f(x^*) is the integral of the Hessian along the segment from x∗x^* to xkx_k, and HkekH_k e_k is the integral of a constant:

ek+1=ek−Hk−1(∇f(xk)−∇f(x∗))=Hk−1∫01(Hk−∇2f(x∗+s ek)) ek ds.\begin{aligned} e_{k+1} &= e_k - H_k^{-1}\big(\nabla f(x_k) - \nabla f(x^*)\big) \\ &= H_k^{-1}\int_0^1 \big(H_k - \nabla^2 f(x^* + s\,e_k)\big)\,e_k\, ds . \end{aligned}

Now take norms: ∥Hk−1∥≤1/μ\lVert H_k^{-1}\rVert \le 1/\mu, since the eigenvalues of Hk−1H_k^{-1} are the reciprocals of those of HkH_k, and the two Hessians in the integral are evaluated at points (1−s)∥ek∥(1 - s)\lVert e_k\rVert apart:

∥ek+1∥≤1μ∫01M(1−s) ∥ek∥2 ds=M2μ∥ek∥2.\begin{aligned} \lVert e_{k+1}\rVert &\le \frac1\mu \int_0^1 M(1 - s)\,\lVert e_k\rVert^2\, ds \\ &= \frac{M}{2\mu}\lVert e_k\rVert^2 . \end{aligned}

Once ∥ek∥<μ/M\lVert e_k\rVert \lt \mu/M, the factor M2μ∥ek∥\frac{M}{2\mu}\lVert e_k\rVert is below 12\frac12, so ∥ek+1∥<12∥ek∥\lVert e_{k+1}\rVert \lt \frac12\lVert e_k\rVert: the next iterate stays in the ball, the assumptions hold again, and the factor shrinks at every step. Taking logarithms, log⁡∥ek+1∥≤2log⁡∥ek∥+log⁡M2μ\log\lVert e_{k+1}\rVert \le 2\log\lVert e_k\rVert + \log\frac{M}{2\mu}: the number of correct digits roughly doubles per step. This is quadratic convergence, against the linear convergence of gradient descent, which adds a fixed number of digits per step. Merkulov (2024), lecture 15, assumes the bound μ\mu only at x∗x^* and finds that the error shrinks from any start with ∥e0∥<2μ/(3M)\lVert e_0\rVert \lt 2\mu/(3M).

The function f(x)=x−ln⁡xf(x) = x - \ln x on x>0x \gt 0 has f′(x)=1−1/xf'(x) = 1 - 1/x, f′′(x)=1/x2f''(x) = 1/x^2 and its minimum at x∗=1x^* = 1. The Newton step is x−(1−1/x) x2=2x−x2x - (1 - 1/x)\,x^2 = 2x - x^2, so xk+1−1=−(xk−1)2x_{k+1} - 1 = -(x_k - 1)^2 exactly: the error is squared at every step. This matches the bound in the limit: on a shrinking interval around 1, μ→f′′(1)=1\mu \to f''(1) = 1 and M→∣f′′′(1)∣=2M \to \lvert f'''(1)\rvert = 2, so M/(2μ)→1M/(2\mu) \to 1, the exact constant. From x0=0.5x_0 = 0.5:

kk012345
xk−1x_k - 1−0.5-0.5−0.25-0.25−0.0625-0.0625−3.9⋅10−3-3.9 \cdot 10^{-3}−1.5⋅10−5-1.5 \cdot 10^{-5}−2.3⋅10−10-2.3 \cdot 10^{-10}

Damping#

Far from the minimum the quadratic model can be a poor guide. In the example, from x0=3x_0 = 3 the full step lands at 2⋅3−9=−32 \cdot 3 - 9 = -3, outside the domain. Damped Newton keeps the Newton direction and chooses its length by backtracking with the Armijo condition, starting from t=1t = 1. The Newton direction is a descent direction whenever the Hessian is positive definite, since g⊤d=−g⊤[∇2f]−1g<0g^\top d = -g^\top[\nabla^2 f]^{-1}g \lt 0.

From x0=3x_0 = 3 the direction is d=−(1−13)⋅9=−6d = -(1 - \frac13) \cdot 9 = -6. The steps t=1t = 1 and t=12t = \frac12 leave the domain, and t=14t = \frac14 gives x=1.5x = 1.5 with f=1.095f = 1.095, below f(3)=1.901f(3) = 1.901 by more than the Armijo threshold. From 1.51.5 the full step is accepted at every iteration, 0.750.75, 0.93750.9375, 0.9961,…0.9961, \dots, and the quadratic phase begins. The two phases are general. For a strongly convex ff with Lipschitz Hessian, Boyd and Vandenberghe (2004), Section 9.5.3, prove that while ∥∇f∥\lVert\nabla f\rVert is above a threshold every damped step lowers ff by at least a fixed amount, and below it every step is a full step with quadratic convergence. Why the full step passes near the minimum is visible on a quadratic, where the model is exact: with Hd=−gHd = -g, f(x+d)=f(x)+g⊤d+12d⊤Hd=f(x)+12g⊤df(x + d) = f(x) + g^\top d + \tfrac12 d^\top H d = f(x) + \tfrac12 g^\top d. Since g⊤d<0g^\top d \lt 0, this meets the Armijo threshold f(x)+c g⊤df(x) + c\,g^\top d exactly when c≤12c \le \frac12, so damping costs nothing there.

Each step still forms the n×nn \times n Hessian and solves a linear system: O(n3)O(n^3) operations and O(n2)O(n^2) memory, out of reach for nn in the millions.

Quasi-Newton methods: BFGS#

Can we keep Newton’s step without the Hessian? A quasi-Newton method uses dk=−Hkgkd_k = -H_k g_k, where HkH_k approximates the inverse Hessian. It is built from gradients only and corrected after every step. The information available after a step is the pair

sk=xk+1−xk,yk=gk+1−gk.\begin{aligned} s_k &= x_{k+1} - x_k , \\ y_k &= g_{k+1} - g_k . \end{aligned}

Along the step the true Hessian maps sks_k approximately to yky_k, as the slope of a secant does in one variable. The secant equation asks the new approximation to reproduce this exactly, Hk+1yk=skH_{k+1}y_k = s_k. A symmetric rank-one correction can satisfy it but may lose positive definiteness, since it can subtract a rank-one term; the rank-two BFGS update (Broyden, Fletcher, Goldfarb and Shanno, 1970) keeps it (Merkulov, 2024, lecture 15). With ρk=1/(yk⊤sk)\rho_k = 1/(y_k^\top s_k):

Hk+1=(I−ρkskyk⊤) Hk (I−ρkyksk⊤)+ρksksk⊤.\begin{aligned} H_{k+1} ={}& \big(I - \rho_k s_k y_k^\top\big)\, H_k\, \big(I - \rho_k y_k s_k^\top\big) \\ &+ \rho_k s_k s_k^\top . \end{aligned}

The secant equation is a one-line check: (I−ρys⊤)y=y−ρy(s⊤y)=0(I - \rho y s^\top)y = y - \rho y(s^\top y) = 0, so the first term vanishes on yy, and ρss⊤y=s\rho s s^\top y = s. The update keeps HH positive definite exactly when yk⊤sk>0y_k^\top s_k \gt 0. If ρ>0\rho \gt 0 and v≠0v \ne 0, put u=(I−ρys⊤)vu = (I - \rho y s^\top)v; then v⊤Hk+1v=u⊤Hku+ρ (s⊤v)2≥0v^\top H_{k+1} v = u^\top H_k u + \rho\,(s^\top v)^2 \ge 0. Both terms vanish only if s⊤v=0s^\top v = 0 and u=0u = 0, but s⊤v=0s^\top v = 0 makes u=v≠0u = v \ne 0, so the sum is positive. If y⊤s≤0y^\top s \le 0, no positive definite matrix can satisfy the secant equation, because y⊤Hy=y⊤sy^\top H y = y^\top s would have to be non-positive. For a μ\mu-strongly convex function, adding its lower parabolas at xkx_k towards xk+1x_{k+1} and at xk+1x_{k+1} towards xkx_k gives yk⊤sk≥μ∥sk∥2>0y_k^\top s_k \ge \mu\lVert s_k\rVert^2 \gt 0. In general the Wolfe curvature condition guarantees it: with sk=t dks_k = t\,d_k, yk⊤sk≥t (c2−1) gk⊤dk>0y_k^\top s_k \ge t\,(c_2 - 1)\, g_k^\top d_k \gt 0, because c2<1c_2 \lt 1 and gk⊤dk<0g_k^\top d_k \lt 0.

Each BFGS step costs O(n2)O(n^2), a few matrix–vector products and outer products. For a strongly convex ff with Lipschitz Hessian, BFGS with backtracking converges from any start and, near the minimum, superlinearly: the ratio ∥ek+1∥/∥ek∥\lVert e_{k+1}\rVert/\lVert e_k\rVert tends to 0, though not as fast as Newton’s square (Vandenberghe, 2022, the convergence result, stated there without proof). L-BFGS stores only the last few pairs (s,y)(s, y), typically 5 to 20, and applies the update implicitly, at O(mn)O(mn) per step for mm pairs. It is the default solver of scikit-learn’s LogisticRegression (solver='lbfgs' in version 1.9.1) and a standard choice for smooth deterministic problems of moderate size.

Gradient descent against Newton#

Figure

Gradient descent against Newton’s method

The zigzag of gradient descent, the Newton step, and convergence speedsLeft, elongated elliptical contours; a blue zigzag approaches the centre, a red arrow reaches the centre in one segment. Right, three error curves against the iteration number: the blue one descends slowly over thousands of steps, the green one faster, the red one drops off a cliff within five steps.κ = 10: descent zigzags, Newton jumpsdescent: 35 stepsNewton: 1 stepx₀the opening logistic loss110⁻⁴10⁻⁸10⁻¹²10⁻¹⁶1101001000iteration k (log scale)f − f*gradient descent, η = 1/L: 2,727with Armijo backtracking: 526Newton: 5The zigzag of gradient descent, the Newton step, and convergence speedsLeft, elongated elliptical contours; a blue zigzag approaches the centre, a red arrow reaches the centre in one segment. Right, three error curves against the iteration number: the blue one descends slowly over thousands of steps, the green one faster, the red one drops off a cliff within five steps.κ = 10: descent zigzags, Newton jumpsdescent: 35 stepsNewton: 1 stepx₀the opening logistic loss110⁻⁴10⁻⁸10⁻¹²10⁻¹⁶1101001000iteration k (log scale)f − f*gradient descent, η = 1/L: 2,727with Armijo backtracking: 526Newton: 5
Left, a quadratic with Hessian eigenvalues 1 and 10, axes turned by 30°. Gradient descent with exact line search (blue) zigzags and multiplies f − f* by 0.669 per step; the Newton step (red arrow) lands on the minimum. Right, f − f* for the opening logistic loss, both axes logarithmic; the legend gives the steps until f − f* ≤ 10⁻¹⁰.

The left panel is the setting of the gradient descent note without its luck: an elongated quadratic with μ=1\mu = 1 and L=10L = 10 whose axes are not the coordinate axes. Even with the best step along the gradient, found by an exact line search, gradient descent zigzags, because each step ends where the new gradient is perpendicular to the old one: the step tt minimizes φ(t)=f(x−tg)\varphi(t) = f(x - tg), and φ′(t)=−∇f(x−tg)⊤g=0\varphi'(t) = -\nabla f(x - tg)^\top g = 0 there. The start is the worst case for which Boyd and Vandenberghe give the iterates in closed form (lecture 9, the quadratic example: f=12(x12+γx22)f = \tfrac12(x_1^2 + \gamma x_2^2) from (γ,1)(\gamma, 1), here with γ=10\gamma = 10 and the axes turned), and ff falls by the factor ((κ−1)/(κ+1))2=0.669\big((\kappa - 1)/(\kappa + 1)\big)^2 = 0.669 per step. A millionfold reduction takes 35 steps, 66 with the fixed step 1/L1/L; the guarantee (1−1/κ)k(1 - 1/\kappa)^k promises 132. Newton’s method takes one step.

The right panel is the opening logistic problem, where no method is exact in one step. Newton needs 5 steps to bring f−f∗f - f^* to about 10−1510^{-15}, BFGS with Armijo backtracking 11 steps to reach 10−1010^{-10}, gradient descent with backtracking 526 and with the fixed step 1/L1/L 2,727. Per step, Newton pays an O(n3)O(n^3) solve, BFGS O(n2)O(n^2) and gradient descent O(n)O(n) beyond the gradient itself; at n=2n = 2 the solve is free, and at n=108n = 10^8 it is impossible.

Where it breaks#

Newton’s method looks for a zero of the gradient and does not care which kind. Take the non-convex function f(x)=x4−2x2+12xf(x) = x^4 - 2x^2 + \tfrac12x of the previous note and start at x=0.3x = 0.3, where f′=−0.592f' = -0.592 and f′′=−2.92f'' = -2.92. The function decreases to the right, but with negative curvature the Newton step −f′/f′′-f'/f'' points left, uphill:

kk0123
xkx_k0.30.30.09730.09730.126760.126760.127050.12705
f(xk)f(x_k)−0.022-0.0220.0300.0300.0320.0320.0320.032

Pure Newton converges, quadratically, to the local maximum at 0.1270.127: as a root finder for f′f', the convergence proof above needs only ∣f′′∣\lvert f''\rvert bounded away from 0 near the root, not its sign. Gradient descent with step 0.050.05 from the same point goes downhill to the local minimum at 0.9300.930. Practical codes guard against this by adding a multiple of the identity to the Hessian until it is positive definite, or by trusting the quadratic model only within a region around the current point. BFGS avoids it by construction: its HkH_k stays positive definite, so −Hkgk-H_k g_k is always a descent direction, and the line search makes ff decrease at every step, which rules out climbing to a maximum.

The analysis has other limits. Every rate here assumes exact gradients; with minibatch gradients the noise changes the picture, as the stochastic optimizers note shows, and BFGS pairs built from noisy gradients become unreliable. Losses with kinks, such as the hinge or the L1 penalty, break the descent lemma and need subgradient or proximal methods.

What you should remember#

  • At a local minimum ∇f=0\nabla f = 0 and ∇2f⪰0\nabla^2 f \succeq 0; for convex ff, ∇f=0\nabla f = 0 suffices.
  • Descent lemma: with η=1/L\eta = 1/L, each step lowers ff by at least ∥∇f∥2/(2L)\lVert\nabla f\rVert^2/(2L).
  • Convex: f(xk)−f∗≤L∥x0−x∗∥2/(2k)f(x_k) - f^* \le L\lVert x_0 - x^*\rVert^2/(2k). Strongly convex: about κln⁡(1/ε)\kappa\ln(1/\varepsilon) steps.
  • Armijo backtracking finds a step with guaranteed decrease without knowing LL.
  • Newton solves ∇2f d=−∇f\nabla^2 f\,d = -\nabla f: affine invariant, quadratic near a minimum, damped far from it.
  • BFGS builds the inverse Hessian from gradient differences through Hk+1yk=skH_{k+1} y_k = s_k.

Code#

Newton’s method on x−ln⁡xx - \ln x with and without damping, gradient descent and Newton on the elongated quadratic, four methods on the logistic loss of the opening with the guarantees checked, and Newton on a non-convex function. Exact digits can differ slightly across platforms and library versions.

import numpy as np
from scipy.optimize import minimize

# Newton's method on f(x) = x - log x, minimum at 1: x <- 2x - x^2, and the error is squared at every step
x = 0.5
for k in range(6):
    print(k, f'{x - 1:.3e}', end=' | ')
    x = 2 * x - x * x
print()
# 0 -5.000e-01 | 1 -2.500e-01 | 2 -6.250e-02 | 3 -3.906e-03 | 4 -1.526e-05 | 5 -2.328e-10

# from x = 3 the full step lands at -3, outside the domain; backtracking (Armijo, c = 1e-4) halves it twice
f = lambda x: x - np.log(x) if x > 0 else np.inf
x = 3.0
for k in range(7):
    g, h = 1 - 1 / x, 1 / x**2
    d, t = -g / h, 1.0
    while f(x + t * d) > f(x) + 1e-4 * t * g * d:
        t /= 2
    print(k, round(x, 6), t, end=' | ')
    x += t * d
print()
# 0 3.0 0.25 | 1 1.5 1.0 | 2 0.75 1.0 | 3 0.9375 1.0 | 4 0.996094 1.0 | 5 0.999985 1.0 | 6 1.0 1.0

# an elongated quadratic: eigenvalues mu = 1 and L = 10, axes turned by 30 degrees, minimum at 0
th = np.deg2rad(30); Rot = np.array([[np.cos(th), -np.sin(th)], [np.sin(th), np.cos(th)]])
mu, L = 1.0, 10.0
A = Rot @ np.diag([mu, L]) @ Rot.T
q = lambda x: 0.5 * x @ A @ x
x0 = Rot @ np.array([2.0, 0.2])
def count(step):
    x, k = x0.copy(), 0
    while q(x) > 1e-6 * q(x0):
        g = A @ x
        x, k = x - step(g) * g, k + 1
    return k
print(count(lambda g: 1 / L), count(lambda g: 2 / (L + mu)), count(lambda g: g @ g / (g @ A @ g)),
      ((L - mu) / (L + mu)) ** 2, np.abs(x0 - np.linalg.solve(A, A @ x0)).max(), np.ceil(np.log(1e6) / -np.log(1 - mu / L)))
# 66 35 35 0.6694214876033059 2.220446049250313e-16 132.0
# steps with 1/L, with 2/(L + mu) and with exact line search; the factor per step of exact line search;
# Newton lands on the minimum in one step; the steps the strongly convex guarantee asks for

# a logistic loss with an L2 penalty on 40 deterministic points; the second feature is ten times larger
n, lam = 40, 0.01
k_ = np.arange(1, n + 1)
X = np.column_stack([2 * ((k_ * 0.6180339887498949) % 1) - 1, 10 * (2 * ((k_ * 0.7548776662466927) % 1) - 1)])
noise = (k_ * 0.4142135623730951) % 1 - 0.5
y = np.where(X[:, 0] + 0.1 * X[:, 1] + noise > 0, 1.0, -1.0)
F = lambda w: np.mean(np.maximum(-y * (X @ w), 0) + np.log1p(np.exp(-np.abs(y * (X @ w))))) + lam / 2 * w @ w
G = lambda w: -X.T @ (y / (1 + np.exp(y * (X @ w)))) / n + lam * w
def Hess(w):
    p = 1 / (1 + np.exp(-(X @ w)))
    return (X.T * (p * (1 - p))) @ X / n + lam * np.eye(2)
Lb = np.linalg.eigvalsh(X.T @ X)[-1] / (4 * n) + lam          # a global bound on the Hessian
w = np.zeros(2)
for _ in range(30):
    w = w - np.linalg.solve(Hess(w), G(w))
ws, Fs = w, F(w)
print(ws.round(4), round(Fs, 5), round(Lb, 3), round(Lb / lam), np.linalg.eigvalsh(Hess(ws)).round(4), round(np.linalg.cond(Hess(ws))))
# [3.4682 0.3803] 0.30901 7.898 790 [0.0293 3.2176] 110

def run(step, tol=1e-10):
    w, k, gaps = np.zeros(2), 0, [F(np.zeros(2)) - Fs]
    while gaps[-1] > tol:
        w, k = step(w), k + 1
        gaps.append(F(w) - Fs)
    return k, gaps
def armijo(w):
    g, t = G(w), 1.0
    while F(w - t * g) > F(w) - 1e-4 * t * g @ g:
        t /= 2
    return w - t * g
k_gd, gaps = run(lambda w: w - G(w) / Lb)
k_ar, _ = run(armijo)
k_nt, gn = run(lambda w: w - np.linalg.solve(Hess(w), G(w)))
print(k_gd, k_ar, k_nt, [f'{g:.1e}' for g in gn], [f'{gaps[i]:.1e}' for i in (10, 100, 1000)])
# 2727 526 5 ['3.8e-01', '5.3e-02', '5.2e-03', '7.7e-05', '2.0e-08', '1.3e-15'] ['2.3e-01', '6.9e-02', '3.8e-05']

# the guarantees: f_k - f* <= (1 - mu/L)^k (f_0 - f*) and f_k - f* <= L |w_0 - w*|^2 / (2k), both hold
print(all(gaps[k] <= (1 - lam / Lb) ** k * gaps[0] + 1e-15 for k in range(len(gaps))),
      all(gaps[k] <= Lb * ws @ ws / (2 * k) for k in range(1, len(gaps))),
      round(np.log(gaps[0] / 1e-10) * Lb / lam))
# True True 17431: the steps the strongly convex bound asks for, against 2727 taken

# BFGS: the inverse-Hessian approximation Hinv is updated from s = w+ - w and y = g+ - g, and satisfies Hinv y = s
def bfgs(w, tol=1e-10):
    Hinv, k = np.eye(2), 0
    while F(w) - Fs > tol:
        g = G(w); d = -Hinv @ g; t = 1.0
        while F(w + t * d) > F(w) + 1e-4 * t * g @ d:
            t /= 2
        s = t * d; yv = G(w + s) - g; rho = 1 / (yv @ s)
        V = np.eye(2) - rho * np.outer(s, yv)
        Hinv = V @ Hinv @ V.T + rho * np.outer(s, s)
        w, k = w + s, k + 1
    return k, Hinv @ yv - s
k_bf, secant = bfgs(np.zeros(2))
r = minimize(F, np.zeros(2), jac=G, method='BFGS', options={'gtol': 1e-9})
print(k_bf, np.abs(secant).max() < 1e-12, r.nit, f'{r.fun - Fs:.0e}')
# 11 True 14 0e+00: our BFGS, the secant equation after the last update, SciPy's BFGS (Wolfe line search)

# where it breaks: pure Newton on x^4 - 2x^2 + x/2 from 0.3 climbs to the local maximum; gradient descent does not
fn = lambda x: x**4 - 2 * x**2 + 0.5 * x
d1, d2 = lambda x: 4 * x**3 - 4 * x + 0.5, lambda x: 12 * x**2 - 4
x = 0.3
for k in range(4):
    print(k, round(x, 5), round(fn(x), 3), end=' | ')
    x -= d1(x) / d2(x)
x_gd = 0.3
for _ in range(200):
    x_gd -= 0.05 * d1(x_gd)
print(round(x_gd, 3), round(fn(x_gd), 3))
# 0 0.3 -0.022 | 1 0.09726 0.03 | 2 0.12676 0.032 | 3 0.12705 0.032 | 0.93 -0.517

Next#

Every problem so far was free: any x∈Rnx \in \mathbb R^n was allowed. Many problems in machine learning are not. Probabilities must stay on the simplex, the SVM requires every point to be on the correct side of its margin, and PCA looks for the best direction among unit vectors. The next note handles constraints: at a constrained minimum the gradient need not vanish, only be balanced by the constraints, and the Lagrange multipliers and KKT conditions say exactly how.

References#