Search

Search papers and lessons

Subgradient

Level 3 · 12 min read

A subgradient generalises the gradient to functions with sharp corners: any slope of a line that stays below the function at a point is a valid subgradient there.

On this page

  • Colour = topic mix
  • Calculus
  • Optimization
  • Linear algebra
  • Discrete math
  • Sign in to see your progress

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 ∣x∣|x| 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 ∣w∣|w| is the penalty that makes the lasso select features, and the hinge loss max⁡(0,1−y f(x))\max(0, 1 - y\,f(x)) 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 f(x)=∣x∣f(x) = |x| and three lines through the origin. The solid curve is the function. The dashed lines have slopes −1-1, 0.50.5 and 11. Each dashed line sits below the function everywhere, so each slope is a subgradient at x=0x = 0. Any slope between −1-1 and 11 works. A line with slope 1.51.5 would cross the function near the origin and would not be valid.

−2−1.5−1−0.500.511.52−1−0.500.511.52xf(x)
  • f(x) = |x|
  • slope 1
  • slope -1
  • slope 0.5
At the corner x = 0, every line with slope between -1 and 1 stays below |x|. Those slopes are the subgradients at 0.

The math

Definition

Definition: Subgradient

Let ff be a convex function and x0x_0 a point in its domain. A vector gg is a subgradient of ff at x0x_0 if

f(x)≥f(x0)+g⊤(x−x0)for all x.f(x) \ge f(x_0) + g^{\top}(x - x_0) \quad \text{for all } x.

In one dimension gg is just a number, the slope of the line.

The set of all subgradients at x0x_0 is the subdifferential, written ∂f(x0)\partial f(x_0).

The inequality says the line through (x0,f(x0))(x_0, f(x_0)) with slope gg lies on or below the graph of ff. When ff is differentiable at x0x_0, the only such slope is the gradient, so ∂f(x0)={∇f(x0)}\partial f(x_0) = \{\nabla f(x_0)\}. At a corner the subdifferential contains several slopes.

The subdifferential is an interval

For a one-dimensional convex ff, ∂f(x0)\partial f(x_0) is a closed interval [a,b][a, b], where aa is the left derivative and bb is the right derivative at x0x_0. Each endpoint exists because convexity makes the one-sided slopes well defined. For f(x)=∣x∣f(x) = |x| at x0=0x_0 = 0, the left derivative is −1-1 and the right derivative is +1+1, so ∂f(0)=[−1,1]\partial f(0) = [-1, 1].

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 ff, the point x∗x^{*} is a global minimiser if and only if

0∈∂f(x∗).0 \in \partial f(x^{*}).

To see why, note that 0∈∂f(x∗)0 \in \partial f(x^{*}) means f(x)≥f(x∗)+0⋅(x−x∗)=f(x∗)f(x) \ge f(x^{*}) + 0 \cdot (x - x^{*}) = f(x^{*}) for every xx. That is exactly the statement that x∗x^{*} is a global minimum. The converse holds by taking g=0g = 0 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.

  1. Sum rule. If f=f1+f2f = f_1 + f_2 with both convex, then ∂f(x)⊇∂f1(x)+∂f2(x)\partial f(x) \supseteq \partial f_1(x) + \partial f_2(x), with equality under mild conditions, for example when both functions are finite everywhere. Add the subgradients term by term.
  2. Pointwise maximum. If f(x)=max⁡ifi(x)f(x) = \max_i f_i(x) with each fif_i convex and differentiable, then at a point xx the subdifferential is the convex hull of the gradients ∇fi(x)\nabla f_i(x) over the indices ii that achieve the maximum (the active pieces).
  3. Affine composition. If f(x)=h(Ax+b)f(x) = h(Ax + b), then ∂f(x)=A⊤∂h(Ax+b)\partial f(x) = A^{\top} \partial h(Ax + b).

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 gk∈∂f(xk)g_k \in \partial f(x_k), the subgradient method takes the step

xk+1=xk−ηk gk,x_{k+1} = x_k - \eta_k\, g_k,

with step sizes ηk\eta_k chosen in advance. The main difference from gradient descent is that the step need not lower ff. 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 O(1/k)O(1/\sqrt{k}) under bounded subgradients. That rate is slower than gradient descent on smooth functions, and it is the price of handling corners.

Worked examples

Example
The kink of the absolute value

Take f(x)=∣x∣f(x) = |x| and compare two points.

  1. At x0=2x_0 = 2, the function is smooth, so ∂f(2)={1}\partial f(2) = \{1\}. Check the definition with g=1g = 1: ∣x∣≥2+1⋅(x−2)|x| \ge 2 + 1\cdot(x - 2) holds for all x>0x \gt 0, and for x≤0x \le 0 the right side is at most 00, so it holds.
  2. At x0=0x_0 = 0, test g=0.5g = 0.5: we need ∣x∣≥0+0.5 x|x| \ge 0 + 0.5\,x for all xx. For x≥0x \ge 0 the left side is xx and the right side is 0.5x0.5x, so it holds. For x<0x \lt 0 the right side is negative, so it holds.
  3. Test g=1.5g = 1.5: at x=1x = 1 the condition says 1≥1.51 \ge 1.5, which fails. So 1.51.5 is not a subgradient at 00.
  4. Conclusion: ∂f(0)=[−1,1]\partial f(0) = [-1, 1], and 0∈∂f(0)0 \in \partial f(0), so x=0x = 0 is the minimiser.

Interpretation: the corner is where all the trouble happens. A method that only uses smooth gradients has nothing to say at x=0x = 0, but the subdifferential says that any slope in [−1,1][-1, 1] is a valid direction for the "is this the bottom?" test.

Example
Minimising a piecewise linear function

Minimise f(x)=∣x−3∣+0.5 xf(x) = |x - 3| + 0.5\,x.

  1. Find the subdifferential of the first term at x=3x = 3. Since ∣x−3∣|x - 3| has a corner there, ∂∣x−3∣∣x=3=[−1,1]\partial |x-3| \big|_{x=3} = [-1, 1].
  2. Add the smooth term 0.5x0.5x, whose gradient is 0.50.5 everywhere. The sum rule gives ∂f(3)=[−1,1]+0.5=[−0.5,1.5]\partial f(3) = [-1, 1] + 0.5 = [-0.5, 1.5].
  3. Check whether 0∈∂f(3)0 \in \partial f(3). It is, since −0.5≤0≤1.5-0.5 \le 0 \le 1.5. So x∗=3x^{*} = 3 is the minimiser.
  4. Check the values: f(3)=0+1.5=1.5f(3) = 0 + 1.5 = 1.5, f(2)=1+1=2f(2) = 1 + 1 = 2, f(4)=1+2=3f(4) = 1 + 2 = 3. The minimum is at the corner, as the optimality condition says.

Interpretation: the minimiser sits at a kink because the smooth slope 0.50.5 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.

Example
Subgradient steps on |x| with a fixed step

Run the method on f(x)=∣x∣f(x) = |x| with step η=0.3\eta = 0.3, starting at x0=1x_0 = 1, always using gk=sign⁡(xk)g_k = \operatorname{sign}(x_k).

  1. x1=1−0.3⋅1=0.7x_1 = 1 - 0.3 \cdot 1 = 0.7.
  2. x2=0.7−0.3⋅1=0.4x_2 = 0.7 - 0.3 \cdot 1 = 0.4.
  3. x3=0.4−0.3⋅1=0.1x_3 = 0.4 - 0.3 \cdot 1 = 0.1.
  4. x4=0.1−0.3⋅1=−0.2x_4 = 0.1 - 0.3 \cdot 1 = -0.2. The function value rose from 0.10.1 to 0.20.2, so the method is not monotone.
  5. x5=−0.2−0.3⋅(−1)=0.1x_5 = -0.2 - 0.3 \cdot (-1) = 0.1.
  6. x6=0.1−0.3⋅1=−0.2x_6 = 0.1 - 0.3 \cdot 1 = -0.2.

Interpretation: the iterates bounce between 0.10.1 and −0.2-0.2 and never settle at zero. A fixed step is too coarse near the kink. Shrinking the steps, for example ηk=1/k\eta_k = 1/k, lets the oscillation die out, at the cost of slower progress.

Tradeoffs and failure modes

SituationWhat goes wrongBetter choice
Loss has a kink and you use a gradient method anywayGradient is undefined at the kink, so the update is arbitrary and can stallUse a subgradient method, or a smoothing that approximates the kink
Subgradient method with a fixed stepIterates oscillate around the minimiser and never convergeUse diminishing steps, or keep the best iterate seen so far
Expecting every step to lower the functionFunction values can rise, which surprises people used to gradient descentJudge progress by the best value found, or by the distance to the optimum
Looking for a fast rate on a nonsmooth problemThe O(1/k)O(1/\sqrt{k}) rate is the best general rate for this methodUse a proximal method, which handles simple nonsmooth terms like ℓ1\ell_1 much faster
Summing subgradients of two functions whose domains barely overlapThe sum rule holds only under mild conditions and can overstate the setCheck 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 ℓ1\ell_1 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 x=3x = 3 the function is smooth with slope 11, so the subdifferential is {1}\{1\}. At x=0x = 0 it has a corner, so every slope between the two one-sided slopes −1-1 and 11 is a subgradient. The subdifferential is the interval [−1,1][-1, 1].

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 [−1,1][-1, 1] at x=1x = 1. The smooth term 2x2x contributes the slope 22. Adding them gives ∂f(1)=[−1,1]+2=[1,3]\partial f(1) = [-1, 1] + 2 = [1, 3]. Zero is not in [1,3][1, 3], so x=1x = 1 is not a minimiser. To the left of 11 the function equals 1+x1 + x, which is below f(1)=2f(1) = 2, 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 0∈∂f(x∗)0 \in \partial f(x^{*}) depends on the inequality holding for every xx. 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 ηk=1/k\eta_k = 1/k, 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 λ∣wj∣\lambda |w_j| has a corner at wj=0w_j = 0, with subdifferential [−λ,λ][-\lambda, \lambda]. If the smooth loss has a gradient smaller in magnitude than λ\lambda 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.

Sources