Optimization and Calculus for Machine Learning

Convexity

Convex sets and convex functions as the geometry that makes local optimization globally reliable.

Lesson
32

Goal

Understand the geometric property that turns local downhill information into a global guarantee.

Convexity is the reason some optimization problems are unusually well behaved:

In a convex problem, a local minimum cannot secretly be worse than another distant minimum.

That does not mean optimization is always fast or numerically easy. It means the landscape has no deceptive local valleys.

Motivation

Gradient descent only sees local information.

At the current point, it asks:

Which direction decreases the loss most rapidly right now?\text{Which direction decreases the loss most rapidly right now?}

For an arbitrary nonconvex function, following local downhill directions can lead to:

Convexity removes one major source of ambiguity.

For a convex function:

These statements are different views of the same underlying shape.

Notation / Symbol Table

SymbolPlain-English nameMeaning
CCsetA collection of allowed points.
fffunctionA scalar-valued objective or loss.
x,yx,ypointsTwo points in the domain.
ttinterpolation weightA number satisfying 0t10\le t\le 1.
tx+(1t)ytx+(1-t)yconvex combinationA point on the line segment joining xx and yy.
f(x)\nabla f(x)gradientLocal first-order change at xx.
Hf(x)H_f(x)HessianLocal second-order curvature at xx.

First Idea: A Convex Set Contains Its Chords

A set CC is convex when the straight line segment between any two points in the set remains entirely inside the set.

Formally, for every x,yCx,y\in C and every t[0,1]t\in[0,1]:

tx+(1t)yCtx+(1-t)y \in C

The expression

tx+(1t)ytx+(1-t)y

interpolates between yy and xx:

Examples of convex sets

Examples of nonconvex sets

The distinction matters in constrained optimization. Even a convex objective can become difficult if the allowed region is nonconvex.

Convex Combinations

The expression

tx+(1t)ytx+(1-t)y

is called a convex combination of xx and yy.

The weights are:

tand1tt \quad\text{and}\quad 1-t

They have two properties:

t0,1t0t\ge 0, \qquad 1-t\ge 0

and:

t+(1t)=1t+(1-t)=1

This is a weighted average.

In programming terms, it is linear interpolation:

function lerp(a: number, b: number, t: number): number {
  return t * a + (1 - t) * b;
}

For vectors, the same operation is applied componentwise.

Convex Functions: The Graph Lies Below Its Chords

A function ff is convex when, for every pair of points xx and yy and every t[0,1]t\in[0,1]:

f(tx+(1t)y)tf(x)+(1t)f(y)f(tx+(1-t)y) \le t f(x)+(1-t)f(y)

This is the convexity inequality, also called the chord inequality.

Read the two sides as:

For a convex function, the graph lies at or below the straight chord joining any two graph points.

Programming analogy

There are two possible orders:

interpolate inputs, then evaluate

versus:

evaluate endpoints, then interpolate outputs

Convexity says:

f(interpolate inputs) <= interpolate f(outputs)

This inequality is the formal version of “bowl-shaped.”

Worked Example: f(x)=x2f(x)=x^2

Choose:

x=0,y=2,t=12x=0, \qquad y=2, \qquad t=\frac12

The interpolated input is:

12(0)+12(2)=1\frac12(0)+\frac12(2)=1

Evaluate the left side:

f(1)=1f(1)=1

Evaluate and interpolate the endpoint values:

12f(0)+12f(2)=12(0)+12(4)=2\frac12 f(0)+\frac12 f(2) = \frac12(0)+\frac12(4) =2

Therefore:

121\le 2

The midpoint of the curve lies below the midpoint of the chord.

One numerical example does not prove global convexity, but it illustrates the definition.

A Nonconvex Example

Consider:

f(x)=x2f(x)=-x^2

Using the same points:

f(1)=1f(1)=-1

while:

12f(0)+12f(2)=12(0)+12(4)=2\frac12 f(0)+\frac12 f(2) = \frac12(0)+\frac12(-4) =-2

Convexity would require:

12-1\le -2

which is false.

The graph lies above its chord, so x2-x^2 is concave rather than convex.

Convex Versus Strictly Convex

A function is strictly convex when the chord inequality is strict for distinct points and interior weights:

f(tx+(1t)y)<tf(x)+(1t)f(y)f(tx+(1-t)y) < t f(x)+(1-t)f(y)

for xyx\ne y and 0<t<10<t<1.

Why the distinction matters:

Example:

f(x)=x2f(x)=x^2

is strictly convex.

A constant function is convex but not strictly convex.

First-Derivative View: Tangent Lines Stay Below

For a differentiable convex function:

f(y)f(x)+f(x)T(yx)f(y) \ge f(x)+\nabla f(x)^T(y-x)

In one dimension:

f(y)f(x)+f(x)(yx)f(y) \ge f(x)+f'(x)(y-x)

The right side is the tangent-line approximation at xx.

Convexity says the tangent line never rises above the function.

This gives another geometric picture:

Every local linear approximation underestimates a convex function globally.

That is stronger than merely being bowl-shaped near one point.

Why A Stationary Point Is Global

Suppose xx^* is a stationary point:

f(x)=0\nabla f(x^*)=0

Apply the first-order convexity condition:

f(y)f(x)+f(x)T(yx)f(y) \ge f(x^*)+\nabla f(x^*)^T(y-x^*)

Since the gradient is zero:

f(y)f(x)f(y) \ge f(x^*)

for every yy.

Therefore xx^* is a global minimum.

This is the central optimization guarantee.

No search over distant regions is needed to rule out a lower valley: convexity already rules one out.

Second-Derivative View: Nonnegative Curvature

For a twice-differentiable one-dimensional function, convexity is equivalent to:

f(x)0f''(x)\ge 0

throughout the domain.

Examples:

f(x)=x2f(x)=x^2

has:

f(x)=2>0f''(x)=2>0

so it is strictly convex.

f(x)=x4f(x)=x^4

has:

f(x)=12x20f''(x)=12x^2\ge 0

so it is convex, even though the second derivative is zero at x=0x=0.

This shows an important subtlety:

Strict convexity does not require f(x)>0f''(x)>0 at every point.

Multivariable View: Positive Semidefinite Hessian

For a twice-differentiable function f:RnRf:\mathbb{R}^n\to\mathbb{R}, convexity is characterized by a positive semidefinite Hessian:

vTHf(x)v0v^T H_f(x)v \ge 0

for every point xx and every direction vector vv.

Equivalently, every eigenvalue of the Hessian is nonnegative:

λi0\lambda_i \ge 0

Interpretation:

For strict local curvature, a positive definite Hessian has:

vTHf(x)v>0v^T H_f(x)v>0

for every nonzero vv.

Then all Hessian eigenvalues are positive.

Worked Example: A Convex Quadratic

Consider:

f(x,y)=4x2+y2f(x,y)=4x^2+y^2

Its Hessian is:

H=[8002]H= \begin{bmatrix} 8 & 0\\ 0 & 2 \end{bmatrix}

The eigenvalues are 88 and 22, both positive.

Therefore the function is strictly convex.

The minimum occurs where the gradient is zero:

f(x,y)=[8x2y]=[00]\nabla f(x,y)= \begin{bmatrix} 8x\\ 2y \end{bmatrix} = \begin{bmatrix} 0\\ 0 \end{bmatrix}

which gives:

x=0,y=0x=0, \qquad y=0

Because the function is convex, this stationary point is not merely a local candidate. It is the unique global minimum.

Worked Example With A Mixed Term

Consider:

f(x,y)=x2+2xy+2y2f(x,y)=x^2+2xy+2y^2

The Hessian is:

H=[2224]H= \begin{bmatrix} 2 & 2\\ 2 & 4 \end{bmatrix}

For a symmetric 2×22\times2 matrix, positive definiteness can be checked using the leading principal minors:

H11>0H_{11}>0

and:

det(H)>0\det(H)>0

Here:

H11=2>0H_{11}=2>0

and:

det(H)=2422=4>0\det(H)=2\cdot4-2\cdot2=4>0

So the Hessian is positive definite, and the function is strictly convex.

The mixed term rotates the principal curvature directions, but it does not destroy convexity.

Convexity Is Global, Curvature Is Local

The Hessian at one point tells us local curvature.

Convexity is a statement about the entire domain.

A function can have a positive semidefinite Hessian at one point and still fail to be convex elsewhere.

To establish convexity through the Hessian, the Hessian condition must hold throughout the relevant domain.

This distinction mirrors a recurring pattern:

Common Convex Losses

Several familiar objectives are convex in their direct prediction or parameter variables under appropriate model choices:

But composition matters.

A convex loss applied to the output of a neural network does not make the training objective convex in the network parameters.

The network’s nonlinear parameterization usually creates a nonconvex objective.

What Convexity Guarantees

For a convex objective over a convex feasible set:

For a strictly convex objective:

What Convexity Does Not Guarantee

Convexity does not guarantee:

For example, a very narrow convex valley can still make gradient descent painfully slow.

Convexity removes deceptive local minima, not bad conditioning.

Connection To Gradient Descent

For a smooth convex function, gradient descent is not wandering among unrelated valleys.

Each step uses local information, but the global geometry makes that information trustworthy.

The update remains:

xt+1=xtηf(xt)x_{t+1}=x_t-\eta\nabla f(x_t)

Convexity tells us what the landscape permits.

Smoothness and curvature bounds tell us which learning rates are safe and how quickly convergence can occur.

These assumptions play different roles:

PropertyWhat it controls
ConvexityNo bad local minima.
Strict convexityAt most one minimizer.
SmoothnessGradient does not change too abruptly.
Strong convexityBowl has a minimum curvature floor.
Condition numberDifficulty caused by unequal curvature directions.

We will build these distinctions gradually rather than treating “convex” as synonymous with “easy.”

Connection To Programming

A numerical chord test can look like this:

type ScalarFunction = (x: number) => number;

function convexityGap(
  f: ScalarFunction,
  x: number,
  y: number,
  t: number,
): number {
  if (t < 0 || t > 1) {
    throw new RangeError("t must lie in [0, 1]");
  }

  const interpolatedInput = t * x + (1 - t) * y;
  const interpolatedOutput = t * f(x) + (1 - t) * f(y);

  return interpolatedOutput - f(interpolatedInput);
}

For a convex function, the returned gap should be nonnegative, up to floating-point error.

This is useful as an experiment, not as a proof. Testing finitely many points cannot establish an inequality over a continuous domain.

Why Convexity Shows Up In ML

Convexity gives a clean dividing line between two kinds of optimization reasoning.

For convex models such as linear and logistic regression, we can often prove:

For neural networks, the objective is generally nonconvex. Convex theory still matters because it provides:

Summary

A convex set contains the line segment between any two of its points:

tx+(1t)yCtx+(1-t)y\in C

A convex function lies below every chord:

f(tx+(1t)y)tf(x)+(1t)f(y)f(tx+(1-t)y) \le t f(x)+(1-t)f(y)

For differentiable convex functions, tangent planes lie below the graph:

f(y)f(x)+f(x)T(yx)f(y)\ge f(x)+\nabla f(x)^T(y-x)

For twice-differentiable functions, convexity means nonnegative curvature in every direction:

vTHf(x)v0v^T H_f(x)v\ge0

The optimization payoff is:

Every local minimum of a convex function is global.

Exercises

  1. In your own words, distinguish a convex set from a convex function.

  2. Compute tx+(1t)ytx+(1-t)y for x=2x=2, y=10y=10, and t=0.25t=0.25. Explain where the result lies on the segment.

  3. Using x=0x=0, y=4y=4, and t=12t=\frac12, check the convexity inequality numerically for f(x)=x2f(x)=x^2.

  4. Perform the same numerical check for f(x)=x2f(x)=-x^2. Which side of the inequality fails?

  5. Determine whether each set is convex and explain why:

    • a filled disk;
    • a circle containing only its boundary;
    • a rectangle;
    • two separated filled disks.
  6. For f(x)=3x24x+7f(x)=3x^2-4x+7, compute f(x)f''(x) and use it to classify the function as convex, strictly convex, or neither.

  7. For

    f(x,y)=x2+4y2f(x,y)=x^2+4y^2

    compute the Hessian and use its eigenvalues to determine whether the function is convex.

  8. Suppose a differentiable convex function has f(x)=0\nabla f(x^*)=0. Use the first-order convexity inequality to explain why xx^* is a global minimum.

  9. Explain why a convex function can still be difficult for gradient descent when one Hessian eigenvalue is much larger than another.

  10. Write TypeScript-style pseudocode that computes the convexity gap

    tf(x)+(1t)f(y)f(tx+(1t)y)t f(x)+(1-t)f(y)-f(tx+(1-t)y)

    and state what sign you expect for a convex function.

Looking Ahead

Convexity tells us that the landscape has no deceptive local minima.

The next question is:

How can we optimize when parameters must satisfy explicit restrictions?

That leads to constrained optimization, feasible sets, and Lagrange multipliers.