Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

8.5. Convexity

As we saw in Chapter 8.3, gradient descent can get stuck in a local minimum that isn’t a global minimum. Let’s revisit the example from that section.


Motivation

Use the buttons to change the initial guess, then drag the slider to step through the updates. Why do the two runs end at different points?

Loading...

When we’re minimizing empirical risk, a local minimum that isn’t global corresponds to a parameter vector w⃗\vec w that doesn’t give us the smallest possible empirical risk. The gradient is 0⃗\vec 0 there, so gradient descent stops updating the parameters.

In this section, we’ll study convex functions, for which every local minimum is a global minimum. We’ll also see what convexity tells us about the optimal parameters for least squares.


Formal Definition of Convexity

You may remember from calculus that if f′′(x)≥0f''(x) \geq 0 everywhere, then ff is convex. But this test requires second derivatives to exist. For instance, f(x)=∣x∣f(x)=|x| is convex even though it isn’t differentiable at x=0x=0.

Also, for a vector-to-scalar function, there isn’t a single second derivative to check. The corresponding test involves a matrix of second partial derivatives called the Hessian matrix, which we’ll introduce shortly.

We’d like a definition that works for both scalar-to-scalar and vector-to-scalar functions, whether or not they’re differentiable.

Let’s start with a scalar-to-scalar function. Pick two points on its graph and draw the line segment connecting them. For a convex function, this segment lies on or above the graph between the two points. Recall that the line through two points on a graph is called a secant line.

In each plot below, the orange segment connects the points on the graph corresponding to the inputs x=−1x=-1 and y=1y=1. Here, yy is another input, not the height of the function. Drag tt to compare the height of the graph (blue) with the height of the segment (orange). Which function isn’t convex?

Loading...
Loading...
Loading...

Suppose we pick two inputs xx and yy, and some t∈[0,1]t \in [0,1].

  • The input that is a fraction tt of the way from xx to yy is (1−t)x+ty(1-t)x+ty.

  • The height of the secant line at that input is (1−t)f(x)+tf(y)(1-t)f(x)+tf(y).

The segment lies on or above the graph if

f((1−t)x+ty)≤(1−t)f(x)+tf(y)f((1-t)x+ty) \leq (1-t)f(x)+tf(y)

For vector-to-scalar functions, we replace the scalar inputs xx and yy with vectors x⃗\vec x and y⃗\vec y.

In other words, for all inputs,

height of function between (x,f(x)) and (y,f(y))≤height of secant line between (x,f(x)) and (y,f(y))\begin{gathered} \text{height of function between }(x,f(x))\text{ and }(y,f(y))\\ \leq\\ \text{height of secant line between }(x,f(x))\text{ and }(y,f(y)) \end{gathered}

The two heights are compared at the same input, (1−t)x+ty(1-t)x+ty.

The inequality must hold for every pair of inputs and every t∈[0,1]t \in [0,1]. One segment that lies above the graph isn’t enough to prove convexity, but one counterexample is enough to disprove it.

For example, consider the third plot above, f(x)=x3−12xf(x)=x^3-\frac12x. If we choose x=−1x=-1, y=1y=1, and t=14t=\frac14, the left side of the inequality is f(−12)=18f(-\frac12)=\frac18, while the right side is −14-\frac14. So, ff isn’t convex.

Activity 1


Local Minimums are Global Minimums

What does this have to do with gradient descent?

Fact: For a convex function, every local minimum is a global minimum.

Suppose ff is convex, and suppose x⃗∗\vec x^* is a local minimum. We’ll prove by contradiction that x⃗∗\vec x^* is a global minimum.

Take any other input z⃗\vec z. Suppose it has a smaller function value,

f(z⃗)<f(x⃗∗)f(\vec z)<f(\vec x^*)

Consider the line segment from (x⃗∗,f(x⃗∗))(\vec x^*,f(\vec x^*)) to (z⃗,f(z⃗))(\vec z,f(\vec z)). Since ff is convex, the graph lies on or below this segment. For any 0<t<10<t<1,

f((1−t)x⃗∗+tz⃗)≤(1−t)f(x⃗∗)+tf(z⃗)<(1−t)f(x⃗∗)+tf(x⃗∗)=f(x⃗∗)\begin{aligned} f((1-t)\vec x^*+t\vec z)&\leq(1-t)f(\vec x^*)+tf(\vec z)\\ &<(1-t)f(\vec x^*)+tf(\vec x^*)\\ &=f(\vec x^*) \end{aligned}

The strict inequality follows from our assumption that f(z⃗)<f(x⃗∗)f(\vec z)<f(\vec x^*) and the fact that t>0t>0.

Why is this a contradiction? By choosing t>0t>0 as small as we want, the input (1−t)x⃗∗+tz⃗(1-t)\vec x^*+t\vec z can be made arbitrarily close to x⃗∗\vec x^*. But the inequality says its function value is smaller than f(x⃗∗)f(\vec x^*). This contradicts the assumption that x⃗∗\vec x^* is a local minimum.

So no input z⃗\vec z can have a smaller function value, and x⃗∗\vec x^* is a global minimum.

This doesn’t mean that a convex function must have a global minimum. For instance, f(x)=exf(x)=e^x is convex, but exe^x approaches 0 as x→−∞x\to-\infty without ever reaching 0.

Loading...

Strict Convexity

A convex function can have more than one minimizer. For example, the function below has a minimum value of 0, attained at every x∈[−1,1]x\in[-1,1].

Loading...

To rule out multiple minimizers, we can strengthen the definition of convexity.

We exclude t=0t=0 and t=1t=1 because the two sides are equal at the endpoints. Between the endpoints, the segment must lie strictly above the graph.

Fact: A strictly convex function has at most one global minimizer.

To prove this, suppose there are two different global minimizers, x⃗∗\vec x^* and y⃗∗\vec y^*, with the same minimum value mm. For any t∈(0,1)t\in(0,1), strict convexity gives

f((1−t)x⃗∗+ty⃗∗)<(1−t)f(x⃗∗)+tf(y⃗∗)=(1−t)m+tm=mf((1-t)\vec x^*+t\vec y^*)<(1-t)f(\vec x^*)+tf(\vec y^*)=(1-t)m+tm=m

This is a contradiction, since mm is the global minimum value. So, if a strictly convex function has a global minimizer, it is unique. A minimizer still doesn’t have to exist, as the example f(x)=exf(x)=e^x shows.

Activity 2

Strict Convexity and Mean Squared Error

Recall that the mean squared error for a multiple linear regression model is

Rsq(w⃗)=1n∥y⃗−Xw⃗∥2R_\text{sq}(\vec w)=\frac1n\lVert\vec y-X\vec w\rVert^2

When the columns of XX are linearly independent, there is a unique optimal parameter vector,

w⃗∗=(XTX)−1XTy⃗\vec w^*=(X^TX)^{-1}X^T\vec y

When the columns are linearly dependent, there are infinitely many optimal parameter vectors. We saw this in Chapter 6.4.

Let’s compare the mean squared error in two examples. On the left, we’ll use

X=[1001112−1],y⃗=[1−103],w⃗∗=[1−1]X=\begin{bmatrix}1&0\\0&1\\1&1\\2&-1\end{bmatrix},\qquad\vec y=\begin{bmatrix}1\\-1\\0\\3\end{bmatrix},\qquad\vec w^*=\begin{bmatrix}1\\-1\end{bmatrix}

On the right, we’ll use

X=[122436],y⃗=[123],w⃗∗=[10]X=\begin{bmatrix}1&2\\2&4\\3&6\end{bmatrix},\qquad\vec y=\begin{bmatrix}1\\2\\3\end{bmatrix},\qquad\vec w^*=\begin{bmatrix}1\\0\end{bmatrix}

In each case, the given w⃗∗\vec w^* has an MSE of 0, because y⃗∈colsp(X)\vec y\in\text{colsp}(X) and Xw⃗∗=y⃗X\vec w^*=\vec y, so there is no error. The columns of XX on the left are linearly independent, while the columns of XX on the right are linearly dependent.

Drag cc to change the parameter vector in each plot according to

w⃗(c)=w⃗∗+c[−21]\vec w(c)=\vec w^*+c\begin{bmatrix}-2\\1\end{bmatrix}

The MSE changes on the left, but stays at 0 on the right. Why?

Loading...

On the right, the direction v⃗=[−21]\vec v=\begin{bmatrix}-2\\1\end{bmatrix} is in nullsp(X)\text{nullsp}(X), so

X(w⃗∗+cv⃗)=Xw⃗∗+cXv⃗=Xw⃗∗X(\vec w^*+c\vec v)=X\vec w^*+cX\vec v=X\vec w^*

Changing cc changes the parameters, but not the predictions or the mean squared error. This is a convex function with a whole line of minimizers. On the left, the mean squared error is strictly convex, and w⃗∗\vec w^* is the unique minimizer.


The Hessian matrix

Let’s return to the second derivative test from calculus. For a twice continuously differentiable scalar-to-scalar function f:R→Rf:\mathbb{R}\to\mathbb{R}, ff is convex if and only if f′′(x)≥0f''(x)\geq0 for every xx.

For vector-to-scalar functions, we need to consider second partial derivatives. For example, suppose

f(x1,x2)=x12+x1x2+2x22f(x_1,x_2)=x_1^2+x_1x_2+2x_2^2

Its gradient is

∇f(x⃗)=[2x1+x2x1+4x2]\nabla f(\vec x)=\begin{bmatrix}2x_1+x_2\\x_1+4x_2\end{bmatrix}

Each component of the gradient is a scalar-valued function of x1x_1 and x2x_2. We can take the partial derivatives of each component:

∂∂x1(2x1+x2)=2,∂∂x2(2x1+x2)=1\frac{\partial}{\partial x_1}(2x_1+x_2)=2,\qquad\frac{\partial}{\partial x_2}(2x_1+x_2)=1
∂∂x1(x1+4x2)=1,∂∂x2(x1+4x2)=4\frac{\partial}{\partial x_1}(x_1+4x_2)=1,\qquad\frac{\partial}{\partial x_2}(x_1+4x_2)=4

We arrange these second partial derivatives in a matrix called the Hessian matrix.

For the example above,

Hf(x⃗)=[2114]H_f(\vec x)=\begin{bmatrix}2&1\\1&4\end{bmatrix}

For our purposes, the Hessian matrix is always symmetric: multivariable calculus rules tell us that ∂2f∂xi∂xj=∂2f∂xj∂xi\frac{\partial^2 f}{\partial x_i\partial x_j}=\frac{\partial^2 f}{\partial x_j\partial x_i}. For now, we’ll take this as fact. There are edge cases where the second partial derivatives aren’t continuous and this equality can fail, but we won’t dwell on them here.

In this case, the Hessian doesn’t depend on x⃗\vec x. For more sophisticated functions, it could.

Convexity and the Hessian

For a scalar-to-scalar function, we check that f′′(x)≥0f''(x)\geq0. For a vector-to-scalar function, we can’t just check that each entry of the Hessian is nonnegative. Instead, we need to check a quadratic form.

Fact: A twice continuously differentiable function f:Rd→Rf:\mathbb{R}^d\to\mathbb{R} is convex if and only if

v⃗THf(x⃗)v⃗≥0for every x⃗∈Rd and every v⃗∈Rd\vec v^T{\color{#3d81f6}H_f(\vec x)}\vec v\geq0\qquad\text{for every }\vec x\in\mathbb{R}^d\text{ and every }\vec v\in\mathbb{R}^d

If the inequality is strict for every x⃗\vec x and every nonzero v⃗\vec v, then ff is strictly convex.

Remember that v⃗TAv⃗\vec v^TA\vec v is called a quadratic form. For a fixed input x⃗\vec x, the condition that v⃗THf(x⃗)v⃗\vec v^TH_f(\vec x)\vec v returns a nonnegative result for every possible v⃗\vec v means that Hf(x⃗)H_f(\vec x) is positive semidefinite. The convexity test requires this at every input x⃗\vec x.

We’ll take this convexity test as fact for now. We will revisit this idea in Chapter 9.6, when we study eigenvalues and eigenvectors.

Example: Checking Convexity

Consider the function

g(x1,x2)=x12+4x1x2+x22g(x_1,x_2)=x_1^2+4x_1x_2+x_2^2

Its Hessian is [2442]\begin{bmatrix}2&4\\4&2\end{bmatrix}. All of its entries are positive, but if we choose v⃗=[1−1]\vec v=\begin{bmatrix}1\\-1\end{bmatrix}, then

v⃗THgv⃗=[1−1][2442][1−1]=−4\vec v^T{\color{#3d81f6}H_g}\vec v=\begin{bmatrix}1&-1\end{bmatrix}{\color{#3d81f6}\begin{bmatrix}2&4\\4&2\end{bmatrix}}\begin{bmatrix}1\\-1\end{bmatrix}=-4

So, gg isn’t convex. We can also see this by restricting gg to the line x2=−x1x_2=-x_1:

g(s,−s)=−2s2g(s,-s)=-2s^2

Compare this to the earlier function f(x1,x2)=x12+x1x2+2x22f(x_1,x_2)=x_1^2+x_1x_2+2x_2^2. The plots below show their graphs in R3\mathbb{R}^3. You can rotate each plot to examine its shape. The first is a convex bowl, while the second is a saddle.

Loading...

Example: Mean Squared Error

In Chapter 8.2, we found that

∇Rsq(w⃗)=2n(XTXw⃗−XTy⃗)\nabla R_\text{sq}(\vec w)=\frac2n(X^TX\vec w-X^T\vec y)

How do we find the Hessian? The gradient is a vector-to-vector function, so we can’t apply the gradient rules from Chapter 8.2 directly to this entire expression. Instead, we’ll take the partial derivatives of its components and stack those into a matrix, as we did above. You can think of this as taking the gradient of each component of ∇Rsq(w⃗)\nabla R_\text{sq}(\vec w) and writing its transpose as a row of the Hessian.

To simplify the notation, let A=XTXA=X^TX and b⃗=XTy⃗\vec b=X^T\vec y. These are both constant with respect to w⃗\vec w, so

∇Rsq(w⃗)=2n(Aw⃗−b⃗)\nabla R_\text{sq}(\vec w)=\frac2n(A\vec w-\vec b)

If XX has dd columns, then AA has shape d×dd\times d, and w⃗\vec w and b⃗\vec b each have dd components. Let’s expand the matrix-vector product:

Aw⃗=[A11A12⋯A1dA21A22⋯A2d⋮⋮⋱⋮Ad1Ad2⋯Add][w1w2⋮wd]=[A11w1+A12w2+⋯+A1dwdA21w1+A22w2+⋯+A2dwd⋮Ad1w1+Ad2w2+⋯+Addwd]A\vec w= \begin{bmatrix} A_{11}&A_{12}&\cdots&A_{1d}\\ A_{21}&A_{22}&\cdots&A_{2d}\\ \vdots&\vdots&\ddots&\vdots\\ A_{d1}&A_{d2}&\cdots&A_{dd} \end{bmatrix} \begin{bmatrix}w_1\\w_2\\\vdots\\w_d\end{bmatrix} = \begin{bmatrix} A_{11}w_1+A_{12}w_2+\cdots+A_{1d}w_d\\ A_{21}w_1+A_{22}w_2+\cdots+A_{2d}w_d\\ \vdots\\ A_{d1}w_1+A_{d2}w_2+\cdots+A_{dd}w_d \end{bmatrix}

The iith component is the dot product of row ii of AA with w⃗\vec w:

(Aw⃗)i=Ai1w1+Ai2w2+⋯+Aidwd(A\vec w)_i=A_{i1}w_1+A_{i2}w_2+\cdots+A_{id}w_d

To get component ii of the gradient, subtract component ii of b⃗\vec b and multiply by 2n\frac2n. By the definition of the gradient, this component is ∂Rsq∂wi\frac{\partial R_\text{sq}}{\partial w_i}, so

∂Rsq∂wi=2n(Ai1w1+Ai2w2+⋯+Aidwd−bi)\frac{\partial R_\text{sq}}{\partial w_i}=\frac2n\left(A_{i1}w_1+A_{i2}w_2+\cdots+A_{id}w_d-b_i\right)

Taking the partial derivative of this scalar expression with respect to wjw_j leaves only the coefficient of wjw_j:

∂∂wj(∂Rsq∂wi)=2nAij\frac{\partial}{\partial w_j}\left(\frac{\partial R_\text{sq}}{\partial w_i}\right)=\frac2nA_{ij}

This gives us entry (i,j)(i,j) of the Hessian. Since it holds for every ii and jj, we have

HRsq(w⃗)=2nA=2nXTXH_{R_\text{sq}}(\vec w)=\frac2nA=\frac2nX^TX

Now, for any v⃗\vec v,

v⃗THRsqv⃗=2nv⃗TXTXv⃗=2n∥Xv⃗∥2≥0\vec v^TH_{R_\text{sq}}\vec v=\frac2n\vec v^TX^TX\vec v=\frac2n\lVert X\vec v\rVert^2\geq0

A squared norm can’t be negative, so the mean squared error for a linear model is convex in w⃗\vec w.

If XX has linearly independent columns, then Xv⃗≠0⃗X\vec v\ne\vec0 for every nonzero v⃗\vec v. The squared norm is strictly positive, which makes the mean squared error strictly convex. Its minimizer exists because projecting y⃗\vec y onto colsp(X)\text{colsp}(X) gives a closest prediction vector, and independent columns give a unique parameter vector for that prediction.

If the columns are dependent, there is a nonzero v⃗∈nullsp(X)\vec v\in\text{nullsp}(X). Moving along this direction leaves the predictions and mean squared error unchanged, as we saw in the slider example. The mean squared error is convex, but not strictly convex.


Aside: Tangent Hyperplanes

For a differentiable scalar-to-scalar function, there’s another way to check convexity: the graph must lie on or above every tangent line.

For vector-to-scalar functions, the tangent line becomes a tangent hyperplane. When there are two input variables, this is a plane in three dimensions.

The figure below uses f(x1,x2)=x12+12x1x2+x22+1f(x_1,x_2)=x_1^2+\frac12x_1x_2+x_2^2+1. Drag the slider to change the point of tangency,

a⃗=[a1−1]\vec a=\begin{bmatrix}a_1\\-1\end{bmatrix}

You can also rotate the plot. The orange plane touches the blue surface but never rises above it.

Loading...

Suppose we fix a point a⃗\vec a. The tangent hyperplane to ff at a⃗\vec a is the linear approximation

La⃗(x⃗)=f(a⃗)+∇f(a⃗)T(x⃗−a⃗)L_{\vec a}(\vec x) = f(\vec a) + \nabla f(\vec a)^T (\vec x - \vec a)

This is the vector-to-scalar version of the tangent-line formula from calculus.

The graph of a differentiable convex function lies on or above every tangent hyperplane.

This gives us another useful result. If ∇f(x⃗∗)=0⃗\nabla f(\vec x^*)=\vec0, then the inequality becomes

f(y⃗)≥f(x⃗∗)for every y⃗f(\vec y)\geq f(\vec x^*)\qquad\text{for every }\vec y

Fact: For a differentiable convex function, any point with zero gradient is a global minimizer. For a general differentiable function, a zero gradient could instead indicate a local maximum or a saddle point.

That still doesn’t let us choose an arbitrary step size for gradient descent. For example, on the strictly convex function f(x)=x2f(x)=x^2, the update is x(k+1)=(1−2α)x(k)x^{(k+1)}=(1-2\alpha)x^{(k)}. Starting from x(0)=1x^{(0)}=1, a step size of α=1\alpha=1 makes the iterates alternate between 1 and -1 forever. Convexity tells us about the function’s minima, but the choice of step size still matters.