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

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

Optimality conditions in several variables

The problem

To minimise a function f(x)f(\mathbf{x}) of nn variables is to find a point x∗\mathbf{x}^* where ff is as small as possible. The point x∗\mathbf{x}^* is a minimiser and f(x∗)f(\mathbf{x}^*) is the minimum value. As in lesson 2.4.1, an answer has two parts: where, in the units of x\mathbf{x}, and how much, in the units of ff.

The function being minimised is called the objective. Unconstrained means that x\mathbf{x} may be any point of Rn\mathbb{R}^n. Module 4.5 adds conditions, such as portfolio weights that must add up to 1.

A maximum needs no new theory. The highest point of ff is the lowest point of −f-f, so this module speaks only of minima.

The first-order condition

Lesson 4.2.1 showed that the slope of ff in a unit direction u\mathbf{u} is ∇f⋅u\nabla f \cdot \mathbf{u}, and that ff falls fastest against the gradient. So if ∇f(x)≠0\nabla f(\mathbf{x}) \neq \mathbf{0}, a small step against the gradient lowers ff, and x\mathbf{x} is not a minimum.

Take f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x, whose gradient is ∇f=(2x+y−3, x+2y)\nabla f = (2x + y - 3,\ x + 2y). At (0,1)(0, 1), f=1f = 1 and ∇f=(−2,2)\nabla f = (-2, 2).

A step of size tt against the gradient goes to (0,1)−t(−2,2)(0, 1) - t(-2, 2). The tangent-plane estimate of lesson 4.2.2 says ff falls by about t∥∇f∥2=8tt\lVert\nabla f\rVert^2 = 8t.

step ttnew pointff thereestimate 1−8t1 - 8t
0.1(0.2,0.8)(0.2, 0.8)0.240.2
0.05(0.1,0.9)(0.1, 0.9)0.610.6
0.01(0.02,0.98)(0.02, 0.98)0.92040.92

Every step lowers ff, and the smaller the step, the closer the estimate.

Rule: First-order condition

∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0}

At a minimum of a differentiable function, every partial derivative is zero. A point where this holds is a critical point (lesson 4.3.2). The condition is needed but not enough: a maximum or a saddle passes it too.

The saddle x2−y2x^2 - y^2 of lesson 4.1.1 has ∇f=(2x,−2y)=0\nabla f = (2x, -2y) = \mathbf{0} at the origin. Yet f(0,0.1)=−0.01f(0, 0.1) = -0.01 is below f(0,0)=0f(0, 0) = 0, so the origin is not a minimum.

For f=x2+xy+y2−3xf = x^2 + xy + y^2 - 3x, the condition is two linear equations, 2x+y=32x + y = 3 and x+2y=0x + 2y = 0. That is a linear system (lesson 3.3.1), and its solution is (2,−1)(2, -1), with f(2,−1)=−3f(2, -1) = -3. On a grid around it, every neighbour is higher:

y=−0.5y = -0.5y=−1y = -1y=−1.5y = -1.5
x=1.5x = 1.5−2.75-2.75−2.75-2.75−2.25-2.25
x=2x = 2−2.75-2.75−3-3−2.75-2.75
x=2.5x = 2.5−2.25-2.25−2.75-2.75−2.75-2.75

A table only samples a few points. The next section gives a test that settles it.

Try it

grad is ∇f\nabla f. The loop takes the three steps of the first table and prints ff against the estimate 1−8t1 - 8t.

Then np.linalg.solve finds the critical point from the two equations, and the loop prints the grid of ff around it. The last line gives the eigenvalues of the Hessian, used in the next section.

⌘/Ctrl + Enter runs the cell

Second-order conditions

Lesson 4.3.2's test reads the Hessian HH, the matrix of second partial derivatives, at a critical point. Positive definite means a minimum, negative definite a maximum, indefinite a saddle, and semidefinite leaves it undecided. For minimising, the test splits into a condition that is needed and one that is enough.

Rule: Second-order conditions

needed: H(x∗) positive semidefiniteenough: ∇f(x∗)=0and H(x∗) positive definite\begin{gathered} \text{needed: } H(\mathbf{x}^*) \text{ positive semidefinite} \\[4pt] \text{enough: } \nabla f(\mathbf{x}^*) = \mathbf{0} \\ \text{and } H(\mathbf{x}^*) \text{ positive definite} \end{gathered}

At every local minimum the Hessian is positive semidefinite (lesson 3.7.1). A critical point with a positive definite Hessian is a strict local minimum: every nearby point is higher.

For f=x2+xy+y2−3xf = x^2 + xy + y^2 - 3x the Hessian is H=(2112)H = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} at every point. Its eigenvalues are 1 and 3 (lesson 3.6.3), both positive, so (2,−1)(2, -1) is a strict local minimum.

The "needed" condition rules points out. If HH has a negative eigenvalue, ff falls along that eigenvector, so the point is no minimum. At the saddle x2−y2x^2 - y^2, HH has eigenvalues 2 and −2-2.

The gap between the two conditions is a semidefinite Hessian with a zero eigenvalue. Lesson 4.3.2 showed it with x4+y2x^4 + y^2 and x3+y2x^3 + y^2: both have ∇f=0\nabla f = \mathbf{0} and H=(0002)H = \begin{pmatrix} 0 & 0 \\ 0 & 2 \end{pmatrix} at the origin, so both pass the needed condition. Only the first has a minimum there.

Local and global minima

A local minimum is lower than every point near it. The global minimum is the lowest value anywhere, as in lesson 2.4.1. Both conditions above are local: they look only near x∗\mathbf{x}^*.

Take f(x,y)=x4−3x2+x+y2f(x, y) = x^4 - 3x^2 + x + y^2. Along y=0y = 0 it is the quartic of lesson 2.4.3.

Its gradient is (4x3−6x+1, 2y)(4x^3 - 6x + 1,\ 2y), so a critical point needs y=0y = 0 and 4x3−6x+1=04x^3 - 6x + 1 = 0. Lesson 2.4.3 found the three roots.

The Hessian is (12x2−6002)\begin{pmatrix} 12x^2 - 6 & 0 \\ 0 & 2 \end{pmatrix}, so the sign of 12x2−612x^2 - 6 decides each point:

critical pointff12x2−612x^2 - 6Hessiankind
(−1.30084,0)(-1.30084, 0)−3.5139-3.513914.3062positive definiteglobal minimum
(0.169938,0)(0.169938, 0)0.0841−5.6535-5.6535indefinitesaddle
(1.130901,0)(1.130901, 0)−1.0702-1.07029.3472positive definitelocal minimum only

Two valleys and a saddle between them. Both minima pass the second-order test; only the comparison of their values shows which is global. The dashed level curve through the saddle encloses one valley in each loop.

To find the global minimum, compare the critical points. On a closed interval lesson 2.4.1 also checked the ends.

In Rn\mathbb{R}^n there are no ends, but there is "far away": here x4x^4 and y2y^2 grow without limit in every direction, with f(3,0)=57f(3, 0) = 57 and f(0,3)=9f(0, 3) = 9. So ff has a lowest point, and it is the lower minimum, −3.5139-3.5139 at (−1.30084,0)(-1.30084, 0).

Without that growth there may be no global minimum. Lesson 4.3.2's cubic x3−3x+y2x^3 - 3x + y^2 has a local minimum at (1,0)(1, 0), where f=−2f = -2, but f(−10,0)=−970f(-10, 0) = -970: it falls without limit.

Rule: Local and global

f grows without limit far away:the lowest critical point is global\begin{gathered} f \text{ grows without limit far away:} \\ \text{the lowest critical point is global} \end{gathered}

If ff falls without limit in some direction, it has no global minimum. Lesson 4.5.3 adds convex functions, for which every local minimum is global.

The methods of lessons 4.4.2 and 4.4.3 walk downhill from a starting point and stop at a minimum near it. Which minimum depends on the start, so case study 4.4.5 tries several.

Try it

np.roots finds the three roots of 4x3−6x+14x^3 - 6x + 1. Each line prints a root, ff there and the entry 12x2−612x^2 - 6 of the Hessian.

The next line shows the quartic growing far away. The last line is the cubic, which has no lowest point.

⌘/Ctrl + Enter runs the cell

Quadratic functions

The most important example is a quadratic function: a quadratic form (lesson 3.7.1) minus a linear part. For a symmetric n×nn \times n matrix AA and a vector b\mathbf{b} in Rn\mathbb{R}^n,

f(x)=12 x⊤A x−b⊤xf(\mathbf{x}) = \tfrac{1}{2}\,\mathbf{x}^\top A\,\mathbf{x} - \mathbf{b}^\top\mathbf{x}

Its gradient is Ax−bA\mathbf{x} - \mathbf{b} and its Hessian is AA at every point. For n=2n = 2, lesson 3.7.1 writes the form out as 12x⊤Ax=12a11x12+a12x1x2+12a22x22\frac{1}{2}\mathbf{x}^\top A\mathbf{x} = \frac{1}{2}a_{11}x_1^2 + a_{12}x_1x_2 + \frac{1}{2}a_{22}x_2^2. Its partial derivative in x1x_1 is a11x1+a12x2a_{11}x_1 + a_{12}x_2, the first entry of AxA\mathbf{x}.

The 12\frac{1}{2} is there so that the gradient has no factor 2.

The example above is one. With A=(2112)A = \begin{pmatrix} 2 & 1 \\ 1 & 2 \end{pmatrix} and b=(3,0)\mathbf{b} = (3, 0), 12x⊤Ax=x2+xy+y2\frac{1}{2}\mathbf{x}^\top A\mathbf{x} = x^2 + xy + y^2 and b⊤x=3x\mathbf{b}^\top\mathbf{x} = 3x.

So the first-order condition ∇f=0\nabla f = \mathbf{0} reads Ax=bA\mathbf{x} = \mathbf{b}: the minimiser of a quadratic solves a linear system. For a quadratic, the second-order expansion of lesson 4.3.1 is exact, with nothing left over. At a solution of Ax∗=bA\mathbf{x}^* = \mathbf{b} its gradient term is zero:

Rule: Minimum of a quadratic

Ax∗=bf(x∗+h)=f(x∗)+12 h⊤A hf(x∗)=−12 b⊤x∗\begin{gathered} A\mathbf{x}^* = \mathbf{b} \\[4pt] f(\mathbf{x}^* + \mathbf{h}) = f(\mathbf{x}^*) + \tfrac{1}{2}\,\mathbf{h}^\top A\,\mathbf{h} \\[4pt] f(\mathbf{x}^*) = -\tfrac{1}{2}\,\mathbf{b}^\top\mathbf{x}^* \end{gathered}

If AA is positive definite, 12h⊤Ah>0\frac{1}{2}\mathbf{h}^\top A\mathbf{h} > 0 for every step h≠0\mathbf{h} \neq \mathbf{0}. So x∗\mathbf{x}^* is the unique global minimum, not just a local one.

For the example, x∗=(2,−1)\mathbf{x}^* = (2, -1) and −12b⊤x∗=−12(6)=−3-\frac{1}{2}\mathbf{b}^\top\mathbf{x}^* = -\frac{1}{2}(6) = -3, as found above. A step h=(0.5,0.5)\mathbf{h} = (0.5, 0.5) raises ff by 12h⊤Ah=0.75\frac{1}{2}\mathbf{h}^\top A\mathbf{h} = 0.75: f(2.5,−0.5)=−2.25f(2.5, -0.5) = -2.25.

Along a unit eigenvector q\mathbf{q} of AA with eigenvalue λ\lambda, a step of tt raises ff by 12λt2\frac{1}{2}\lambda t^2. Here one unit along (1,1)/2(1, 1)/\sqrt{2} adds 1.5, and along (1,−1)/2(1, -1)/\sqrt{2} only 0.5. The bowl is steeper along one eigenvector than the other, which matters for lesson 4.4.2.

Three variables. Lesson 3.7.1's positive definite matrix

K=(4−10−132025)K = \begin{pmatrix} 4 & -1 & 0 \\ -1 & 3 & 2 \\ 0 & 2 & 5 \end{pmatrix}

with b=(3,0,−3)\mathbf{b} = (3, 0, -3) gives x∗=(1,1,−1)\mathbf{x}^* = (1, 1, -1) and a minimum value of −3-3. Its eigenvalues are 1.4711, 4.1674 and 6.3615.

When A is not positive definite

Ax=bA\mathbf{x} = \mathbf{b} can still have a solution, but it is not a minimum.

AAAx=bA\mathbf{x} = \mathbf{b}the quadratic
positive definiteone solutionone global minimum
indefinitea solutiona saddle: no minimum
semidefinite, singularnone, or a whole lineno minimum, or a line of minima

Indefinite. N=(1221)N = \begin{pmatrix} 1 & 2 \\ 2 & 1 \end{pmatrix}, with eigenvalues −1-1 and 3, and b=(3,3)\mathbf{b} = (3, 3) give the solution (1,1)(1, 1), where f=−3f = -3. Along (1,−1)(1, -1), f(1+t,1−t)=−3−t2f(1 + t, 1 - t) = -3 - t^2, which is −4-4, −7-7 and −103-103 at t=1t = 1, 2 and 10.

Semidefinite. J=(1111)J = \begin{pmatrix} 1 & 1 \\ 1 & 1 \end{pmatrix} is singular, so solve stops with an error. With b=(1,1)\mathbf{b} = (1, 1), f=12(x+y)2−(x+y)f = \frac{1}{2}(x + y)^2 - (x + y) is smallest, −0.5-0.5, on the whole line x+y=1x + y = 1. With b=(1,−1)\mathbf{b} = (1, -1), f(t,−t)=−2tf(t, -t) = -2t falls without limit.

Try it

The first lines check that 12x⊤Ax−b⊤x\frac{1}{2}\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x} is the example's ff at a point, then solve Ax=bA\mathbf{x} = \mathbf{b} and test f(x∗+h)−f(x∗)=12h⊤Ahf(\mathbf{x}^* + \mathbf{h}) - f(\mathbf{x}^*) = \frac{1}{2}\mathbf{h}^\top A\mathbf{h}.

Then the 3×33 \times 3 example, the saddle of NN and the singular JJ. Always check the eigenvalues before trusting a solution.

⌘/Ctrl + Enter runs the cell

Least squares is a quadratic

Lesson 3.5.3 chose x^\hat{\mathbf{x}} to make ∥Mx−y∥2\lVert M\mathbf{x} - \mathbf{y} \rVert^2 as small as possible, for a matrix MM and data y\mathbf{y}. Multiplied out, this is

F(x)=x⊤M⊤M x−2(M⊤y)⊤x+y⊤y\begin{gathered} F(\mathbf{x}) = \mathbf{x}^\top M^\top M\,\mathbf{x} - 2(M^\top\mathbf{y})^\top\mathbf{x} + \mathbf{y}^\top\mathbf{y} \end{gathered}

That is a quadratic function with A=2M⊤MA = 2M^\top M and b=2M⊤y\mathbf{b} = 2M^\top\mathbf{y}, plus a constant. So ∇F=2M⊤(Mx−y)\nabla F = 2M^\top(M\mathbf{x} - \mathbf{y}), and ∇F=0\nabla F = \mathbf{0} is M⊤M x=M⊤yM^\top M\,\mathbf{x} = M^\top\mathbf{y}: the normal equations of lesson 3.5.3.

The Hessian 2M⊤M2M^\top M is always positive semidefinite, and positive definite when the columns of MM are independent (lesson 3.7.1). Then the normal equations give the global minimum.

For lesson 3.5.3's five points, the line y=1+1.2xy = 1 + 1.2x leaves a sum of squares of 0.8, and the gradient there is (0,0)(0, 0). The line y=1+xy = 1 + x leaves 2, with gradient (−4,−12)(-4, -12): both entries negative, so raising the intercept or the slope lowers FF. The Hessian has eigenvalues 2.9844 and 67.0156.

When a model is not linear in its unknowns, ∇F=0\nabla F = \mathbf{0} is no longer a linear system. Lessons 4.4.2 and 4.4.3 find such minima by walking downhill, and case study 4.4.5 fits a yield curve that way. Module 5.9 maximises a likelihood with the same conditions.

Try it

M has a column of ones and the five xx values; F is the sum of squared misses and grad its gradient 2M⊤(Mx−y)2M^\top(M\mathbf{x} - \mathbf{y}).

The gradient is zero at the least-squares answer from the normal equations, and not at (1,1)(1, 1).

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. ∇f=0\nabla f = \mathbf{0} is not enough. Maxima and saddles pass it too; check the Hessian.
  2. A semidefinite Hessian proves nothing. x4+y2x^4 + y^2 and x3+y2x^3 + y^2 have the same Hessian at the origin; only the first has a minimum there (lesson 4.3.2).
  3. Both conditions are local. The minimum at (1.130901,0)(1.130901, 0) passes the test but lies above the one at (−1.30084,0)(-1.30084, 0).
  4. A local minimum may have no global one below it. x3−3x+y2x^3 - 3x + y^2 falls without limit as xx goes to −∞-\infty.
  5. Mind the 12\frac{1}{2}. The gradient of 12x⊤Ax\frac{1}{2}\mathbf{x}^\top A\mathbf{x} is AxA\mathbf{x}; the gradient of x⊤Ax\mathbf{x}^\top A\mathbf{x} is 2Ax2A\mathbf{x}.
  6. Check AA before you solve. An indefinite AA still gives a solution of Ax=bA\mathbf{x} = \mathbf{b}, and it is a saddle.
  7. For a maximum, minimise −f-f. At a maximum the Hessian of ff is negative definite.

Optimality conditions

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

  • To minimise f(x)f(\mathbf{x}) over Rn\mathbb{R}^n is to find the minimiser x∗\mathbf{x}^* and the value f(x∗)f(\mathbf{x}^*). A maximum of ff is a minimum of −f-f.
  • First-order condition: ∇f(x∗)=0\nabla f(\mathbf{x}^*) = \mathbf{0}. Needed at every minimum of a differentiable ff, but maxima and saddles pass it too.
  • Second-order conditions: at a local minimum HH is positive semidefinite; ∇f=0\nabla f = \mathbf{0} with HH positive definite is enough for a strict local minimum. A zero eigenvalue leaves it undecided.
  • Local is not global. If ff grows without limit in every direction, compare the critical points; if it falls without limit in some direction, there is no global minimum.
  • f(x)=12x⊤Ax−b⊤xf(\mathbf{x}) = \frac{1}{2}\mathbf{x}^\top A\mathbf{x} - \mathbf{b}^\top\mathbf{x} has gradient Ax−bA\mathbf{x} - \mathbf{b} and Hessian AA. If AA is positive definite, the solution of Ax∗=bA\mathbf{x}^* = \mathbf{b} is the global minimum, with value −12b⊤x∗-\frac{1}{2}\mathbf{b}^\top\mathbf{x}^*.
  • Least squares ∥Mx−y∥2\lVert M\mathbf{x} - \mathbf{y} \rVert^2 is a quadratic with Hessian 2M⊤M2M^\top M; its first-order condition is the normal equations.

Free further reading

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