Numerical Methods

All books

A step has a direction and a length

What this page answers

The class page says optimisation is one score over a set of choices, and that a gradient points at the steepest way up. It does not say how far to walk. This page separates the two decisions, shows why the gradient alone cannot answer the second, and derives the window a fixed step has to sit in.

It settles the line-search row of the measured gaps: a reader can say why a step length is a separate question, and can compute the window for a quadratic.

The mathematics

Write the score as a quadratic in nn variables,

f(x)=12xTAx−bTx,f(x) = \tfrac12 x^{\mathsf T} A x - b^{\mathsf T} x,

with AA symmetric and positive definite, so the minimum is where Ax=bA x = b. Gradient descent takes

xk+1=xk−t ∇f(xk),∇f(x)=Ax−b.x_{k+1} = x_k - t\,\nabla f(x_k), \qquad \nabla f(x) = A x - b.

Let ek=xk−x∗e_k = x_k - x^{*} be the error, where x∗x^{*} solves Ax=bA x = b. Subtracting the fixed point from the step gives the error's own rule:

ek+1=ek−tAek=(I−tA) ek.e_{k+1} = e_k - t A e_k = (I - tA)\, e_k.

That line is the whole analysis: the error is multiplied by the same matrix every step, so its behaviour is decided by the eigenvalues of I−tAI - tA. If AA has eigenvalues λi\lambda_i, the error along eigenvector ii is multiplied by 1−tλi1 - t\lambda_i each step. It decays only if

∣1−tλi∣<1for everyi,|1 - t\lambda_i| \lt 1 \quad\text{for every} i,

which is the window

0<t<2λmax⁡.0 \lt t \lt \frac{2}{\lambda_{\max}}.

So the largest curvature sets an upper limit, and the smallest sets the speed: the slowest mode decays by the factor 1−tλmin⁡1 - t\lambda_{\min} per step, so reaching a relative error ε\varepsilon needs about

k≈log⁡(1/ε)t λmin⁡k \approx \frac{\log(1/\varepsilon)}{t\,\lambda_{\min}}

steps. The ratio λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min} is the condition number of AA, the same number the linear-system pages use. A well-conditioned quadratic converges quickly at the best step; a badly conditioned one crawls, or oscillates, because the step that suits the largest curvature is far too short for the smallest.

The step length, chosen rather than fixed. Along the direction g=∇f(x)g = \nabla f(x), the best distance is the tt that minimises f(x−tg)f(x - tg). For a quadratic that is a one-line calculation: differentiating f(x−tg)f(x - tg) in tt and setting it to zero gives

t=gTggTAg.t = \frac{g^{\mathsf T} g}{g^{\mathsf T} A g}.

With that tt, one step lands on the minimum of ff along gg. It does not find the minimum of ff, because the next gradient points somewhere new — but it removes the only question the gradient could not answer.

The check. Take A=diag⁡(10,2)A = \operatorname{diag}(10, 2), so λmax⁡=10\lambda_{\max} = 10 and the window is 0<t<0.20 \lt t \lt 0.2, and start at (1,1)(1,1). At t=0.19t = 0.19 both modes decay and the run reaches f<10−5f \lt 10^{-5}. At t=0.25t = 0.25 the first mode is multiplied by 1−0.25×10=−1.51 - 0.25 \times 10 = -1.5 every step, so the run explodes. At t=0.05t = 0.05 it converges, but the slow mode decays by 0.90.9 per step, which is the slow crawl. The exact step reaches the minimum in one iteration.

The same iteration drawn on its plane. The two unknowns are the two axes, so each step moves along both. The model is f=5x2+y2f = 5x^2 + y^2 and the step is xk+1=xk−t∇f(xk)x_{k+1} = x_k - t\nabla f(x_k).

Figure 1

One step length cannot be right for both directions. Say which coordinate the step of 0.05 leaves behind, which one the step of 0.19 leaves behind, and why the exact line search removes only half of that problem.

The code

The program runs four fixed steps and one exact line search on that quadratic and prints the final point and score. Sixty iterations is long enough to separate converged from diverged.

Four fixed steps and one exact line search
import sympy as sp

x, y = sp.symbols("x y")
f = 5 * x**2 + y**2
gx, gy = sp.diff(f, x), sp.diff(f, y)
start = (1.0, 1.0)


def fixed(step, n=60):
    px, py = start
    for _ in range(n):
        g = (float(gx.subs({x: px, y: py})),
             float(gy.subs({x: px, y: py})))
        px, py = px - step * g[0], py - step * g[1]
    return px, py


def exact(n=60):
    px, py = start
    for _ in range(n):
        a = float(gx.subs({x: px, y: py}))
        b = float(gy.subs({x: px, y: py}))
        step = (a * a + b * b) / (10 * a * a + 2 * b * b)
        px, py = px - step * a, py - step * b
    return px, py


print("step   x            y            f")
for s in (0.05, 0.19, 0.25, 0.45):
    px, py = fixed(s)
    print(f"{s:>4}  {px:>11.3e}  {py:>11.3e}  {5*px*px + py*py:>11.3e}")
px, py = exact()
print(f"exact {px:>11.3e}  {py:>11.3e}  {5*px*px + py*py:>11.3e}")
step   x            y            f
0.05    8.674e-19    1.797e-03    3.229e-06
0.19    1.797e-03    3.495e-13    1.615e-05
0.25    3.677e+10    8.674e-19    6.760e+21
0.45    4.406e+32    1.000e-60    9.708e+65
exact   5.458e-30    5.458e-30    1.787e-58

The first two rows are inside the window and the last two are outside it, which is the window's shape rather than a coincidence: 0.19<0.2<0.250.19 \lt 0.2 \lt 0.25. Read the two converged rows against each other and the second decision appears. At t=0.05t = 0.05 the yy component is still 1.8×10−31.8 \times 10^{-3} after sixty steps, because its curvature is 22 and it decays by 0.90.9 a step; the xx component, with curvature 1010, is already gone. At t=0.19t = 0.19 the order reverses: yy has vanished and xx is the one left over, oscillating as (−0.9)k(-0.9)^k. One step length cannot be right for both directions, and that is the whole reason a line search exists.

The exact row reaches 10−3010^{-30} and stops, which is the point at which the arithmetic runs out rather than the method.

The theory

The analysis assumed three things, and each one is where a real problem differs.

What the gradient cannot supply is curvature. It gives the direction of steepest increase at the point, and nothing about how that direction changes as you move along it. A method that uses only gradients is therefore choosing a length from information it does not have. There are two ways out. One is to measure the curvature, which is Newton's method and needs a Hessian and a solve. The other is to measure the score along the line, which is a line search. The trade is that Newton needs a Hessian and a linear solve per step, and a line search needs one or more extra score evaluations per step.

The account of line search and of the conditions a step must satisfy is Wright's, in Numerical Optimization; the convergence argument above is the quadratic case of the same theorem.

What it will not do: a line search makes a step safe, and does not make the method fast on a badly conditioned problem. It fixes the length, not the direction, and on an ill-conditioned quadratic the direction is still the slow part.

What this cost

One gradient per iteration, at O(n)O(n) for a score written as a formula, and the analysis above costs one O(n2)O(n^2) matrix that a real problem never forms. Memory is O(n)O(n). An exact line search costs one extra score evaluation per step for a quadratic, and several for a general ff, where the minimisation along the line is itself iterative.

The accuracy is limited by the condition number: the iteration count grows with λmax⁡/λmin⁡\lambda_{\max}/\lambda_{\min}, so a hundredfold worse conditioned problem costs about a hundredfold more steps. The step length is the cheapest thing to fix and the least valuable: it stops the run failing, and it does not stop it crawling.

What it will not do: it will not choose the direction. It will not work without a bound on the curvature. And it will not tell you that a small final gradient means a small error — on a flat valley a small gradient and a large error look the same.

Back to the map

Optimisation is the class page this works under. Convergence, and how to measure it is how the rate is measured when it is not a quadratic. Home is the summary.

Settings

Colour scheme

Light or dark pages. Your colour and layout stay the same.

Colour

The paper, ink and lettering. Your layout stays the same.

Quiet colours
Bold colours
Layout

How pages are arranged. Your colours stay the same.

Reading offline

Keep this book on this device and read it offline.