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, and , lies in the set. The points of the segment are for from 0 to 1 (lesson 3.1.1). Here gives , gives , and the midpoint.
- Convex: a line, a half-plane such as , a disc, and the triangle , , .
- Not convex: the ring . The points and are in it, but their midpoint, the origin, is in the hole.
- Not convex: a circle, the curve of lesson 4.5.1. The midpoint of and 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 to crosses the hole.
Rule: Linear constraints give convex sets
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 () to ().
All 11 are in the triangle. In the ring, the seven points from to fall in the hole.
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
The left side is the function partway between and ; the right side is the chord at the same place. A function is concave when is convex. Lesson 2.4.2's "concave up" is this convex.
Take between and 3, where and 9. At , halfway, the curve is at 1 and the chord at 5.
| 0.25 | 0.5 | 0.75 | |
|---|---|---|---|
| 2 | 1 | 0 | |
| curve | 4 | 1 | 0 |
| chord | 7 | 5 | 3 |
Now take . It has a global minimum, the lowest value anywhere, at , and a local one, lowest only nearby, at . The chord between them passes under the hump, so is not convex: at the curve is 0 and the chord .
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: is convex where at every point (lesson 2.4.2). For , , which is at 0: the hump bends down.
- Several variables: is convex where its Hessian (lesson 4.3.1) is positive semidefinite at every point. Lesson 3.7.1's eigenvalue test checks that: every eigenvalue of is .
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 has tangent plane at . The bowl is above it by 3 at , by 9 at and by 6 at .
Rule: Convexity tests
Either test shows that is convex on a convex set. With positive definite everywhere, is strictly convex: every chord lies strictly above the graph, except at its ends.
| eigenvalues | convex? | ||
|---|---|---|---|
| 2, 4 | yes, strictly | ||
| 0, 4 | yes, not strictly | ||
| , 2 | no: a saddle | ||
| , 2 | only where |
A linear function such as has , whose eigenvalues are 0 and 0. It is convex and concave at once.
Try it
The first loop is the chord table for . Then brentq (lesson 1.3.2) finds the two minima, where is 0. The last line compares curve and chord at 0, and prints .
Try it
eigvalsh (lesson 3.7.1) gives the eigenvalues of each Hessian in the table, at and at . 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 .
Convex problems
A convex problem minimises a convex function over a convex set. Its constraints may be inequalities with convex, and linear equations.
Rule: Convex problems
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 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 , starts at 1 and 0.5 end at the local minimum 1.347; only the start finds the global one.
In a convex problem one candidate settles it. Minimise over the disc : a linear function over a convex set.
Lesson 4.5.1's two candidates on the circle were with and with . A constraint needs (lesson 4.5.2), so passes, and convexity says it is the minimum, , without comparing values.
Try it
The first loop runs a local search on from three starts. The second solves the disc problem from three starts with SLSQP (lesson 4.5.2); each finds .
The answers are rounded to 4 decimals.
Flat bottoms, and no bottom
Convexity promises that minima are global, not that there is exactly one, or any.
- Semidefinite: a flat bottom. is convex but not strictly. It is 0 at every point of the line , such as and : infinitely many minimum points, all with the same value.
- No minimum. is convex, since its second derivative 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 with symmetric (lesson 3.7.1). Its Hessian is the constant matrix . So is convex exactly when is positive semidefinite, and strictly convex when is positive definite.
Add linear constraints, which give a convex set. The result is a convex problem, and when is positive definite it has at most one minimum. That is why lesson 4.5.1's linear system for , with , gave the one global minimum.
Case study 4.5.5 relies on this. A portfolio's variance is , a covariance matrix 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 , as lesson 3.7.1 found them: all positive, so is strictly convex.
Then the flat bottom: , which is with all ones, is 0 at three points of the line .
Common mistakes
- One minimum does not make a function convex. The valley has one critical point, its minimum , yet its Hessian at has eigenvalue . The chord from to passes under the graph: 2 at the midpoint, where .
- Test everywhere, not at one point. has a positive definite at , yet it is not convex where .
- Semidefinite allows a flat bottom. has a whole line of minima.
- A curved equation is not a convex constraint. A circle is not a convex set; only linear equations keep a problem convex.
- Maximising a convex function is not a convex problem. Maximising a concave function is, since it is minimising .
- Convex does not promise a minimum. falls for ever.
Try it
The valley's Hessian at and at its minimum . Then the chord test between and : the two ends, the midpoint, and the chord's height there.
The last line shows at 5 and 10, still falling towards 0. It is positive for every , so it never reaches 0.
Convex functions and convex problems
0 of 12 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 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,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for 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: in one variable, positive semidefinite everywhere in several. 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.
- with positive semidefinite and linear constraints is a convex problem.