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.
- 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.
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 is a set such that for any two points and any , the point
also lies in . 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 , defined on a convex set , satisfies for all and :
This says the chord connecting two points on the graph of never dips below the graph itself — the function is "bowl-shaped." If the inequality is strict whenever and , is strictly convex, ruling out flat segments and guaranteeing a unique minimizer when one exists. A function is concave if is convex (the graph bulges upward, like a dome). The domain must itself be convex for the definition to make sense, since we need 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 , , (any norm is convex), and (the hinge loss). Common convex sets include the entire space , any linear subspace, any half-space , and any norm ball .
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 is differentiable on a convex set , then is convex on if and only if for all :
This says the tangent hyperplane at any point lies entirely below (or touches) the graph of — 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 is twice differentiable on an open convex set , then is convex on if and only if its Hessian matrix is positive semi-definite (PSD) for every , meaning
In one dimension this collapses to the familiar everywhere. If the Hessian is positive definite ( for all ), 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 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.
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 — this is not a finite algorithm at all. The practical cost comes from the certificates instead:
- Composition rules (sum, affine composition, pointwise max) cost per rule application, since they are syntactic checks on how the function is built from known convex pieces.
- Second-order (Hessian) test costs to write down the Hessian for , plus the cost of an eigenvalue/PSD check, which is via Cholesky decomposition or eigendecomposition. For many losses of interest, the Hessian has closed-form structure (e.g., 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 holds for all . Pick any and , and let . Apply the tangent inequality at with target points and :
Multiply the first by , the second by , and add:
But by the definition of . So the gradient term vanishes, leaving
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 is convex on convex domain , and is a local minimum: there is a neighborhood around where for all . We show for every , not just nearby ones. Take any . By convexity of , the segment from to stays in . For small enough , the point lies inside (since as ). Local optimality gives:
where the last step is the convexity inequality. Rearranging:
Since was arbitrary, is a global minimum. 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 with and weight vector :
We verify convexity in by computing gradient and Hessian, tracing the algebra step by step.
Step 1: reduce to a 1-D composition. Let (an affine function of ), and define
Since is composed with an affine map, is convex in if is convex in (affine composition preserves convexity — no need to redo the multivariable Hessian test).
Step 2: compute . Using :
Step 3: compute .
Step 4: check the sign. Since for all finite , we have always. So everywhere, meaning is strictly convex in , hence is convex in (though not strictly, since the affine map can be non-injective when ).
Step 5: numeric trace. Let , , and compare two candidate weight vectors and .
| quantity | midpoint | ||
|---|---|---|---|
The chord midpoint value is . The actual function value at the midpoint weight is . Since , 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 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 . Gradient descent starting from any initialization and converging to a point with 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.
Common pitfalls & traps
| pitfall | why it happens | fix |
|---|---|---|
| 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 convex | is convex only if is PSD; e.g. gives a saddle, not a bowl | Always check the Hessian's eigenvalues, not just "it's a quadratic" |
| Forgetting strictness matters for uniqueness | A convex (not strictly convex) function can have a flat region with infinitely many global minimizers | Check for strict convexity (positive definite, not just semi-definite, Hessian) if you need a unique solution |
| Applying composition rules backwards | with convex and convex is not generally convex unless is also non-decreasing | Verify monotonicity of the outer function before applying the composition rule |
| Believing convexity implies easy/fast convergence | Convexity guarantees correctness (no bad local minima) but says nothing about convergence rate without extra assumptions like strong convexity or Lipschitz gradients | Separately 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
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 is convex if and only if the matrix is positive semi-definite, meaning all its eigenvalues are non-negative. If has a negative eigenvalue, the function curves downward in that eigenvector's direction, producing a saddle shape rather than a bowl. Least-squares loss expands into a quadratic in with , which is always PSD (since for any ), 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 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 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.