Search

Search papers and lessons

Coordinate Descent

Level 3 · 9 min read

Coordinate descent minimises a function by optimising one variable at a time while holding the rest fixed, cycling through the variables until nothing moves.

On this page

  • Colour = topic mix
  • Calculus
  • Probability
  • Linear algebra
  • Losses & regularization
  • Optimizers
  • Discrete math
  • Sign in to see your progress

TL;DR

Coordinate descent minimises a function of several variables by picking one variable at a time, minimising over that variable while every other variable stays fixed, and cycling through the variables. Each single-variable problem is usually much easier than the full one, so a cheap step on each coordinate can add up to a good solution.

Where you'll see it: fitting lasso and elastic-net regressions, the least-squares solvers inside many statistics packages, and training of sparse linear models with millions of features.

Intuition

Picture a hilly field described by two numbers, east-west and north-south. Gradient descent takes a step in the steepest direction, which may be diagonal and needs you to know the slope in both directions at once. Coordinate descent takes a different approach. It first walks due east until it reaches the lowest point on that line, then walks due north to the lowest point on that line, then east again, and so on. Each walk is a one-dimensional problem, which is easy to solve, and the walks zig-zag toward the bottom of the valley.

The method exists because many problems have a structure where moving along one coordinate is cheap and exact, even when the full problem is tangled. In regression with many features, changing one coefficient while the others are fixed reduces to a simple formula. A gradient step over all coefficients at once has to touch every feature on every step, which is expensive when there are many features and most of them are zero.

flowchart LR
  A["start at x"] --> B["minimise over x_1, others fixed"]
  B --> C["minimise over x_2, others fixed"]
  C --> D["... over x_n"]
  D --> E{"any coordinate still moving?"}
  E -- yes --> B
  E -- no --> F["stop: approximate minimiser"]

The zig-zag is also the method's weakness. When two variables are strongly correlated, the valley is narrow and diagonal, and axis-aligned steps make slow progress. A diagonal step would be much better, which is why coordinate descent works best when the variables are only weakly coupled.

The math

Let f:Rn→Rf:\mathbb{R}^n \to \mathbb{R} be the function to minimise and write x=(x1,…,xn)x = (x_1, \dots, x_n). Coordinate descent updates one coordinate xix_i at a time:

xinew=arg⁡min⁡u∈R  f(x1,…,xi−1,u,xi+1,…,xn).x_i^{\text{new}} = \arg\min_{u \in \mathbb{R}} \; f(x_1, \dots, x_{i-1}, u, x_{i+1}, \dots, x_n).

The other coordinates are held at their latest values. A full pass that updates i=1,…,ni = 1, \dots, n in order is one cycle. The cyclic version visits coordinates in a fixed order, and the random version picks one at random each step.

When ff is smooth and strictly convex, the single-coordinate problem has a closed-form solution in many cases. For a quadratic

f(x)=12xTAx−bTx,A symmetric positive definite,f(x) = \tfrac{1}{2} x^{\mathsf T} A x - b^{\mathsf T} x, \qquad A \text{ symmetric positive definite},

the derivative with respect to xix_i is (Ax)i−bi(Ax)_i - b_i. Setting it to zero with the other coordinates fixed gives

xi←1Aii(bi−∑j≠iAijxj).x_i \leftarrow \frac{1}{A_{ii}}\Big(b_i - \sum_{j \ne i} A_{ij} x_j\Big).

This is the Gauss–Seidel method for solving Ax=bAx = b, which is why the two are often taught together.

Convergence

For a smooth, strictly convex ff, cyclic coordinate descent converges to the unique minimiser. Its rate depends on the coupling between coordinates, which is the off-diagonal part of AA in the quadratic case. Weak coupling means fast convergence, and strong coupling means the zig-zag described above.

For a non-smooth ff, coordinate descent can stall. Take f(x,y)=∣x+y∣+3∣x−y∣f(x,y) = |x+y| + 3|x-y|. At (1,1)(1,1), moving along xx alone gives ∣x+1∣+3∣x−1∣|x+1| + 3|x-1|, which has its minimum at x=1x=1, and the same holds for yy. So no single-coordinate move improves on (1,1)(1,1), where f=2f = 2, yet f(0,0)=0f(0,0) = 0. The kink couples the two coordinates, and the method stalls. Non-smoothness is safe when it is separable. If

f(x)=g(x)+∑ihi(xi),f(x) = g(x) + \sum_{i} h_i(x_i),

with gg smooth and convex and each hih_i convex (possibly non-smooth, for example λ∣xi∣\lambda|x_i|, see Subgradient), then coordinate descent still converges. The lasso penalty is exactly this kind of term, which is why the method suits sparse regression.

The lasso update

Take the lasso objective 12n∥y−Xβ∥2+λ∥β∥1\frac{1}{2n}\lVert y - X\beta\rVert^2 + \lambda\lVert\beta\rVert_1 with each feature column standardised so that 1n∥Xj∥2=1\frac{1}{n}\lVert X_j\rVert^2 = 1. Let rj=1nXjT(y−∑k≠jXkβk)r_j = \frac{1}{n} X_j^{\mathsf T}(y - \sum_{k\ne j} X_k\beta_k) be the correlation of feature jj with the partial residual (the residual with feature jj's own contribution left out). The one-dimensional problem in βj\beta_j then has the closed-form solution

β^j=sign⁡(rj) max⁡(∣rj∣−λ, 0).\hat\beta_j = \operatorname{sign}(r_j)\,\max\big(|r_j| - \lambda,\, 0\big).

This is the Soft Thresholding operator. It shrinks rjr_j toward zero by λ\lambda and sets it exactly to zero when ∣rj∣|r_j| is below λ\lambda. Exact zeros are what make the lasso select variables, and coordinate descent is the usual way to compute it.

Worked examples

Example
A two-variable quadratic

Minimise f(x,y)=x2+y2+xy−2x−2yf(x, y) = x^2 + y^2 + xy - 2x - 2y by cyclic coordinate descent, starting at (x,y)=(0,0)(x, y) = (0, 0).

  1. The derivative with respect to xx is 2x+y−22x + y - 2. Setting it to zero with yy fixed gives x=(2−y)/2x = (2 - y)/2.
  2. The derivative with respect to yy is x+2y−2x + 2y - 2. Setting it to zero with xx fixed gives y=(2−x)/2y = (2 - x)/2.
  3. Cycle 1: x=(2−0)/2=1x = (2 - 0)/2 = 1. Then y=(2−1)/2=0.5y = (2 - 1)/2 = 0.5.
  4. Cycle 2: x=(2−0.5)/2=0.75x = (2 - 0.5)/2 = 0.75. Then y=(2−0.75)/2=0.625y = (2 - 0.75)/2 = 0.625.
  5. Cycle 3: x=(2−0.625)/2=0.6875x = (2 - 0.625)/2 = 0.6875. Then y=(2−0.6875)/2=0.65625y = (2 - 0.6875)/2 = 0.65625.

The exact minimiser solves 2x+y=22x + y = 2 and x+2y=2x + 2y = 2, which gives x=y=2/3≈0.6667x = y = 2/3 \approx 0.6667. The iterates approach that value, and the gap shrinks by a factor of about 1/41/4 each cycle. Here the coupling is 1/21/2 on each side of the diagonal, and the factor is the product of the two coupling terms. The interpretation: each cycle removes most of the remaining error, so three cycles already get within about 0.02 of the answer.

Example
One lasso coordinate

Use the standardised lasso setting above. Fix the other coefficients, and suppose rj=3r_j = 3. The penalty is λ=1\lambda = 1.

  1. Compute ∣rj∣−λ=3−1=2|r_j| - \lambda = 3 - 1 = 2.
  2. Since 2>02 > 0, take the positive part, which is 22.
  3. Multiply by the sign of rjr_j, which is +1+1. So β^j=2\hat\beta_j = 2.

Now take rj=0.4r_j = 0.4 with the same penalty.

  1. Compute ∣rj∣−λ=0.4−1=−0.6|r_j| - \lambda = 0.4 - 1 = -0.6.
  2. The positive part of −0.6-0.6 is 00, so β^j=0\hat\beta_j = 0.

The coefficient is zeroed out entirely. Coordinate descent on the lasso does exactly this, one coordinate at a time, and repeats until the coefficients stop changing.

Tradeoffs and failure modes

Coordinate descent is a good choice when each one-dimensional update is cheap or closed-form, when the problem is sparse so most updates change nothing, and when the penalty is separable. It is a poor choice when variables are strongly coupled, since convergence can be slow, and when the non-smooth part couples several coordinates, since the method can stall at a non-minimiser.

SituationProblemBetter choice
Many features, sparse solutionEach update is cheap, and most coefficients stay zeroCoordinate descent or its variants
Strongly correlated featuresThe zig-zag is slowGradient methods with a good preconditioner, or a full Newton-type step
Non-separable non-smooth penaltyThe method can stop at a non-minimiserProximal gradient or subgradient methods
Very large nn with a cheap gradientEach cycle touches every coordinateStochastic gradient descent

History

The idea of optimising one variable at a time is very old. The Gauss–Seidel iteration for linear systems, which is coordinate descent on a quadratic, is named after Carl Friedrich Gauss and Philipp Ludwig von Seidel, and dates from the nineteenth century. Its use for large regression problems grew in the 2000s, when the lasso became a standard tool and people needed to fit it to very large datasets. Friedman, Hastie, Höfling and Tibshirani popularised coordinate descent for the lasso and elastic net in that period, and it is now the default in several statistical packages. Stephen Wright's 2015 survey gives the theory for the cases where it works.

Check yourself

QuestionFor f(x,y)=x2+y2+xy−2x−2yf(x, y) = x^2 + y^2 + xy - 2x - 2y, the first cycle of coordinate descent from (0,0)(0, 0) gives x=1x = 1. What is yy after that first cycle?Show answer

With x=1x = 1 fixed, the derivative in yy is x+2y−2=1+2y−2x + 2y - 2 = 1 + 2y - 2. Setting it to zero gives 2y=12y = 1, so y=0.5y = 0.5. This matches step 3 of the worked example.

QuestionIn the standardised lasso update β^j=sign⁡(rj)max⁡(∣rj∣−λ,0)\hat\beta_j = \operatorname{sign}(r_j)\max(|r_j| - \lambda, 0), what is β^j\hat\beta_j when rj=−2.5r_j = -2.5 and λ=0.5\lambda = 0.5?Show answer

The absolute value is ∣rj∣=2.5|r_j| = 2.5, so ∣rj∣−λ=2|r_j| - \lambda = 2. The positive part is 22, and the sign of rjr_j is −1-1. So β^j=−2\hat\beta_j = -2. The shrinkage moves the value toward zero by λ\lambda and keeps its sign.

QuestionWhy does coordinate descent get stuck on some non-smooth functions, and why does adding a separable penalty like λ∣xi∣\lambda |x_i| fix the problem?Show answer

A kink that couples coordinates, such as ∣x+y∣+3∣x−y∣|x+y| + 3|x-y| at (1,1)(1,1), can make every single-axis move fail to lower the value even though a diagonal move would. Coordinate descent cannot take the diagonal move, so it stops. When the non-smooth part is a sum of terms, one per coordinate, each kink lies along a single axis, so a point with no improving axis move is a true minimiser. The stuck corners cannot form, so the method converges.

QuestionA researcher has a problem with a million features, where a gradient over all features costs a great deal but each feature touches only a few samples. Argue whether coordinate descent is a reasonable choice.Show answer

It is a reasonable choice if each coordinate's update is cheap, because a single coordinate step only needs the samples that involve that feature, and a sparse solution means many updates change nothing. The cost of a full cycle is then proportional to the number of non-zero entries, not to the full gradient. The risk is strong correlation between features, which slows convergence. A check is to run a few cycles and see whether the objective is falling at a useful rate before committing to the method.

Sources