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 3 of 5

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

Newton's method in several variables

The Newton step

Gradient descent uses only the slope, so it cannot tell a narrow valley from a round bowl. Newton's method uses the curvature too. Near the current point xk\mathbf{x}_k, lesson 4.3.1 copies ff by a quadratic in the step h\mathbf{h}:

f(xk+h)≈f(xk)+∇f⋅h+12 h⊤H h\begin{gathered} f(\mathbf{x}_k + \mathbf{h}) \approx f(\mathbf{x}_k) + \nabla f \cdot \mathbf{h} + \tfrac{1}{2}\,\mathbf{h}^\top H\,\mathbf{h} \end{gathered}

with ∇f\nabla f and HH taken at xk\mathbf{x}_k. This is lesson 4.4.1's quadratic in h\mathbf{h}, with A=HA = H and b=−∇f\mathbf{b} = -\nabla f.

If HH is positive definite, the copy's minimum is where Hh=−∇fH\mathbf{h} = -\nabla f. Newton's method jumps to that minimum, then builds a new copy there.

Rule: Newton's method

solve H(xk) hk=−∇f(xk)xk+1=xk+hk\begin{gathered} \text{solve } H(\mathbf{x}_k)\,\mathbf{h}_k = -\nabla f(\mathbf{x}_k) \\[4pt] \mathbf{x}_{k+1} = \mathbf{x}_k + \mathbf{h}_k \end{gathered}

It is often written xk+1=xk−H−1∇f\mathbf{x}_{k+1} = \mathbf{x}_k - H^{-1}\nabla f. Compute it with np.linalg.solve (lesson 3.3.1): forming H−1H^{-1} costs more and adds rounding. The curvature sets the length of the step, so there is no η\eta to choose.

In one variable, HH is the number f′′f'' and ∇f\nabla f is f′f', so the step is h=−f′/f′′h = -f'/f''. That is lesson 2.4.3's Newton's method for a critical point.

A quadratic takes one step

For f=12x⊤Ax−b⊤xf = \frac{1}{2}\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x} the copy is exact, so one step lands on the minimum from any start. Take lesson 4.4.1's x2+xy+y2−3xx^2 + xy + y^2 - 3x, with H=(2112)H = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix}. At (0,1)(0, 1) the gradient is (−2,2)(-2, 2), so the step solves

2h1+h2=2h1+2h2=−2\begin{aligned} 2h_1 + h_2 &= 2 \\ h_1 + 2h_2 &= -2 \end{aligned}

which gives h=(2,−2)\mathbf{h} = (2, -2) and x1=(2,−1)\mathbf{x}_1 = (2, -1), the minimum. From (0,0)(0, 0) the step is (2,−1)(2, -1), and from (5,5)(5, 5) it is (−3,−6)(-3, -6): both land on (2,−1)(2, -1). Gradient descent needed 22 steps at its best step size (lesson 4.4.2).

Lesson 2.4.3's quartic, in two variables

Take f(x,y)=x4−3x2+x+y2f(x, y) = x^4 - 3x^2 + x + y^2 of lesson 4.4.1, with ∇f=(4x3−6x+1, 2y)\nabla f = (4x^3 - 6x + 1,\ 2y) and H=(12x2−6002)H = \begin{pmatrix} 12x^2 - 6 & 0 \\ 0 & 2 \end{pmatrix}. This HH is diagonal, so the Newton step splits into two one-variable steps.

For yy the step is −2y/2=−y-2y/2 = -y: yy lands on 0 at once, because y2y^2 is a quadratic. For xx it is lesson 2.4.3's step −f′(x)/f′′(x)-f'(x)/f''(x).

From (1,0.5)(1, 0.5) the xx values are 1.166667, 1.132616, 1.130905 and 1.130901, exactly those of lesson 2.4.3. The end is the local minimum (1.130901,0)(1.130901, 0).

Try it

newton_step solves Hh=−∇fH\mathbf{h} = -\nabla f with np.linalg.solve. The first loop takes one step on the quadratic from three starts; each lands on (2,−1)(2, -1).

The second loop takes four steps on the quartic from (1,0.5)(1, 0.5).

⌘/Ctrl + Enter runs the cell

How fast: the digits double

Lesson 4.4.2's curved valley, f(x,y)=(1−x)2+5(y−x2)2f(x, y) = (1 - x)^2 + 5(y - x^2)^2, has its minimum at (1,1)(1, 1) and a condition number of about 133 there. Its gradient and Hessian are

∇f=(−2(1−x)−20x(y−x2)10(y−x2))H=(2−20(y−x2)+40x2−20x−20x10)\begin{gathered} \nabla f = \begin{pmatrix} -2(1 - x) - 20x(y - x^2) \\ 10(y - x^2) \end{pmatrix} \\[6pt] H = \begin{pmatrix} 2 - 20(y - x^2) + 40x^2 & -20x \\ -20x & 10 \end{pmatrix} \end{gathered}

At x0=(0.5,0)\mathbf{x}_0 = (0.5, 0), ∇f=(1.5,−2.5)\nabla f = (1.5, -2.5) and H=(17−10−1010)H = \begin{pmatrix} 17 & -10 \\ -10 & 10 \end{pmatrix}, with eigenvalues 2.9052 and 24.0948. Solving Hh=−∇fH\mathbf{h} = -\nabla f gives x1=(0.642857,0.392857)\mathbf{x}_1 = (0.642857, 0.392857). The copy promised f=0.1786f = 0.1786 there; the true value is 0.1296.

step kkxk\mathbf{x}_kf(xk)f(\mathbf{x}_k)error ∥xk−(1,1)∥\lVert\mathbf{x}_k - (1, 1)\rVertcorrect digits
0(0.5,0)(0.5, 0)0.56251.118
1(0.642857,0.392857)(0.642857, 0.392857)0.12960.7040.2
2(0.939467,0.794621)(0.939467, 0.794621)0.042360.2140.7
3(0.971669,0.943104)(0.971669, 0.943104)0.0008080.06361.2
4(0.999709,0.998632)(0.999709, 0.998632)3.175×10−63.175 \times 10^{-6}0.00142.9
5(0.999998,0.999995)(0.999998, 0.999995)5.18×10−125.18 \times 10^{-12}5.15×10−65.15 \times 10^{-6}5.3
6(1,1)(1, 1)9.12×10−129.12 \times 10^{-12}11.0

The correct digits, −log⁡10-\log_{10} of the error, grow slowly at first and then about double: 1.2, 2.9, 5.3, 11.0. This is the quadratic convergence of lesson 2.4.3. Near a minimum where HH is positive definite, each error is roughly a constant times the square of the last.

From the same start, gradient descent with η=0.02\eta = 0.02 needs 2186 steps to bring ∥∇f∥\lVert\nabla f\rVert below 10−810^{-8}; Newton needs 6. The valley's narrowness does not slow Newton down: HH measures the curvature in every direction and sizes the step for each. That is the rescaling lesson 4.4.2 asked for.

Each Newton step jumps to the minimum of the quadratic copy, whose level curves are the dashed ellipses. Where the Hessian is indefinite, plain Newton heads for the nearest critical point, here a saddle; a safeguarded step goes downhill instead.

Try it

The loop takes six Newton steps on the curved valley from (0.5,0)(0.5, 0) and prints the point, ff, the error and the correct digits.

The last lines run gradient descent from the same start and count its steps.

⌘/Ctrl + Enter runs the cell

When H is not positive definite

Newton's method solves ∇f=0\nabla f = \mathbf{0}, so it heads for the nearest critical point of any kind. Where HH is not positive definite, the copy has no minimum, and two things go wrong.

It can land on a saddle or a maximum. For the quartic at (0,0.5)(0, 0.5), H=(−6002)H = \begin{pmatrix} -6 & 0 \\ 0 & 2 \end{pmatrix} is indefinite. The xx steps are lesson 2.4.3's from 0: 0.166667, 0.169935, 0.169938. They reach the saddle (0.169938,0)(0.169938, 0) of lesson 4.4.1, which is no minimum.

Its step can go uphill. When HH is positive definite, ∇f⋅h=−∇f⊤H−1∇f<0\nabla f \cdot \mathbf{h} = -\nabla f^\top H^{-1}\nabla f < 0, so the step points downhill (lesson 4.2.1). Otherwise it may not. On the curved valley at (−1.5,2.5)(-1.5, 2.5), H=(87303010)H = \begin{pmatrix} 87 & 30 \\ 30 & 10 \end{pmatrix} has determinant −30-30 and eigenvalues −0.3083-0.3083 and 97.3083.

There the Newton step goes to (−3.166667,7.25)(-3.166667, 7.25), where f=55.94f = 55.94, up from 6.5625. Plain Newton does reach (1,1)(1, 1) after 8 steps from this start, but it passes f=611.5f = 611.5 on the way. That was luck.

Lesson 4.4.2's explorer has a Newton's method switch. Turn it on at the valley's start (−1.5,2.5)(-1.5, 2.5) and set 1 step, then 8. The first step leaves the picture uphill; by step 8 the path is back at (1,1)(1, 1).

Rule: A safeguarded Newton step

H positive definite: solve Hh=−∇fotherwise: h=−∇fhalve h until f(x+h)<f(x)\begin{gathered} H \text{ positive definite: solve } H\mathbf{h} = -\nabla f \\[4pt] \text{otherwise: } \mathbf{h} = -\nabla f \\[4pt] \text{halve } \mathbf{h} \text{ until } f(\mathbf{x} + \mathbf{h}) < f(\mathbf{x}) \end{gathered}

Test HH with np.linalg.cholesky, which fails unless HH is positive definite (lesson 3.7.2). Every step now lowers ff, so the method heads for a minimum. Near one, HH is positive definite and the full Newton steps take over.

With the safeguard, the quartic from (0,0.5)(0, 0.5) takes one gradient step, to (−1,−0.5)(-1, -0.5). Then the Newton steps give x=−1.5x = -1.5, −1.333333-1.333333, −1.301932-1.301932, −1.300841-1.300841: lesson 2.4.3's steps from −1-1, ending at the global minimum (−1.30084,0)(-1.30084, 0). The curved valley from (−1.5,2.5)(-1.5, 2.5) takes 12 steps, all downhill.

Try it

The first lines take the plain Newton step on the valley at (−1.5,2.5)(-1.5, 2.5): HH has a negative eigenvalue, ∇f⋅h>0\nabla f \cdot \mathbf{h} > 0, and ff rises.

safe_newton follows the rule above and returns the point and the number of steps. It runs on the valley and on the quartic.

⌘/Ctrl + Enter runs the cell

The cost of a step

Newton takes few steps, but each one costs more. A gradient-descent step needs the gradient, nn numbers.

A Newton step also needs the Hessian, which has n(n+1)2\frac{n(n+1)}{2} different entries because it is symmetric. It then solves an n×nn \times n system, about n33\frac{n^3}{3} multiplications by elimination (lesson 3.3.1).

variables nngradient entriesHessian entriessolving, about
101055333
1001005,0503.3×1053.3 \times 10^5
1,0001,000500,5003.3×1083.3 \times 10^8
1,000,00010610^65×10115 \times 10^{11}3.3×10173.3 \times 10^{17}

With up to a few thousand variables, Newton's handful of steps wins. With millions, as in a large machine-learning model, the Hessian cannot even be stored, and gradient methods are the only choice.

Between the two sit quasi-Newton methods. The best known, BFGS, builds an estimate of HH from the gradients of earlier steps, so it needs only gradients and still converges fast.

scipy.optimize.minimize

minimize(f, x0) from scipy.optimize returns a result with the minimiser .x, the value .fun, the number of iterations .nit, and .success with a .message. Without bounds or constraints it uses BFGS. Given no gradient, it estimates one by finite differences, as in lesson 2.8.1; jac=grad passes yours, which saves evaluations of ff.

method="Newton-CG" with hess=hess takes Newton steps. It solves Hh=−∇fH\mathbf{h} = -\nabla f by an iterative method instead of elimination, which suits a large HH.

On the curved valley from (−1.5,2.5)(-1.5, 2.5), BFGS takes 17 iterations: 66 evaluations of ff without a gradient, and 22 with one. Newton-CG takes 23. The counts can differ slightly with the SciPy version.

minimize finds a local minimum near its start. On the quartic from (1,0.5)(1, 0.5) it returns (1.130901,0)(1.130901, 0), with f=−1.0702f = -1.0702, the minimum that is not global (lesson 4.4.1).

From (−1,0.5)(-1, 0.5) it returns the global one, −3.5139-3.5139. Case study 4.4.5 uses minimize on a yield curve.

Try it

The first three results are the curved valley: BFGS with an estimated gradient, BFGS with jac, and Newton-CG with the Hessian too. Each line prints success, the answer and the counts.

The last loop runs BFGS on the quartic from two starts.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. Do not invert HH. Solve Hh=−∇fH\mathbf{h} = -\nabla f with np.linalg.solve.
  2. Mind the sign. The step solves Hh=−∇fH\mathbf{h} = -\nabla f; with +∇f+\nabla f it goes the wrong way.
  3. Newton finds critical points, not minima. From (0,0.5)(0, 0.5) on the quartic it lands on a saddle; check HH at the answer (lesson 4.4.1).
  4. Away from the minimum, HH may be indefinite and the step may go uphill. Test HH, fall back to a gradient step, and halve until ff falls.
  5. A wrong Hessian spoils the speed. Check it against central differences of the gradient (lesson 4.1.2).
  6. Few steps is not the same as cheap. Each step forms HH and solves a system; with many variables, use BFGS or gradient methods.
  7. success is not a proof. minimize reports a local minimum near its start; try other starts.

Newton's method in several variables

0 of 12 correct

  1. 1.
  2. 2.
  3. 3.
  4. 4.
  5. 5.
  6. 6.
  7. 7.
  8. 8.
  9. 9.
  10. 10.
  11. 11.
  12. 12.
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

  • Newton's method jumps to the minimum of the quadratic copy: solve H(xk)h=−∇f(xk)H(\mathbf{x}_k)\mathbf{h} = -\nabla f(\mathbf{x}_k), then xk+1=xk+h\mathbf{x}_{k+1} = \mathbf{x}_k + \mathbf{h}. In one variable it is lesson 2.4.3's −f′/f′′-f'/f''.
  • A quadratic with positive definite AA is solved in one step. Near a minimum with HH positive definite, the correct digits about double each step, however narrow the valley.
  • Newton finds critical points of any kind. Where HH is not positive definite it can land on a saddle or step uphill. The safeguard: a gradient step instead, and halving until ff falls.
  • A Newton step costs a Hessian, n(n+1)2\frac{n(n+1)}{2} entries, and a solve, about n33\frac{n^3}{3} multiplications. Gradient descent takes many cheap steps; BFGS sits between, building HH from gradients.
  • scipy.optimize.minimize(f, x0, jac=grad) uses BFGS by default; method="Newton-CG" with hess takes Newton steps. It returns a local minimum near the start.

Free further reading

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