TExcelTAD
Home › Optimization › Coordinate Descent

Coordinate Descent

Coordinate descent minimizes over one variable (or one block of variables) at a time, holding the others fixed. Each step is a one-dimensional problem, often with a closed-form solution. That simplicity makes it the fastest method for some important problems, most famously the lasso and elastic net in glmnet and scikit-learn.

The method

Cycle through the coordinates i=1,…,ni = 1, \dots, n:

xi←arg⁡min⁡zf(x1,…,xi−1,z,xi+1,…,xn)x_i \leftarrow \arg\min_{z} f(x_1, \dots, x_{i-1}, z, x_{i+1}, \dots, x_n)

For a quadratic f(x)=12xTAx−bTxf(\mathbf{x}) = \frac{1}{2}\mathbf{x}^TA\mathbf{x} - \mathbf{b}^T\mathbf{x}, setting ∂f/∂xi=0\partial f/\partial x_i = 0 gives xi=(bi−∑j≠iAijxj)/Aiix_i = \left(b_i - \sum_{j \ne i}A_{ij}x_j\right)/A_{ii}: exactly the Gauss-Seidel iteration for Ax=bA\mathbf{x} = \mathbf{b}.

python · try it yourself
import numpy as np

A = np.array([[4.0, 1.0, 0.5], [1.0, 3.0, 0.2], [0.5, 0.2, 2.0]]); b = np.array([1.0, 2.0, 3.0])
x = np.zeros(3)
f = lambda x: 0.5 * x @ A @ x - b @ x
for sweep in range(1, 9):
    for i in range(3):
        x[i] = (b[i] - A[i] @ x + A[i, i] * x[i]) / A[i, i]
    print(f"sweep {sweep}: f = {f(x):.10f}")
print("exact minimum f =", round(float(f(np.linalg.solve(A, b))), 10))
sweep 1: f = -2.5375173611
sweep 2: f = -2.7464630339
sweep 3: f = -2.7468293494
sweep 4: f = -2.7468294954
sweep 5: f = -2.7468294974
sweep 6: f = -2.7468294974
sweep 7: f = -2.7468294974
sweep 8: f = -2.7468294974
exact minimum f = -2.7468294974

Coordinate descent for the lasso

The lasso, min⁡w12∥Xw−y∥2+λ∥w∥1\min_{\mathbf{w}} \frac{1}{2}\lVert X\mathbf{w} - \mathbf{y} \rVert^2 + \lambda\lVert \mathbf{w} \rVert_1, is not differentiable, but its one-dimensional problems are easy. With the partial residual r−j=y−∑k≠jX:,kwk\mathbf{r}_{-j} = \mathbf{y} - \sum_{k \ne j}X_{:,k}w_k, the update is a soft-thresholding:

wj←Sλ(X:,jTr−j)∥X:,j∥2,Sλ(z)=sign⁡(z)max⁡(∣z∣−λ,0)w_j \leftarrow \frac{S_\lambda(X_{:,j}^T\mathbf{r}_{-j})}{\lVert X_{:,j} \rVert^2}, \qquad S_\lambda(z) = \operatorname{sign}(z)\max(\lvert z \rvert - \lambda, 0)

The threshold sets small coefficients exactly to zero, which is how the lasso selects features.

python · try it yourself
import numpy as np

rng = np.random.default_rng(0)
n, d = 100, 8
X = rng.normal(size=(n, d))
w_true = np.array([3.0, -2.0, 0, 0, 1.5, 0, 0, 0])
y = X @ w_true + 0.5 * rng.normal(size=n)
soft = lambda z, t: np.sign(z) * max(abs(z) - t, 0.0)

def lasso_cd(X, y, lam, sweeps=200):
    w = np.zeros(X.shape[1]); r = y.copy()
    col_sq = (X**2).sum(axis=0)
    for _ in range(sweeps):
        for j in range(X.shape[1]):
            r += X[:, j] * w[j]                         # remove coordinate j
            w[j] = soft(X[:, j] @ r, lam) / col_sq[j]
            r -= X[:, j] * w[j]                         # add it back
    return w

w = lasso_cd(X, y, lam=20.0)
print("lasso weights:", np.round(w, 3) + 0)
from sklearn.linear_model import Lasso
sk = Lasso(alpha=20.0 / n, fit_intercept=False).fit(X, y)       # sklearn divides the loss by n
print("scikit-learn: ", np.round(sk.coef_, 3) + 0)
lasso weights: [ 2.691 -1.754  0.     0.     1.311  0.     0.     0.   ]
scikit-learn:  [ 2.691 -1.754  0.     0.     1.311  0.     0.     0.   ]

Five coefficients are exactly zero, and the result matches scikit-learn, which uses the same algorithm.

When coordinate descent works

Condition Effect
smooth convex ff converges to the minimum
ff = smooth convex + separable nonsmooth (∑igi(xi)\sum_i g_i(x_i), e.g. L1) converges (the lasso case)
nonsmooth term couples variables (e.g. ∣x1−x2∣\lvert x_1 - x_2 \rvert) can get stuck at non-optimal points
cheap 1D updates (residual bookkeeping) each sweep costs about one gradient evaluation

Variables that are strongly correlated slow it down, as with Gauss-Seidel on ill-conditioned matrices. Randomized coordinate order often gives better theoretical guarantees.

A failure case

python · try it yourself
import numpy as np

# f(x, y) = |x - y| + 0.1 (x + y)^2 ... minimum at (0, 0), but from (1, 1) no single coordinate helps
f = lambda x, y: abs(x - y) + 0.1 * (x + y)**2
x = y = 1.0
grid = np.linspace(-3, 3, 60001)
for sweep in range(3):
    x = grid[np.argmin([f(z, y) for z in grid])]
    y = grid[np.argmin([f(x, z) for z in grid])]
print("coordinate descent stops at", (round(float(x), 3), round(float(y), 3)), " f =", round(f(x, y), 4), " true minimum f(0, 0) = 0")
coordinate descent stops at (1.0, 1.0)  f = 0.4  true minimum f(0, 0) = 0

At (1,1)(1, 1), moving xx alone or yy alone increases ∣x−y∣\lvert x - y \rvert faster than it decreases the smooth part, yet moving both together reaches 0.

In software

  • scikit-learn's Lasso, ElasticNet and LassoCV, and R's glmnet, use cyclic coordinate descent with "warm starts" along a path of λ\lambda values.
  • LinearSVC (liblinear) solves the SVM dual by coordinate descent; matrix factorization for recommender systems uses alternating least squares, a block-coordinate method.

Exercises

Exercise 1: One coordinate update

For f(x,y)=x2+xy+y2−3xf(x, y) = x^2 + xy + y^2 - 3x, starting at (0,0)(0, 0), do one sweep (xx then yy).

Answer

x=1.5x = 1.5, then y=−0.75y = -0.75.

Solution

∂f/∂x=2x+y−3=0\partial f/\partial x = 2x + y - 3 = 0 with y=0y = 0 gives x=1.5x = 1.5. Then ∂f/∂y=x+2y=0\partial f/\partial y = x + 2y = 0 gives y=−0.75y = -0.75.

Exercise 2: Soft threshold

Compute S2(5)S_2(5), S2(−1)S_2(-1) and S2(−3.5)S_2(-3.5).

Answer

3, 0 and −1.5-1.5.

Solution

Shrink the magnitude by 2 and clip at zero, keeping the sign.

Exercise 3: Bigger λ\lambda (code)

Rerun lasso_cd with λ=200\lambda = 200. How many weights are non-zero?

Answer

Only one: the weight of the strongest feature, w1≈0.41w_1 \approx 0.41.

Solution
python · try it yourself
import numpy as np
rng = np.random.default_rng(0)
X = rng.normal(size=(100, 8)); y = X @ np.array([3.0, -2.0, 0, 0, 1.5, 0, 0, 0]) + 0.5 * rng.normal(size=100)
soft = lambda z, t: np.sign(z) * max(abs(z) - t, 0.0)
w = np.zeros(8); r = y.copy(); cs = (X**2).sum(axis=0)
for _ in range(200):
    for j in range(8):
        r += X[:, j] * w[j]; w[j] = soft(X[:, j] @ r, 200.0) / cs[j]; r -= X[:, j] * w[j]
print(np.round(w, 3) + 0, " non-zero:", int(np.sum(w != 0)))
[0.412 0.    0.    0.    0.    0.    0.    0.   ]  non-zero: 1

Test yourself

Coordinate descent on a convex quadratic is the same as…

  1. Newton's method
  2. the Gauss-Seidel iteration
  3. the simplex method
  4. gradient descent with step 1

Why does coordinate descent work for the lasso even though ∥w∥1\lVert \mathbf{w} \rVert_1 is not differentiable?

  1. The L1 term is separable across coordinates
  2. The lasso is not convex
  3. It uses the Hessian
  4. It ignores the L1 term

The soft-threshold Sλ(z)S_\lambda(z) with ∣z∣≤λ\lvert z \rvert \le \lambda returns…

  1. zz
  2. λ\lambda
  3. 00
  4. −z-z