Preview

A lesson from Essential Calculus for ML

£29/month

£29/month£29/month or £249/year · 30-day money-back guarantee

Get full access

Our content is best on a larger screen

Convex functions

What convex means

Explanation

A graph that is concave up, such as the graph of x2x^2, bends upwards: it lies above each of its tangent lines. Now join two points of the graph with a secant line instead. Because the graph bends upwards, it sags below the secant line between those two points, and the same happens for every pair of points we choose.

A function with this property is called convex: it is the same bending, described with secant lines rather than tangent lines. For a differentiable function, convex means exactly the same as concave up. A secant line needs no derivative, though, so convexity also makes sense for graphs with corners, such as ∣x∣|x|, where there is no tangent line.

To make this precise, we compare the graph's height with the secant line's height above each point between two inputs x1x_1 and x2x_2.

Convex function

Definition

A function ff whose domain is an interval is convex if, for all x1x_1 and x2x_2 in the domain and every tt in [0,1][0, 1],

f((1−t)x1+tx2)≤(1−t)f(x1)+tf(x2).f\big((1-t)x_1 + tx_2\big) \leq (1-t)f(x_1) + tf(x_2).

The first diagram below builds this up step by step. The second shows a function that is not convex: drag its endpoints to find a secant line that the graph rises above.

123−1−2−312345
f
x1
x2

Step 1: Two inputs and the secant line

Take two inputs x1 and x2 in the domain. The green segment is the secant line through (x1,f(x1)) and (x2,f(x2)).

Step 2: The point between them

A fraction t of the way from x1 to x2 is the point between them,

(1−t)x1+tx2.

Here t=14.

Step 3: Two heights to compare

Above the point between them, the definition asks for the graph to be no higher than the secant line:

f((1−t)x1+tx2)≤(1−t)f(x1)+tf(x2).

The secant line's height puts the same weights, 1−t and t, on the heights f(x1) and f(x2) as the point between them puts on x1 and x2.

Step 4: Vary t

Drag the slider to move the point between them from x1 (t=0) to x2 (t=1). For every t the graph stays on or below the secant line. Touching it is allowed, since ≤ includes equality.

Key Point

One choice of x1x_1, x2x_2 and tt where the graph rises above the secant line shows that ff is not convex. Choices where it does not, however many, prove nothing.

Practice questions

5 questions

The graph of f(x)=max⁡(0,x)f(x) = \max(0, x), which is 00 for x<0x < 0 and xx for x≥0x \geq 0, with domain R\mathbb{R} is shown below.

0.511.522.5−0.5−1−1.5−2−2.50.511.522.53−0.5
f

Is ff convex?

Select the correct answer:

+ 4 more questions · unlocked with a subscription

Convexity in two variables

Explanation

Convexity for a function f(x,y)f(x, y) on the whole plane is the same idea as in one variable. The two inputs are now points PP and QQ of the plane rather than numbers x1x_1 and x2x_2, and the graph is a surface, but we still compare it with a secant line: the straight line joining the two points of the surface above PP and QQ.

Convex function of two variables

Definition

A function f(x,y)f(x, y) on the whole plane is convex if, for all points PP and QQ and every tt in [0,1][0, 1],

f((1−t)P+tQ)≤(1−t)f(P)+tf(Q).f\big((1-t)P + tQ\big) \leq (1-t)f(P) + tf(Q).

For P=(p1,p2)P = (p_1, p_2) and Q=(q1,q2)Q = (q_1, q_2), the point between them puts the same weights, 1−t1-t and tt, on each coordinate:

(1−t)P+tQ=((1−t)p1+tq1, (1−t)p2+tq2).(1-t)P + tQ = \big((1-t)p_1 + tq_1,\ (1-t)p_2 + tq_2\big).

The first diagram below builds this comparison step by step. The second shows a function that is not convex.

x
y
z
P
Q

Step 1: The surface and two points

The surface z=x2+y2, with two points P and Q in the xy-plane.

Drag the scene to rotate it.

Step 2: The point between them

The point between them, (1−t)P+tQ, lies on the segment from P to Q. Here t=14. Over that segment, the surface traces the blue curve.

Drag the scene to rotate it.

Step 3: Two heights to compare

The secant line joins the points of the surface over P and over Q. Above the point between them, the definition asks for the surface to be no higher than the secant line, and here it is below.

Every secant line of this bowl lies on or above the surface between its endpoints, so f(x,y)=x2+y2 is convex.

Drag the scene to rotate it.

Step 4: Vary t

Drag the slider to move the point between them from P (t=0) to Q (t=1). For every t the surface stays on or below the secant line.

As in one variable, one choice of PP, QQ and tt where the inequality fails shows that ff is not convex; choices where it holds, however many, prove nothing. Showing that a function of two variables is convex takes a general test, which we meet in a later lesson.

Example

Show that f(x,y)=x2−4xy+y2f(x, y) = x^2 - 4xy + y^2 on the whole plane is not convex, using P=(3,1)P = (3, 1), Q=(−1,−3)Q = (-1, -3) and t=14t = \dfrac{1}{4}.

Solution

With t=14t = \dfrac{1}{4}, the weights are 34\dfrac{3}{4} on PP and 14\dfrac{1}{4} on QQ, so the point between them is

(1−t)P+tQ=(34(3)+14(−1), 34(1)+14(−3))=(2,0).(1-t)P + tQ = \left(\dfrac{3}{4}(3) + \dfrac{1}{4}(-1),\ \dfrac{3}{4}(1) + \dfrac{1}{4}(-3)\right) = (2, 0).

We evaluate ff at PP, at QQ and at (2,0)(2, 0):

f(3,1)=9−12+1=−2f(−1,−3)=1−12+9=−2f(2,0)=4−0+0=4\begin{align*} f(3, 1) &= 9 - 12 + 1 = -2 \\ f(-1, -3) &= 1 - 12 + 9 = -2 \\ f(2, 0) &= 4 - 0 + 0 = 4 \end{align*}

We find the secant line's height above (2,0)(2, 0) with the same weights, now on f(P)f(P) and f(Q)f(Q):

(1−t)f(P)+tf(Q)=34(−2)+14(−2)=−2.(1-t)f(P) + tf(Q) = \dfrac{3}{4}(-2) + \dfrac{1}{4}(-2) = -2.

We compare the two heights:

f(2,0)=4>−2=(1−t)f(P)+tf(Q).f(2, 0) = 4 > -2 = (1-t)f(P) + tf(Q).

The surface rises above the secant line here, so the inequality fails for this choice of PP, QQ and tt. Therefore f(x,y)=x2−4xy+y2f(x, y) = x^2 - 4xy + y^2 is not convex.

Practice questions

4 questions

Take f(x,y)=x2−y2f(x, y) = x^2 - y^2 on the whole plane, P=(1,−1)P = (1, -1), Q=(1,3)Q = (1, 3) and t=14t = \dfrac{1}{4}. Which statement is correct?

Select the correct answer:

+ 3 more questions · unlocked with a subscription

Memory is built by recalling, not by reading.

Every time you pull an answer from memory, the memory gets stronger. Reading doesn't do that. Nodeledge asks you questions on every step of every lesson, then asks again after days, weeks and months.

Get full access

£29/month or £249/year · 30-day money-back guarantee

See the learning path