SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 4.4.1 Optimality conditions in several variablesConcept · 30 min
      2. 4.4.2 Gradient descentConcept · 30 min
      3. 4.4.3 Newton's method in several variablesConcept · 30 min
      4. 4.4.4 Practice set: unconstrained optimisationPractice set · 25 min
      5. 4.4.5 Case study: fitting a curve by least squaresCase study · 35 min

Level 4, Multivariable calculus and optimisation 4.4 Unconstrained optimisation Lesson 2 of 5

ConceptLesson 4.4.2 in Level 4, Multivariable calculus and optimisation. About 30 minutes.

Gradient descent

Walking downhill

Lesson 4.4.1 found minima by solving ∇f=0\nabla f = \mathbf{0}. Usually that equation has no formula, so the computer walks downhill instead. Lesson 4.2.1 showed that −∇f-\nabla f is the direction in which ff falls fastest.

Rule: Gradient descent

xk+1=xk−η ∇f(xk)\mathbf{x}_{k+1} = \mathbf{x}_k - \eta\,\nabla f(\mathbf{x}_k)

Start at a point x0\mathbf{x}_0 and repeat the step. The step size η\eta (eta), a positive number, sets how far each step goes; in machine learning it is called the learning rate. At a critical point the gradient is 0\mathbf{0}, so the steps stop there.

η\eta has the units that make η∇f\eta\nabla f a distance in the units of x\mathbf{x}. Doubling η\eta doubles every step.

Take f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x of lesson 4.4.1, with minimum (2,−1)(2, -1) and gradient (2x+y−3, x+2y)(2x + y - 3,\ x + 2y). From (0,1)(0, 1) the gradient is (−2,2)(-2, 2). With η=0.3\eta = 0.3 the first step goes to (0,1)−0.3(−2,2)=(0.6,0.4)(0, 1) - 0.3(-2, 2) = (0.6, 0.4), where f=−1.04f = -1.04.

step kkxk\mathbf{x}_kf(xk)f(\mathbf{x}_k)distance to (2,−1)(2, -1)
0(0,1)(0, 1)12.8284
1(0.6,0.4)(0.6, 0.4)−1.04-1.041.9799
2(1.02,−0.02)(1.02, -0.02)−2.0396-2.03961.3859
3(1.314,−0.314)(1.314, -0.314)−2.5294-2.52940.9702
4(1.5198,−0.5198)(1.5198, -0.5198)−2.7694-2.76940.6791
5(1.6639,−0.6639)(1.6639, -0.6639)−2.887-2.8870.4754

ff falls at every step, towards −3-3. Each distance is 0.7 times the one before; the next section explains that number. After 42 steps, ∥∇f∥\lVert\nabla f\rVert is below 10−610^{-6}.

Try it

The loop prints each step, ff and the distance to (2,−1)(2, -1), then takes the step.

descend repeats the step until ∥∇f∥<10−6\lVert\nabla f\rVert < 10^{-6} and returns the point and the number of steps.

⌘/Ctrl + Enter runs the cell

How large a step?

One variable

Take f(x)=2x2f(x) = 2x^2, with its minimum at 0 and slope f′(x)=4xf'(x) = 4x. One step gives x−η⋅4x=(1−4η)xx - \eta \cdot 4x = (1 - 4\eta)x: each step multiplies xx by the same factor 1−4η1 - 4\eta. From x0=1x_0 = 1:

η\etafactor 1−4η1 - 4\etax1x_1x2x_2x3x_3x4x_4
0.10.60.60.360.2160.1296
0.2500000
0.45−0.8-0.8−0.8-0.80.64−0.512-0.5120.4096
0.55−1.2-1.2−1.2-1.21.44−1.728-1.7282.0736

A small step creeps towards 0 from one side. At 0.25 one step lands on the minimum.

At 0.45 every step overshoots to the other side but still gets closer. At 0.55 each overshoot is larger than the last: the method diverges.

The steps close in on 0 exactly when the factor lies between −1-1 and 1, that is, when 0<η<0.50 < \eta < 0.5; past η=0.25\eta = 0.25 they overshoot. The 4 in 1−4η1 - 4\eta is f′′f'', the curvature (lesson 2.4.2). So a curve that bends more allows only a smaller step: the limit is η<2/f′′\eta < 2/f''.

Several variables: one factor per eigenvector

For a quadratic f(x)=12x⊤Ax−b⊤xf(\mathbf{x}) = \frac{1}{2}\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x} with minimiser x∗\mathbf{x}^*, lesson 4.4.1 gave ∇f=Ax−b=A(x−x∗)\nabla f = A\mathbf{x} - \mathbf{b} = A(\mathbf{x} - \mathbf{x}^*). Write ek=xk−x∗\mathbf{e}_k = \mathbf{x}_k - \mathbf{x}^* for the error. Then one step is

ek+1=ek−ηAek=(I−ηA) ek\mathbf{e}_{k+1} = \mathbf{e}_k - \eta A\mathbf{e}_k = (I - \eta A)\,\mathbf{e}_k

Along a unit eigenvector qi\mathbf{q}_i of AA, with eigenvalue λi\lambda_i, (I−ηA)qi=(1−ηλi)qi(I - \eta A)\mathbf{q}_i = (1 - \eta\lambda_i)\mathbf{q}_i (lesson 3.6.1). So the part of the error along each eigenvector is multiplied by its own factor 1−ηλi1 - \eta\lambda_i at every step, as in one variable.

Rule: Step size for a quadratic

factor along qi: 1−ηλiconverges from every start  ⟺  0<η<2λmax⁡\begin{gathered} \text{factor along } \mathbf{q}_i: \ 1 - \eta\lambda_i \\[4pt] \text{converges from every start} \\ \iff 0 < \eta < \frac{2}{\lambda_{\max}} \end{gathered}

Here AA is positive definite and λmax⁡\lambda_{\max} is its largest eigenvalue. The factor furthest from 0 in size sets the speed. Near the minimum of any smooth ff, the Hessian there plays the part of AA.

For x2+xy+y2−3xx^2 + xy + y^2 - 3x, AA has eigenvalue 3 along (1,1)(1, 1) and 1 along (1,−1)(1, -1) (lesson 3.6.3). So the limit is η<2/3\eta < 2/3. From (0,1)(0, 1) the error (−2,2)(-2, 2) lies along (1,−1)(1, -1), which is why it shrank by exactly 1−0.3×1=0.71 - 0.3 \times 1 = 0.7 per step.

From (0,0)(0, 0) the error (−2,1)(-2, 1) has a part along each eigenvector. Steps until ∥∇f∥<10−6\lVert\nabla f\rVert < 10^{-6}:

η\etafactor along (1,1)(1, 1)factor along (1,−1)(1, -1)steps
0.10.70.9139
0.30.10.741
0.5−0.5-0.50.522
0.6−0.8-0.80.466
0.66−0.98-0.980.34722
0.7−1.1-1.10.3never: diverges

Too small is slow, because the factor along (1,−1)(1, -1) is close to 1. Too large is slow too, because the factor along (1,1)(1, 1) is close to −1-1, and past 2/32/3 it diverges. The best fixed step, η=2/(λmax⁡+λmin⁡)=0.5\eta = 2/(\lambda_{\max} + \lambda_{\min}) = 0.5, makes the two factors the same size.

Gradient descent with two step sizes on the same bowl, and in a narrow valley. A step too large for the steep direction makes the path bounce across it. In the narrow valley the bouncing goes on while the path creeps along the floor.

At η=0.7\eta = 0.7 the part of the error along (1,1)(1, 1) goes −0.7071-0.7071, 0.7778, −0.8556-0.8556, 0.9412: it grows by 1.1 each step and flips sign. ff first falls to −1.89-1.89, then climbs, reaching −0.1519-0.1519 after seven steps, and goes on rising.

Try it

descend is the loop of the first cell. The first loop is the table: steps from (0,0)(0, 0) for each step size, or None when the path runs off.

The last loop follows η=0.7\eta = 0.7: the step, ff, and the part of the error along (1,1)/2(1, 1)/\sqrt{2}.

⌘/Ctrl + Enter runs the cell

Long narrow valleys

The ratio κ=λmax⁡/λmin⁡\kappa = \lambda_{\max}/\lambda_{\min} of a positive definite AA is its condition number (lesson 3.3.2). It measures how stretched the bowl is: the level curves are ellipses whose axes have lengths in the ratio κ\sqrt{\kappa} (lesson 3.7.1). With the best fixed step, the slowest factor is

κ−1κ+1\frac{\kappa - 1}{\kappa + 1}

which is 0.5 for κ=3\kappa = 3, 0.8182 for κ=10\kappa = 10, and close to 1 for a large κ\kappa.

Take f(x,y)=x2+10y2f(x, y) = x^2 + 10y^2, with Hessian (20020)\begin{pmatrix} 2 & 0 \\ 0 & 20 \end{pmatrix} and κ=10\kappa = 10. The limit is η<2/20=0.1\eta < 2/20 = 0.1. From (−4,1)(-4, 1):

η\etafactor for xxfactor for yysteps
0.010.980.8787
0.050.90151
0.090.82−0.8-0.881
0.0950.81−0.9-0.9160
0.10.8−1-1never: yy bounces forever
0.1050.79−1.1-1.1never: diverges

At η=0.09\eta = 0.09, yy flips sign at every step, 1, −0.8-0.8, 0.64, −0.512-0.512, while xx creeps from −4-4 to −3.28-3.28, −2.6896-2.6896, −2.2055-2.2055. The path zig-zags across the valley, as in the figure. The steep direction caps the step size, and the gentle direction pays for it.

For the round bowl x2+y2x^2 + y^2, with κ=1\kappa = 1, the step η=0.5\eta = 0.5 reaches the minimum from any start in one step. Rescaling yy by 10\sqrt{10} would turn x2+10y2x^2 + 10y^2 into that bowl. Lesson 4.4.3's Newton's method does such a rescaling automatically.

A curved valley

The function f(x,y)=(1−x)2+5(y−x2)2f(x, y) = (1 - x)^2 + 5(y - x^2)^2 has a long valley along the curve y=x2y = x^2, with its minimum at (1,1)(1, 1). There the Hessian is (42−20−2010)\begin{pmatrix} 42 & -20 \\ -20 & 10 \end{pmatrix}, with eigenvalues 0.3875 and 51.6125, so κ≈133\kappa \approx 133. Near the minimum, a step must stay below 2/51.6125=0.03882/51.6125 = 0.0388.

From (−1.5,2.5)(-1.5, 2.5) with η=0.02\eta = 0.02, 1688 steps bring ∥∇f∥\lVert\nabla f\rVert below 10−610^{-6}. After 50 steps the point is still at (−0.8817,0.9382)(-0.8817, 0.9382), crawling round the bend.

With η=0.04\eta = 0.04, just above the limit, the path gets near (1,1)(1, 1) quickly but never settles. It ends up jumping between two points, 0.0539 and 0.055 from the minimum. With η=0.05\eta = 0.05 the steps blow up within six steps.

The explorer starts on this valley at (−1.5,2.5)(-1.5, 2.5) with step size 0.02. Set 50 steps and watch the path zig-zag across the curved floor as it creeps along. Then try 0.04, and 0.05, when the steps grow without limit.

Pick the tilted bowl x2+xy+y2−3xx^2 + xy + y^2 - 3x, move the start to (0,0)(0, 0), and compare 0.3 with 0.6. The Newton's method switch is for lesson 4.4.3.

Explore

Each step moves against the gradient, a distance set by the step size. Try a small and a large step size, then switch to Newton's method, which uses the Hessian too.

−2−1012−10123
Point
(−1.5, 2.5), f = 6.5625
Gradient descent, step size 0.02
after 5 steps: (−1.508, 2.3287), f = 6.305

Try it

The first lines are the valley's gradient and its Hessian at (1,1)(1, 1), with the eigenvalues, κ\kappa and the limit 2/λmax⁡2/\lambda_{\max}.

Then 50 steps with η=0.02\eta = 0.02, and with η=0.04\eta = 0.04 the two points the path ends up jumping between, after 10,000 and 10,001 steps.

⌘/Ctrl + Enter runs the cell

When to stop

The steps rarely land exactly on the minimum, so the code needs a rule for when to stop. Three tests are common, and each can mislead.

Rule: Stopping rules

∥∇f(xk)∥<tolor ∥xk+1−xk∥<toland always a cap on the steps\begin{gathered} \lVert\nabla f(\mathbf{x}_k)\rVert < \text{tol} \\[4pt] \text{or } \lVert\mathbf{x}_{k+1} - \mathbf{x}_k\rVert < \text{tol} \\[4pt] \text{and always a cap on the steps} \end{gathered}

A small gradient means the ground is nearly flat; a small step means the point has stopped moving. A third test stops when ff stops falling. Whatever the test, cap the number of steps and report when the cap is hit.

A small gradient in a flat valley. For a quadratic, the distance to the minimum is at most ∥∇f∥/λmin⁡\lVert\nabla f\rVert/\lambda_{\min}. Take f=x2+0.01y2f = x^2 + 0.01y^2 at (0,1)(0, 1): ∇f=(0,0.02)\nabla f = (0, 0.02), a small gradient, yet the minimum is 1 away. Scale the tolerance to the problem.

A small step with a small η\eta. Each step has length η∥∇f∥\eta\lVert\nabla f\rVert. With η=10−4\eta = 10^{-4} on the bowl x2+xy+y2−3xx^2 + xy + y^2 - 3x, the first step from (0,1)(0, 1) moves only 0.000283, far from the minimum. A step test then stops at once.

Backtracking: a step size without λmax⁡\lambda_{\max}

In practice λmax⁡\lambda_{\max} is unknown and changes from point to point. Backtracking chooses η\eta afresh at each step, by trial.

The tangent plane (lesson 4.2.2) says a step −η∇f-\eta\nabla f lowers ff by about η∥∇f∥2\eta\lVert\nabla f\rVert^2. Accept the step if ff really falls by at least half of that; otherwise halve η\eta and try again.

Rule: Backtracking

start at η=1; halve η untilf(x−η∇f)≤f(x)−12 η ∥∇f∥2\begin{gathered} \text{start at } \eta = 1; \text{ halve } \eta \text{ until} \\[4pt] f(\mathbf{x} - \eta\nabla f) \le f(\mathbf{x}) - \tfrac{1}{2}\,\eta\,\lVert\nabla f\rVert^2 \end{gathered}

Then take the step with that η\eta. A step that overshoots fails the test, so ff falls at every accepted step. Each trial costs one evaluation of ff.

On the curved valley from (−1.5,2.5)(-1.5, 2.5), backtracking needs 442 steps, against 1688 with the fixed η=0.02\eta = 0.02. The first accepted step size is 1128\frac{1}{128}, and later ones are often 132\frac{1}{32} or 164\frac{1}{64}. Its 2017 halvings in all each cost one evaluation of ff, so the saving is real but smaller than the step counts suggest.

Try it

backtrack takes gradient steps with the rule above. It returns the point, the number of steps and the number of halvings.

The two lines are the curved valley from (−1.5,2.5)(-1.5, 2.5) and x2+10y2x^2 + 10y^2 from (−4,1)(-4, 1).

⌘/Ctrl + Enter runs the cell

Lesson 4.4.3 replaces the fixed direction −∇f-\nabla f with Newton's step, which bends towards the minimum in a narrow valley. Practice set 4.4.4 asks you to write the backtracking method.

Common mistakes

  1. Too large a step diverges. Past 2/λmax⁡2/\lambda_{\max} the error grows each step; watch that ff falls.
  2. Too small a step only looks safe. The path creeps, and a step-length test may stop it far from the minimum.
  3. The limit comes from the steepest direction. It is 2/λmax⁡2/\lambda_{\max}, set by the largest eigenvalue, not by an average curvature.
  4. A small gradient is not a small error in a flat valley: x2+0.01y2x^2 + 0.01y^2 has ∥∇f∥=0.02\lVert\nabla f\rVert = 0.02 at (0,1)(0, 1), a distance of 1 from the minimum.
  5. A zig-zag is bad scaling, not a bug. It comes from a large condition number; rescale the variables or use Newton's method.
  6. Gradient descent stops at any critical point it reaches, including a local minimum that is not global (lesson 4.4.1).
  7. Always cap the number of steps, and report when the cap is reached.

Gradient descent

0 of 11 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
How to type answers
  • Powers with ^: x^7, 2^(1/3), a^-2.
  • Multiply with * or by writing side by side: 3x^2, 2(x+1).
  • ln(x) is the natural log, log(x, 2) is log base 2, e is Euler's number, sqrt(x) is a square root.
  • sin(x), cos(x) and tan(x) take radians; write pi for π and sin(x)^2 for sin²x.
  • Your first answer to each question is saved: it counts towards your score and brings the question back for review.
  • Exact answers like ln(20)/ln(3) are best. Decimals need about 4 significant figures.

Summary card

  • Gradient descent: xk+1=xk−η∇f(xk)\mathbf{x}_{k+1} = \mathbf{x}_k - \eta\nabla f(\mathbf{x}_k), a step against the gradient of size set by η>0\eta > 0.
  • In one variable each step multiplies the error by about 1−ηf′′1 - \eta f'': it converges when 0<η<2/f′′0 < \eta < 2/f'', overshooting when η>1/f′′\eta > 1/f''.
  • For a quadratic with positive definite AA, the error along each eigenvector is multiplied by 1−ηλi1 - \eta\lambda_i; it converges from every start exactly when η<2/λmax⁡\eta < 2/\lambda_{\max}.
  • A large condition number κ=λmax⁡/λmin⁡\kappa = \lambda_{\max}/\lambda_{\min} means a narrow valley: the best factor is κ−1κ+1\frac{\kappa - 1}{\kappa + 1}, and the path zig-zags.
  • Stop on a small gradient or a small step, scaled to the problem, and always cap the steps. A small gradient can still be far from the minimum.
  • Backtracking halves η\eta from 1 until f(x−η∇f)≤f(x)−12η∥∇f∥2f(\mathbf{x} - \eta\nabla f) \le f(\mathbf{x}) - \frac{1}{2}\eta\lVert\nabla f\rVert^2, so ff falls at every step without knowing λmax⁡\lambda_{\max}.

Free further reading

Optional. This lesson covers what you need; these add more examples and practice. All are free to read.