Convex Functions and Convex Sets

Imagine training a linear regression model. You might worry that gradient descent gets stuck at a bad local minimum. For a huge and important class of problems, this worry is unfounded. The Loss Function being minimized is convex. So every local minimum is automatically a global minimum. Convexity is the single structural property that matters here. It separates optimization problems we can solve reliably from hard ones. In some problems, even finding a decent answer is hard. Convex functions and convex sets form the backbone of machine learning theory. Linear regression, ridge regression, and logistic regression reduce to convex optimization problems. So do support vector machines and LASSO. This is precisely why these methods come with strong guarantees. Given enough compute, they provably find the best possible fit. This holds true for the training objective. Even in deep learning, the loss surface is famously non-convex. Still, convex analysis supplies useful vocabulary and diagnostic tools. These include Lipschitz gradients, strong convexity, and duality. Researchers use these tools to understand training algorithms. They also use them to accelerate algorithms like Gradient Descent. This article develops convexity from first principles. We cover what makes a set convex. We cover what makes a function convex. We connect these definitions to derivatives a calculus student already knows. We build up first-order and second-order tests for convexity. Then we explain why convex functions cannot have "fake" local minima. We use an actual proof sketch rather than a hand-wave. Along the way, we build geometric intuition. Convex functions are bowl-shaped. Convex sets have no dents. This intuition will make later material on Convex Optimization Problems feel inevitable, not mysterious. By the end, you should recognize convex functions on sight. You should verify convexity using the Hessian. You should explain geometrically why gradient descent on a convex loss avoids spurious minima. You should articulate the tradeoffs of convexity as a modeling assumption. This is foundational machinery. Nearly every convergence guarantee later in the optimization curriculum leans on the definitions built here.

Key takeaways
  • A set is convex if the line segment between any two of its points stays inside the set.
  • A function is convex if the segment connecting any two points on its graph lies on or above the graph (it never dips below).
  • First-order condition: a differentiable function is convex iff its tangent plane is always a global underestimate.
  • Second-order condition: a twice-differentiable function is convex iff its Hessian is positive semi-definite everywhere.
  • The key payoff: for convex functions, every local minimum is a global minimum — no "getting stuck" is possible.
  • Convexity underlies linear regression, logistic regression, SVMs, and LASSO, and motivates why gradient-based methods work reliably on these losses.
The core insight

A function is convex exactly when its graph always lies above every one of its own tangent lines, which is precisely what forbids spurious local minima.

The idea

A convex set C⊆RnC \subseteq \mathbb{R}^n is a set such that for any two points x,y∈Cx, y \in C and any λ∈[0,1]\lambda \in [0,1], the point

λx+(1−λ)y\lambda x + (1-\lambda) y

also lies in CC. Geometrically, the entire straight-line segment joining any two members of the set stays inside the set — no dents, no holes, no disconnected pieces. Balls, half-spaces, hyperplanes, boxes, and intersections of convex sets are all convex; a crescent moon or a star shape is not. A convex function f:C→Rf: C \to \mathbb{R}, defined on a convex set CC, satisfies for all x,y∈Cx, y \in C and λ∈[0,1]\lambda \in [0,1]:

f(λx+(1−λ)y)  ≤  λf(x)+(1−λ)f(y).f(\lambda x + (1-\lambda) y) \;\le\; \lambda f(x) + (1-\lambda) f(y).

This says the chord connecting two points on the graph of ff never dips below the graph itself — the function is "bowl-shaped." If the inequality is strict whenever x≠yx \ne y and λ∈(0,1)\lambda \in (0,1), ff is strictly convex, ruling out flat segments and guaranteeing a unique minimizer when one exists. A function ff is concave if −f-f is convex (the graph bulges upward, like a dome). The domain must itself be convex for the definition to make sense, since we need λx+(1−λ)y\lambda x + (1-\lambda) y to remain in the domain for the inequality to be evaluable. This is why convexity is really a joint statement about a function and the convex set it lives on. Common convex functions include f(x)=x2f(x) = x^2, f(x)=exf(x) = e^x, f(x)=∥x∥2f(x) = \|x\|_2 (any norm is convex), and f(x)=max⁡(0,1−x)f(x) = \max(0, 1 - x) (the hinge loss). Common convex sets include the entire space Rn\mathbb{R}^n, any linear subspace, any half-space {x:a⊤x≤b}\{x : a^\top x \le b\}, and any norm ball {x:∥x∥≤r}\{x : \|x\| \le r\}.

How it works

Working with the definition directly (checking the chord inequality for every pair of points) is correct but impractical. Instead, convexity is diagnosed using calculus-based tests that require checking a condition pointwise rather than over all pairs.

First-order condition. If ff is differentiable on a convex set CC, then ff is convex on CC if and only if for all x,y∈Cx, y \in C:

f(y)  ≥  f(x)+∇f(x)⊤(y−x).f(y) \;\ge\; f(x) + \nabla f(x)^\top (y - x).

This says the tangent hyperplane at any point xx lies entirely below (or touches) the graph of ff — the linear (first-order Taylor) approximation is always a global underestimate. This is the single most useful fact in convex optimization: it turns a local piece of information (the gradient at one point) into a global guarantee about the function everywhere.

Second-order condition. If ff is twice differentiable on an open convex set CC, then ff is convex on CC if and only if its Hessian matrix ∇2f(x)\nabla^2 f(x) is positive semi-definite (PSD) for every x∈Cx \in C, meaning

v⊤∇2f(x) v≥0for all vectors v.v^\top \nabla^2 f(x)\, v \ge 0 \quad \text{for all vectors } v.

In one dimension this collapses to the familiar f′′(x)≥0f''(x) \ge 0 everywhere. If the Hessian is positive definite (v⊤∇2f(x)v>0v^\top \nabla^2 f(x) v > 0 for all v≠0v \ne 0), ff is strictly convex. This is usually the most mechanical way to check convexity for a smooth function: compute the Hessian, check its eigenvalues are non-negative.

Algorithmic procedure to certify convexity of a candidate loss: ``` FUNCTION IsConvexCandidate(f, domain C):

# Step 1: confirm the domain is convex IF C is not a convex set (e.g., defined by non-linear equalities): RETURN "cannot certify via standard rules"

# Step 2: try composition rules first (cheapest) IF f = sum of known convex functions: RETURN "convex, by sum rule" IF f = affine composition g(Ax + b) with g convex: RETURN "convex, by affine composition rule" IF f = max(f_1, ..., f_k) with each f_i convex: RETURN "convex, by pointwise-max rule"

# Step 3: fall back to calculus tests compute gradient ∇f(x) symbolically or numerically compute Hessian ∇²f(x) FOR representative sample of points x in C: check eigenvalues of ∇²f(x) IF any eigenvalue < 0: RETURN "not convex (found negative curvature direction)" IF Hessian is PSD at all tested points AND this holds analytically: RETURN "convex, by second-order condition" ELSE: RETURN "inconclusive — verify analytically" ``` This "procedure" is really a checklist a practitioner runs through mentally: recognize the loss as a composition of known convex building blocks (sums of convex functions, affine substitutions, pointwise maxima of convex functions, all preserve convexity), and only compute the Hessian by hand when no shortcut rule applies. For example, ridge regression's loss ∥Xw−y∥22+λ∥w∥22\|Xw - y\|_2^2 + \lambda \|w\|_2^2 is instantly recognized as convex: it is a sum of a convex quadratic and a convex norm-squared term, with no need to ever write down a Hessian.

Figure
Convex vs. non-convex functions, and why tangents matter
Convex: f(x) = x² chord stays above curve tangent lies below curve Non-convex: has a "dip" chord dips below curve here violation point

Complexity & correctness

Complexity of checking convexity. Verifying convexity from the raw definition requires checking the chord inequality for infinitely many pairs of points and all λ∈[0,1]\lambda \in [0,1] — this is not a finite algorithm at all. The practical cost comes from the certificates instead:

  • Composition rules (sum, affine composition, pointwise max) cost O(1)O(1) per rule application, since they are syntactic checks on how the function is built from known convex pieces.
  • Second-order (Hessian) test costs O(n2)O(n^2) to write down the Hessian for x∈Rnx \in \mathbb{R}^n, plus the cost of an eigenvalue/PSD check, which is O(n3)O(n^3) via Cholesky decomposition or eigendecomposition. For many losses of interest, the Hessian has closed-form structure (e.g., X⊤XX^\top X for least squares) so this is done once, symbolically, rather than at every point.
  • In machine learning, we rarely certify convexity at runtime; it is verified once, analytically, when the loss function is designed, and then exploited by the optimizer for the lifetime of the model.

Correctness of the first-order condition (why it matches the definition). Suppose f(y)≥f(x)+∇f(x)⊤(y−x)f(y) \ge f(x) + \nabla f(x)^\top(y-x) holds for all x,y∈Cx, y \in C. Pick any x,y∈Cx, y \in C and λ∈[0,1]\lambda \in [0,1], and let z=λx+(1−λ)yz = \lambda x + (1-\lambda) y. Apply the tangent inequality at zz with target points xx and yy:

f(x)≥f(z)+∇f(z)⊤(x−z),f(y)≥f(z)+∇f(z)⊤(y−z).f(x) \ge f(z) + \nabla f(z)^\top (x - z), \qquad f(y) \ge f(z) + \nabla f(z)^\top (y - z).

Multiply the first by λ\lambda, the second by (1−λ)(1-\lambda), and add:

λf(x)+(1−λ)f(y)  ≥  f(z)+∇f(z)⊤(λ(x−z)+(1−λ)(y−z)).\lambda f(x) + (1-\lambda) f(y) \;\ge\; f(z) + \nabla f(z)^\top\big(\lambda(x-z) + (1-\lambda)(y - z)\big).

But λ(x−z)+(1−λ)(y−z)=λx+(1−λ)y−z=0\lambda(x - z) + (1-\lambda)(y-z) = \lambda x + (1-\lambda) y - z = 0 by the definition of zz. So the gradient term vanishes, leaving

λf(x)+(1−λ)f(y)≥f(z)=f(λx+(1−λ)y),\lambda f(x) + (1-\lambda) f(y) \ge f(z) = f(\lambda x + (1-\lambda) y),

which is exactly the definition of convexity. The converse direction (convexity implies the tangent inequality) follows by taking the limit of the chord slope as one endpoint approaches the other, using the definition of the derivative. This proves the two definitions are equivalent, not merely correlated.

Correctness of "local minimum implies global minimum." This is the central theorem that justifies using convexity as a design principle for loss functions. Suppose ff is convex on convex domain CC, and x∗x^* is a local minimum: there is a neighborhood UU around x∗x^* where f(x∗)≤f(x)f(x^*) \le f(x) for all x∈U∩Cx \in U \cap C. We show f(x∗)≤f(x)f(x^*) \le f(x) for every x∈Cx \in C, not just nearby ones. Take any x∈Cx \in C. By convexity of CC, the segment from x∗x^* to xx stays in CC. For small enough λ∈(0,1)\lambda \in (0,1), the point zλ=x∗+λ(x−x∗)z_\lambda = x^* + \lambda(x - x^*) lies inside U∩CU \cap C (since zλ→x∗z_\lambda \to x^* as λ→0\lambda \to 0). Local optimality gives:

f(x∗)≤f(zλ)=f((1−λ)x∗+λx)≤(1−λ)f(x∗)+λf(x)f(x^*) \le f(z_\lambda) = f\big((1-\lambda)x^* + \lambda x\big) \le (1-\lambda) f(x^*) + \lambda f(x)

where the last step is the convexity inequality. Rearranging:

f(x∗)≤(1−λ)f(x∗)+λf(x)  ⟹  λf(x∗)≤λf(x)  ⟹  f(x∗)≤f(x).f(x^*) \le (1-\lambda) f(x^*) + \lambda f(x) \;\Longrightarrow\; \lambda f(x^*) \le \lambda f(x) \;\Longrightarrow\; f(x^*) \le f(x).

Since x∈Cx \in C was arbitrary, x∗x^* is a global minimum. ■\blacksquare This is the entire reason convexity matters for Gradient Descent: any algorithm that finds a point with zero gradient (or, for constrained problems, a point satisfying the KKT conditions) on a convex objective has found the answer, not merely an answer. There is no landscape of decoys to worry about.

Honest tradeoffs. Convexity is a strong assumption, and most interesting models (neural networks, mixtures, most probabilistic graphical models with latent variables) are not convex in their parameters. Enforcing convexity often means giving up expressiveness — a linear model is convex in its weights, but a neural network is not, precisely because it composes nonlinear functions with themselves in ways that destroy the convexity of the composite. Convexity also does not by itself say anything about the speed of convergence; for that, you additionally need properties like Lipschitz-continuous gradients or strong convexity (a stronger form where the function curves upward at least quadratically), which give explicit convergence rates for gradient descent.

Worked example

Consider the logistic regression loss for a single training example (xi,yi)(x_i, y_i) with yi∈{0,1}y_i \in \{0, 1\} and weight vector w∈Rdw \in \mathbb{R}^d:

ℓ(w)=−yilog⁡σ(w⊤xi)−(1−yi)log⁡(1−σ(w⊤xi)),σ(t)=11+e−t.\ell(w) = -y_i \log \sigma(w^\top x_i) - (1 - y_i)\log(1 - \sigma(w^\top x_i)), \qquad \sigma(t) = \frac{1}{1 + e^{-t}}.

We verify convexity in ww by computing gradient and Hessian, tracing the algebra step by step.

Step 1: reduce to a 1-D composition. Let t=w⊤xit = w^\top x_i (an affine function of ww), and define

g(t)=−yilog⁡σ(t)−(1−yi)log⁡(1−σ(t)).g(t) = -y_i \log \sigma(t) - (1-y_i)\log(1-\sigma(t)).

Since ℓ(w)=g(w⊤xi)\ell(w) = g(w^\top x_i) is gg composed with an affine map, ℓ\ell is convex in ww if gg is convex in tt (affine composition preserves convexity — no need to redo the multivariable Hessian test).

Step 2: compute g′(t)g'(t). Using σ′(t)=σ(t)(1−σ(t))\sigma'(t) = \sigma(t)(1-\sigma(t)):

g′(t)=−yi(1−σ(t))+(1−yi)σ(t)=σ(t)−yi.g'(t) = -y_i (1 - \sigma(t)) + (1-y_i)\sigma(t) = \sigma(t) - y_i.

Step 3: compute g′′(t)g''(t).

g′′(t)=σ′(t)=σ(t)(1−σ(t)).g''(t) = \sigma'(t) = \sigma(t)\big(1 - \sigma(t)\big).

Step 4: check the sign. Since σ(t)∈(0,1)\sigma(t) \in (0,1) for all finite tt, we have σ(t)(1−σ(t))>0\sigma(t)(1-\sigma(t)) > 0 always. So g′′(t)>0g''(t) > 0 everywhere, meaning gg is strictly convex in tt, hence ℓ(w)\ell(w) is convex in ww (though not strictly, since the affine map w↦w⊤xiw \mapsto w^\top x_i can be non-injective when d>1d>1).

Step 5: numeric trace. Let xi=(2,−1)x_i = (2, -1), yi=1y_i = 1, and compare two candidate weight vectors wa=(0.1,0.2)w_a = (0.1, 0.2) and wb=(1.0,−0.5)w_b = (1.0, -0.5).

quantitywa=(0.1,0.2)w_a = (0.1, 0.2)wb=(1.0,−0.5)w_b = (1.0, -0.5)midpoint wm=(0.55,−0.15)w_m = (0.55, -0.15)
t=w⊤xit = w^\top x_i2(0.1)−1(0.2)=0.02(0.1) - 1(0.2) = 0.02(1.0)−1(−0.5)=2.52(1.0) -1(-0.5) = 2.52(0.55)−1(−0.15)=1.252(0.55) - 1(-0.15) = 1.25
σ(t)\sigma(t)0.50.50.9240.9240.7770.777
ℓ(w)=−log⁡σ(t)\ell(w) = -\log\sigma(t)0.6930.6930.0790.0790.2520.252

The chord midpoint value is 12(0.693)+12(0.079)=0.386\tfrac{1}{2}(0.693) + \tfrac{1}{2}(0.079) = 0.386. The actual function value at the midpoint weight is ℓ(wm)=0.252\ell(w_m) = 0.252. Since 0.252≤0.3860.252 \le 0.386, the chord lies above the function at this sampled point — consistent with (though not a proof of) convexity, exactly as the theory predicts.

Step 6: consequence for training. Because ℓ\ell is convex (and the sum of convex functions over all training examples is convex, by the sum rule), the full logistic regression loss over a dataset is convex in ww. Gradient descent starting from any initialization w0w_0 and converging to a point with ∇L(w)≈0\nabla L(w) \approx 0 has found the global minimum of the training loss — this is why logistic regression training is considered "solved" in a way that deep network training is not.

Figure
Convex loss landscape: any local dip is the global dip
Logistic loss surface (schematic, 1-D slice) unique global minimum w* gradient descent start A start B Both trajectories converge to the SAME minimum, regardless of start

Common pitfalls & traps

pitfallwhy it happensfix
Confusing "the domain is convex" with "the function is convex"Both use the word "convex" but describe different objects (a set vs. a function)Always ask separately: is the domain a convex set, and separately, does the function satisfy the chord inequality on it
Assuming any quadratic is convexf(x)=x⊤Axf(x) = x^\top A x is convex only if AA is PSD; e.g. A=(100−1)A = \begin{pmatrix}1&0\\0&-1\end{pmatrix} gives a saddle, not a bowlAlways check the Hessian's eigenvalues, not just "it's a quadratic"
Forgetting strictness matters for uniquenessA convex (not strictly convex) function can have a flat region with infinitely many global minimizersCheck for strict convexity (positive definite, not just semi-definite, Hessian) if you need a unique solution
Applying composition rules backwardsg(f(x))g(f(x)) with ff convex and gg convex is not generally convex unless gg is also non-decreasingVerify monotonicity of the outer function before applying the composition rule
Believing convexity implies easy/fast convergenceConvexity guarantees correctness (no bad local minima) but says nothing about convergence rate without extra assumptions like strong convexity or Lipschitz gradientsSeparately check curvature bounds if you need rate guarantees, not just existence of a global optimum

Pioneers & history

The systematic mathematical study of convex sets and functions matured in the early-to-mid twentieth century, driven by connections to geometry, economics, and — eventually — computation.

Origins · 1 minWerner Fenchel and how this was discovered
WF
Werner Fenchel
1905–1988 · Developed convex duality theory and the Legendre–Fenchel transform, connecting convex functions to their dual representations

History
1905
Minkowski's earlier work on convex bodies establishes the geometric foundations later formalized by convex analysis.
1949
Fenchel's lecture notes on convex functions formalize conjugate duality, a cornerstone of modern convex optimization.
1951
Kuhn and Tucker publish optimality conditions for constrained convex optimization, generalizing Lagrange multipliers.
1970
Rockafellar's Convex Analysis becomes the standard rigorous reference, unifying decades of scattered results.
1990
s: Interior-point methods (Nesterov, Nemirovski) make large-scale convex optimization computationally practical, enabling SVMs and LASSO at scale.

Convex analysis was, for decades, a subject pursued mostly by mathematicians interested in geometry and economics (convex sets model resource constraints and utility functions naturally). Its explosive relevance to machine learning arrived once researchers realized that many core estimation problems — least squares, logistic regression, support vector machines — are convex programs in disguise, meaning decades of theory about duality, optimality conditions, and efficient algorithms could be imported wholesale into statistics and learning theory.

Where this matters

Convexity is not an abstract nicety; it is load-bearing infrastructure in production machine learning systems. Linear and logistic regression solvers in libraries like scikit-learn, Vowpal Wabbit, and glmnet rely on convexity to guarantee that Newton's method, L-BFGS, or coordinate descent converges to the actual optimal fit, not an arbitrary stationary point. Support vector machines are formulated explicitly as convex quadratic programs, which is why SVM training has reliable, reproducible solvers (SMO, libsvm) rather than requiring dozens of random restarts. LASSO and ridge regression, workhorses of high-dimensional statistics and feature selection pipelines, are convex programs whose sparsity or shrinkage properties are provable precisely because of convex duality. In deep learning, individual layers and losses are analyzed through a convex lens even though the full network is not convex: cross-entropy loss is convex in the logits, hinge loss is convex, and the softmax layer's log-partition function is convex, all of which stabilizes training even inside a globally non-convex optimization. Convex relaxations — replacing a hard combinatorial or non-convex problem with the closest convex approximation — power compressed sensing, low-rank matrix completion (recommender systems like the Netflix Prize solutions), and robust statistics. Portfolio optimization in finance, control system design in robotics, and network resource allocation in distributed systems all reduce, at their core, to convex programs solved by algorithms whose correctness rests on exactly the definitions and theorems in this article.

Ask the tutor: How does strong convexity change the convergence rate guarantees for gradient descent compared to plain convexity?

What is the difference between a convex function and a convex set?

A convex set is a geometric object: a region in space where the line segment between any two of its points stays inside the region, like a disk or a half-space. A convex function is a mapping whose graph curves upward everywhere, formalized by the chord inequality. The two concepts are related but distinct — a convex function's domain must be a convex set for the definition to make sense, and the region above the graph of a convex function (its "epigraph") is itself always a convex set. This connection is why the two ideas share the word "convex" and are usually taught together.

Is every quadratic function convex?

No. A quadratic f(x)=x⊤Ax+b⊤x+cf(x) = x^\top A x + b^\top x + c is convex if and only if the matrix AA is positive semi-definite, meaning all its eigenvalues are non-negative. If AA has a negative eigenvalue, the function curves downward in that eigenvector's direction, producing a saddle shape rather than a bowl. Least-squares loss ∥Xw−y∥22\|Xw - y\|_2^2 expands into a quadratic in ww with A=X⊤XA = X^\top X, which is always PSD (since v⊤X⊤Xv=∥Xv∥2≥0v^\top X^\top X v = \|Xv\|^2 \ge 0 for any vv), which is exactly why linear regression is always a convex problem regardless of the data.

Does convexity guarantee fast convergence for gradient descent?

No — convexity alone only guarantees that gradient descent (run correctly, with a suitable step size) will not get trapped in a spurious local minimum; it says nothing about how many iterations are needed. Convergence rate guarantees require additional structure, most commonly a Lipschitz-continuous gradient (bounding how fast the gradient can change) and, for faster linear-rate convergence, strong convexity (a lower bound on curvature). Plain convexity without these extra assumptions can still lead to very slow, sublinear convergence in the worst case, even though the destination is guaranteed to be correct.

Why is deep learning training not covered by this theory, if it works so well in practice?

The loss of a deep neural network, viewed as a function of all its weights, is generally non-convex because composing multiple nonlinear layers (matrix multiplications interleaved with nonlinearities like ReLU or sigmoid) destroys the convexity that each piece might individually have. This means the global-minimum guarantee proven in this article does not apply, and in principle gradient descent could stop at a poor local minimum or saddle point. In practice, empirical and theoretical work suggests that many saddle points and local minima in large overparameterized networks are surprisingly benign, but this is an active research area, not a settled convex-analysis guarantee.

How do I check convexity for a function I've never seen before?

Start with composition rules before touching calculus: is your function a sum of known convex functions, an affine substitution into a convex function, or a pointwise maximum of convex functions? These operations preserve convexity and cover the vast majority of losses used in practice. If no rule applies, fall back to computing the Hessian and checking that it is positive semi-definite everywhere in the domain — for a one-dimensional function, this just means checking that the second derivative is non-negative for all inputs in the domain.

Can a convex function have multiple global minimizers?

Yes, if it is convex but not strictly convex. A flat-bottomed bowl, like f(x)=max⁡(0,x−1)f(x) = \max(0, x-1) restricted to a bounded interval containing a flat segment, can have infinitely many points achieving the same minimum value. Strict convexity (Hessian positive definite, not merely semi-definite, or strict inequality in the chord condition) rules this out and guarantees a unique minimizer whenever a minimum exists. This distinction matters in practice: ridge regression's added ℓ2\ell_2 penalty strictly convexifies an otherwise possibly flat least-squares loss, guaranteeing a unique solution.

What is the connection between convex sets and constrained optimization?

Many real optimization problems require the solution to satisfy constraints, such as probabilities summing to one or weights being non-negative — these constraints define a feasible region. When both the objective function and the feasible region (defined by the constraints) are convex, the overall problem is a convex program, and the global-minimum guarantee from unconstrained convex functions extends to this constrained setting via tools like the KKT conditions. This is the bridge from the material here into the broader study of Convex Optimization Problems.

Before this

Where this leads