SigmaField
Mathematical Foundations for Quant Finance
0 of 256 lessons
      1. 4.5.1 Lagrange multipliersConcept · 30 min
      2. 4.5.2 Inequality constraints and the KKT conditionsConcept · 30 min
      3. 4.5.3 Convex functions and convex problemsConcept · 30 min
      4. 4.5.4 Practice set: constrained optimisationPractice set · 25 min
      5. 4.5.5 Case study: the minimum-variance portfolio and the efficient frontierCase study · 35 min

Level 4, Multivariable calculus and optimisation 4.5 Constrained optimisation Lesson 3 of 5

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

Convex functions and convex problems

Convex sets

Lessons 4.5.1 and 4.5.2 produced candidates and then had to compare them. Some problems need no comparison: a candidate that passes the conditions is the answer. Those are the convex problems, and they start with a shape.

A set of points is convex when the whole straight segment between any two of its points, PP and QQ, lies in the set. The points of the segment are tP+(1−t)QtP + (1 - t)Q for tt from 0 to 1 (lesson 3.1.1). Here t=1t = 1 gives PP, t=0t = 0 gives QQ, and t=12t = \frac{1}{2} the midpoint.

  • Convex: a line, a half-plane such as x+y≥3x + y \ge 3, a disc, and the triangle a≥0a \ge 0, b≥0b \ge 0, a+b≤1a + b \le 1.
  • Not convex: the ring 1≤x2+y2≤41 \le x^2 + y^2 \le 4. The points (−1.5,0)(-1.5, 0) and (1.5,0)(1.5, 0) are in it, but their midpoint, the origin, is in the hole.
  • Not convex: a circle, the curve x2+y2=5x^2 + y^2 = 5 of lesson 4.5.1. The midpoint of (1,2)(1, 2) and (−1,−2)(-1, -2) is the origin, which is not on the circle.

A convex set holds every segment between its points. The ring is not convex: the segment from PP to QQ crosses the hole.

Rule: Linear constraints give convex sets

a⋅x≤c,a⋅x=cand any list of them\begin{gathered} \mathbf{a} \cdot \mathbf{x} \le c, \quad \mathbf{a} \cdot \mathbf{x} = c \\[4pt] \text{and any list of them} \end{gathered}

Each linear inequality gives a half-plane (a half-space in more variables), and each linear equation a line or plane. The points meeting every constraint of a list form a convex set: a segment inside each set is inside all of them.

Try it

in_triangle and in_ring test whether a point is in each set. The cell checks 11 points along each segment of the figure, from QQ (t=0t = 0) to PP (t=1t = 1).

All 11 are in the triangle. In the ring, the seven points from t=0.2t = 0.2 to t=0.8t = 0.8 fall in the hole.

⌘/Ctrl + Enter runs the cell

Convex functions

A function is convex when every chord of its graph lies on or above the graph. Between two inputs, the function is never above the straight line joining its two values.

Rule: Convex function

f(tP+(1−t)Q)≤t f(P)+(1−t) f(Q)for every P,Q and 0≤t≤1\begin{gathered} f\big(tP + (1 - t)Q\big) \\ \le t\,f(P) + (1 - t)\,f(Q) \\[4pt] \text{for every } P, Q \text{ and } 0 \le t \le 1 \end{gathered}

The left side is the function partway between PP and QQ; the right side is the chord at the same place. A function is concave when −f-f is convex. Lesson 2.4.2's "concave up" is this convex.

Take f(x)=x2f(x) = x^2 between −1-1 and 3, where f=1f = 1 and 9. At x=1x = 1, halfway, the curve is at 1 and the chord at 5.

tt0.250.50.75
x=t(−1)+(1−t) 3x = t(-1) + (1 - t)\,3210
curve f(x)f(x)410
chord t f(−1)+(1−t) f(3)t\,f(-1) + (1 - t)\,f(3)753

Now take q(x)=x4−4x2+xq(x) = x^4 - 4x^2 + x. It has a global minimum, the lowest value anywhere, −5.4442-5.4442 at x=−1.473x = -1.473, and a local one, lowest only nearby, −2.6186-2.6186 at x=1.347x = 1.347. The chord between them passes under the hump, so qq is not convex: at x=0x = 0 the curve is 0 and the chord −3.9682-3.9682.

A convex function lies under each of its chords. A function with two valleys has a chord that passes under the hump between them.

The tests

Checking every chord is impossible, but derivatives do it for you.

  • One variable: ff is convex where f′′(x)≥0f''(x) \ge 0 at every point (lesson 2.4.2). For qq, q′′(x)=12x2−8q''(x) = 12x^2 - 8, which is −8-8 at 0: the hump bends down.
  • Several variables: ff is convex where its Hessian HH (lesson 4.3.1) is positive semidefinite at every point. Lesson 3.7.1's eigenvalue test checks that: every eigenvalue of HH is ≥0\ge 0.

A convex function also lies on or above each of its tangent planes (lesson 4.2.2), as lesson 2.4.2's concave-up curves lie above their tangents. The bowl x2+2y2x^2 + 2y^2 has tangent plane 3+2(x−1)+4(y−1)3 + 2(x - 1) + 4(y - 1) at (1,1)(1, 1). The bowl is above it by 3 at (0,0)(0, 0), by 9 at (2,−1)(2, -1) and by 6 at (3,2)(3, 2).

Rule: Convexity tests

f′′(x)≥0 for every xH positive semidefinite everywhere\begin{gathered} f''(x) \ge 0 \text{ for every } x \\[4pt] H \text{ positive semidefinite everywhere} \end{gathered}

Either test shows that ff is convex on a convex set. With HH positive definite everywhere, ff is strictly convex: every chord lies strictly above the graph, except at its ends.

f(x,y)f(x, y)HHeigenvaluesconvex?
x2+2y2x^2 + 2y^2(2004)\begin{pmatrix} 2 & 0 \\ 0 & 4 \end{pmatrix}2, 4yes, strictly
(x+y)2(x + y)^2(2222)\begin{pmatrix} 2 & 2 \\ 2 & 2 \end{pmatrix}0, 4yes, not strictly
x2−y2x^2 - y^2(200−2)\begin{pmatrix} 2 & 0 \\ 0 & -2 \end{pmatrix}−2-2, 2no: a saddle
x3−3x+y2x^3 - 3x + y^2(6x002)\begin{pmatrix} 6x & 0 \\ 0 & 2 \end{pmatrix}6x6x, 2only where x≥0x \ge 0

A linear function such as 3x−y3x - y has H=0H = 0, whose eigenvalues are 0 and 0. It is convex and concave at once.

Try it

The first loop is the chord table for x2x^2. Then brentq (lesson 1.3.2) finds the two minima, where q′(x)=4x3−8x+1q'(x) = 4x^3 - 8x + 1 is 0. The last line compares curve and chord at 0, and prints q′′(0)q''(0).

⌘/Ctrl + Enter runs the cell

Try it

eigvalsh (lesson 3.7.1) gives the eigenvalues of each Hessian in the table, at (1,0)(1, 0) and at (−1,0)(-1, 0). Only the cubic's eigenvalues change from one point to the other.

The last loop measures how far the bowl lies above its tangent plane at (1,1)(1, 1).

⌘/Ctrl + Enter runs the cell

Convex problems

A convex problem minimises a convex function over a convex set. Its constraints may be inequalities g≤cg \le c with gg convex, and linear equations.

Rule: Convex problems

local minimum=global minimumKKT point=minimum\begin{gathered} \text{local minimum} = \text{global minimum} \\[4pt] \text{KKT point} = \text{minimum} \end{gathered}

In a convex problem every local minimum is global, and any point that passes the KKT conditions of lesson 4.5.2 is a minimum. If ff is strictly convex, there is at most one minimum point.

The picture behind it: a convex function has no hump, so it cannot have a second, lower valley. The Proof Gym proves it.

A search can get stuck when the problem is not convex. A local search such as scipy.optimize.minimize walks downhill from where it starts. On qq, starts at 1 and 0.5 end at the local minimum 1.347; only the start −1-1 finds the global one.

In a convex problem one candidate settles it. Minimise x+2yx + 2y over the disc x2+y2≤5x^2 + y^2 \le 5: a linear function over a convex set.

Lesson 4.5.1's two candidates on the circle were (1,2)(1, 2) with λ=12\lambda = \frac{1}{2} and (−1,−2)(-1, -2) with λ=−12\lambda = -\frac{1}{2}. A ≤\le constraint needs λ≤0\lambda \le 0 (lesson 4.5.2), so (−1,−2)(-1, -2) passes, and convexity says it is the minimum, −5-5, without comparing values.

Try it

The first loop runs a local search on qq from three starts. The second solves the disc problem from three starts with SLSQP (lesson 4.5.2); each finds (−1,−2)(-1, -2).

The answers are rounded to 4 decimals.

⌘/Ctrl + Enter runs the cell

Flat bottoms, and no bottom

Convexity promises that minima are global, not that there is exactly one, or any.

  • Semidefinite: a flat bottom. (x+y)2(x + y)^2 is convex but not strictly. It is 0 at every point of the line x+y=0x + y = 0, such as (1,−1)(1, -1) and (−2,2)(-2, 2): infinitely many minimum points, all with the same value.
  • No minimum. e−xe^{-x} is convex, since its second derivative e−xe^{-x} is positive, but it only falls towards 0: 0.006738 at 5, 0.000045 at 10. A linear function on a whole plane has no minimum either.

Quadratic forms with linear constraints

Take f(x)=x⊤Axf(\mathbf{x}) = \mathbf{x}^\top A\mathbf{x} with AA symmetric (lesson 3.7.1). Its Hessian is the constant matrix 2A2A. So ff is convex exactly when AA is positive semidefinite, and strictly convex when AA is positive definite.

Add linear constraints, which give a convex set. The result is a convex problem, and when AA is positive definite it has at most one minimum. That is why lesson 4.5.1's linear system for x2+2y2+3z2x^2 + 2y^2 + 3z^2, with A=(100020003)A = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3 \end{pmatrix}, gave the one global minimum.

Case study 4.5.5 relies on this. A portfolio's variance is w⊤Σw\mathbf{w}^\top\Sigma\mathbf{w}, a covariance matrix Σ\Sigma is positive semidefinite (lesson 3.7.1), and budgets and target returns are linear constraints.

Try it

The eigenvalues of case study 3.2.5's covariance matrix Σ\Sigma, as lesson 3.7.1 found them: all positive, so w⊤Σw\mathbf{w}^\top\Sigma\mathbf{w} is strictly convex.

Then the flat bottom: (x+y)2(x + y)^2, which is x⊤Jx\mathbf{x}^\top J\mathbf{x} with JJ all ones, is 0 at three points of the line x+y=0x + y = 0.

⌘/Ctrl + Enter runs the cell

Common mistakes

  1. One minimum does not make a function convex. The valley (1−x)2+5(y−x2)2(1 - x)^2 + 5(y - x^2)^2 has one critical point, its minimum (1,1)(1, 1), yet its Hessian at (0,1)(0, 1) has eigenvalue −18-18. The chord from (−1,1)(-1, 1) to (1,1)(1, 1) passes under the graph: 2 at the midpoint, where f=6f = 6.
  2. Test HH everywhere, not at one point. x3−3x+y2x^3 - 3x + y^2 has a positive definite HH at (1,0)(1, 0), yet it is not convex where x<0x < 0.
  3. Semidefinite allows a flat bottom. (x+y)2(x + y)^2 has a whole line of minima.
  4. A curved equation is not a convex constraint. A circle is not a convex set; only linear equations keep a problem convex.
  5. Maximising a convex function is not a convex problem. Maximising a concave function is, since it is minimising −f-f.
  6. Convex does not promise a minimum. e−xe^{-x} falls for ever.

Try it

The valley's Hessian at (0,1)(0, 1) and at its minimum (1,1)(1, 1). Then the chord test between (−1,1)(-1, 1) and (1,1)(1, 1): the two ends, the midpoint, and the chord's height there.

The last line shows e−xe^{-x} at 5 and 10, still falling towards 0. It is positive for every xx, so it never reaches 0.

⌘/Ctrl + Enter runs the cell

Convex functions and convex problems

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

  • A set is convex when the segment between any two of its points stays inside. Linear constraints, and any list of them, give convex sets; a ring or a circle does not.
  • A function is convex when every chord lies on or above its graph: f′′≥0f'' \ge 0 in one variable, HH positive semidefinite everywhere in several. HH positive definite makes it strictly convex.
  • A convex problem minimises a convex function over a convex set. Every local minimum is global, and a point that passes the KKT conditions is a minimum. Strictly convex: at most one minimum point.
  • Semidefinite allows a flat bottom of many minima; a convex function may also have no minimum.
  • x⊤Ax\mathbf{x}^\top A\mathbf{x} with AA positive semidefinite and linear constraints is a convex problem.

Free further reading

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