TL;DR
The gradient of a function is the slope of its tangent line, and it only exists where the function is smooth. A convex function can have sharp corners, such as at zero, where many lines touch the graph and none is clearly "the" tangent. A subgradient at a point is any slope of a line that passes through that point and stays below the whole function. The set of all such slopes, the subdifferential, is an interval for a function of one variable, and a point minimises a convex function exactly when zero is one of its subgradients.
Where you'll see it: L1-regularised regression such as the lasso, support vector machines with hinge loss, and the subgradient method used to train models whose loss has kinks.
Intuition
Picture a bowl with a pointed bottom, like a tent seen from the side. At the tip there is no unique tangent. Many straight lines touch the tip and sit below the whole tent. Each of those lines is a supporting line, and its slope is a subgradient. On the smooth sides of the tent there is exactly one supporting line, which is the ordinary tangent, so the subgradient is just the gradient.
Why does this matter? Gradient descent needs a direction that lowers the function. At a smooth point the gradient gives one. At a corner, the gradient does not exist, and a naive method stalls. Many useful losses have corners on purpose. The absolute value is the penalty that makes the lasso select features, and the hinge loss is the margin loss of support vector machines. Both are convex, both have kinks, and both need a notion of slope that survives the kink.
The plot below shows and three lines through the origin. The solid curve is the function. The dashed lines have slopes , and . Each dashed line sits below the function everywhere, so each slope is a subgradient at . Any slope between and works. A line with slope would cross the function near the origin and would not be valid.
- f(x) = |x|
- slope 1
- slope -1
- slope 0.5
The math
Definition
Let be a convex function and a point in its domain. A vector is a subgradient of at if
In one dimension is just a number, the slope of the line.
The set of all subgradients at is the subdifferential, written .
The inequality says the line through with slope lies on or below the graph of . When is differentiable at , the only such slope is the gradient, so . At a corner the subdifferential contains several slopes.
The subdifferential is an interval
For a one-dimensional convex , is a closed interval , where is the left derivative and is the right derivative at . Each endpoint exists because convexity makes the one-sided slopes well defined. For at , the left derivative is and the right derivative is , so .
In higher dimensions the subdifferential is a convex set of vectors, and it is nonempty at every interior point of the domain. We will use the one-dimensional picture throughout.
Optimality condition
The most useful fact is the following. For convex , the point is a global minimiser if and only if
To see why, note that means for every . That is exactly the statement that is a global minimum. The converse holds by taking in the definition. This generalises the familiar condition that a smooth function has zero gradient at a minimum, and it works at kinks too.
Useful rules
Three rules cover most cases.
- Sum rule. If with both convex, then , with equality under mild conditions, for example when both functions are finite everywhere. Add the subgradients term by term.
- Pointwise maximum. If with each convex and differentiable, then at a point the subdifferential is the convex hull of the gradients over the indices that achieve the maximum (the active pieces).
- Affine composition. If , then .
The second rule explains the hinge loss and the piecewise linear losses used in practice. Each linear piece contributes its slope, and at a junction the subgradient is a mixture of the neighbouring slopes.
The subgradient method
Given a subgradient , the subgradient method takes the step
with step sizes chosen in advance. The main difference from gradient descent is that the step need not lower . A subgradient does not give a descent direction in general, so the function value can go up. What matters is the distance to the minimiser. With diminishing steps, the best value found so far converges to the optimum, typically at rate under bounded subgradients. That rate is slower than gradient descent on smooth functions, and it is the price of handling corners.
Worked examples
Take and compare two points.
- At , the function is smooth, so . Check the definition with : holds for all , and for the right side is at most , so it holds.
- At , test : we need for all . For the left side is and the right side is , so it holds. For the right side is negative, so it holds.
- Test : at the condition says , which fails. So is not a subgradient at .
- Conclusion: , and , so is the minimiser.
Interpretation: the corner is where all the trouble happens. A method that only uses smooth gradients has nothing to say at , but the subdifferential says that any slope in is a valid direction for the "is this the bottom?" test.
Minimise .
- Find the subdifferential of the first term at . Since has a corner there, .
- Add the smooth term , whose gradient is everywhere. The sum rule gives .
- Check whether . It is, since . So is the minimiser.
- Check the values: , , . The minimum is at the corner, as the optimality condition says.
Interpretation: the minimiser sits at a kink because the smooth slope is not strong enough to push the function off the corner. This is the same mechanism that makes the lasso set coefficients exactly to zero.
Run the method on with step , starting at , always using .
- .
- .
- .
- . The function value rose from to , so the method is not monotone.
- .
- .
Interpretation: the iterates bounce between and and never settle at zero. A fixed step is too coarse near the kink. Shrinking the steps, for example , lets the oscillation die out, at the cost of slower progress.
Tradeoffs and failure modes
| Situation | What goes wrong | Better choice |
|---|---|---|
| Loss has a kink and you use a gradient method anyway | Gradient is undefined at the kink, so the update is arbitrary and can stall | Use a subgradient method, or a smoothing that approximates the kink |
| Subgradient method with a fixed step | Iterates oscillate around the minimiser and never converge | Use diminishing steps, or keep the best iterate seen so far |
| Expecting every step to lower the function | Function values can rise, which surprises people used to gradient descent | Judge progress by the best value found, or by the distance to the optimum |
| Looking for a fast rate on a nonsmooth problem | The rate is the best general rate for this method | Use a proximal method, which handles simple nonsmooth terms like much faster |
| Summing subgradients of two functions whose domains barely overlap | The sum rule holds only under mild conditions and can overstate the set | Check the conditions, or work with the functions separately |
The main tradeoff is between generality and speed. The subgradient method works on any convex function you can evaluate a subgradient for, which makes it simple to implement. Specialised proximal methods are faster for the common case of a smooth term plus a simple nonsmooth term, such as the lasso.
History
The notion of a subgradient was developed in convex analysis in the 1960s. R. Tyrrell Rockafellar's book Convex Analysis (1970) gave the systematic theory of subdifferentials, and the work of Jean-Jacques Moreau in the same period introduced related ideas for nonsmooth functions. Naum Shor and others developed the subgradient method for minimisation in the 1960s, as a way to optimise functions that the smooth methods of the time could not handle.
The method was largely a specialist tool until the rise of machine learning. Margin losses such as the hinge loss, and absolute-value penalties, made nonsmooth objectives common, and the subgradient method and its proximal relatives became standard. The lasso, introduced by Robert Tibshirani in 1996, made the penalty and its kink central to statistics.
Check yourself
QuestionWhat is the subdifferential of f(x) = |x| at x = 3, and at x = 0?Show answer
At the function is smooth with slope , so the subdifferential is . At it has a corner, so every slope between the two one-sided slopes and is a subgradient. The subdifferential is the interval .
QuestionCompute the subdifferential of f(x) = |x - 1| + 2x at x = 1 and decide whether x = 1 is a minimiser.Show answer
The corner term gives at . The smooth term contributes the slope . Adding them gives . Zero is not in , so is not a minimiser. To the left of the function equals , which is below , so moving left lowers it.
QuestionWhy must a subgradient be below the whole graph of f, not just at the point where it touches?Show answer
The optimality test depends on the inequality holding for every . If a line touched the graph at one point but crossed above it elsewhere, it would not certify that the point is a minimum, because the function could drop below it somewhere else. The global inequality is what turns a local slope into a global statement.
QuestionA fixed-step subgradient method on f(x) = |x| oscillates between two values. What change would stop the oscillation, and what does it cost?Show answer
Shrinking the step size over time, for example , makes successive moves smaller so the iterates settle. The cost is slower progress: the steps shrink, so the method takes more iterations to reach a given accuracy.
QuestionIn a lasso fit, why does the minimiser often land exactly at zero for some coefficients, and how does the subgradient explain it?Show answer
The penalty has a corner at , with subdifferential . If the smooth loss has a gradient smaller in magnitude than at zero, then zero lies in the sum of subdifferentials, so the optimum sits exactly at the corner. The coefficient is set to zero, which is how the lasso selects features.