Cyclic Coordinate Descent for Convex Quadratics
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: medium
Interview Round: Other
Implement `quadratic_objective(A, b, x)`, returning `f(x) = 0.5·xᵀAx − bᵀx` as a `float`. Then implement `coordinate_descent(A, b, x0, tol=1e-10, max_sweeps=1000)`. Each sweep exactly minimises `f` over coordinates `i = 0..n-1` in order, updating in place: `x[i] = (b[i] − Σ_{j≠i} A[i,j]·x[j]) / A[i,i]`, using the newest values. Return `(x, n_sweeps, history)`.
- `history` is a NumPy array: `history[0] = f(x0)` and `history[k]` is `f` after sweep `k`, so its length is `n_sweeps + 1`.
- After each counted sweep, stop if `max|x − x_prev| ≤ tol`. Otherwise stop after `max_sweeps` sweeps.
- Raise `ValueError` if any diagonal entry of `A` is `≤ 0`.
- Do not mutate `x0`.
For example:
```python
A = [[2, 1], [1, 2]]
b = [1, 1]
x0 = [0, 0]
x, n_sweeps, history = coordinate_descent(A, b, x0, max_sweeps=2)
# x = [0.375, 0.3125], n_sweeps = 2
# history = [0.0, -0.3125, -0.33203125]
# sweep 1: x = [0.5, 0.25] sweep 2: x = [0.375, 0.3125]
```
```hint Newest values
Inside a sweep, coordinate `i` must see the values already written for coordinates `0..i-1` in this sweep. Think about which array each read comes from, and what you need to keep a separate copy of.
```
```hint Order of bookkeeping
Decide whether the sweep is counted and `f` recorded before or after the stopping test. Starting at the minimiser must give exactly one counted sweep and a history of length 2.
```
### Constraints & Assumptions
- `A` is a symmetric `n × n` matrix and `b`, `x0` have length `n`.
- `A` is positive definite unless it is being tested for the `ValueError`.
- Starting at the minimiser stops after exactly 1 sweep.
### What a Strong Answer Covers
- The closed-form coordinate update from setting the partial derivative to zero, applied in place so later coordinates use the newest values.
- The stopping rule and bookkeeping: the confirming sweep is counted, `history` has length `n_sweeps + 1`, and `x0` is left untouched.
- The diagonal check, and why each one-dimensional step needs a positive diagonal entry.
- Why `f` never increases, why the iterates reach the solution of `Ax = b` for positive-definite `A`, and the cost per sweep.
### Follow-up Questions
- How does the conditioning of `A` affect convergence speed?
- How would you extend this to Lasso (an L1 penalty)?
- Cyclic vs randomized coordinate order: when does it matter?
Overview: Implement cyclic coordinate descent for a convex quadratic objective, minimising one coordinate at a time in closed form and recording the objective after each sweep. Tests deriving the coordinate update, in-place Gauss-Seidel ordering, stopping-rule bookkeeping, and how conditioning slows convergence.