Phase II — Unconstrained Optimization | Week 3 | 2.5 hours O(n) per step, n-step convergence for quadratics — the middle ground.
Ceres uses Conjugate Gradient internally via ITERATIVE_SCHUR and CGNR linear solvers. When solving $J^TJ\delta = -J^Tr$ at each Gauss-Newton step, CG avoids forming $J^TJ$ explicitly — just matrix-vector products. For sparse SLAM with 100K parameters, this is essential. CG for optimization (nonlinear CG) is also the core of Newton-CG.
For $Ax = b$ with $A \succ 0$: generate conjugate directions $d_0, d_1, \ldots$ satisfying $d_i^T A d_j = 0$.
Key property: Linear CG converges in exactly $n$ steps for $n \times n$ systems.
Extend CG to general $\min f(x)$:
The magic is in choosing $\beta_k$:
Fletcher-Reeves:
Polak-Ribière:
Polak-Ribière+ (recommended):
This automatic restart (setting $\beta = 0$ when PR is negative) is crucial for robustness.
For a quadratic $f = \frac{1}{2}x^TAx - b^Tx$: - CG directions are A-conjugate: $d_i^T A d_j = 0$ - At step $k$, $x_k$ minimizes $f$ over $x_0 + \text{span}\{d_0, \ldots, d_{k-1}\}$ - Converges in at most $n$ steps (exact arithmetic) - Much faster if eigenvalues are clustered: $O(\sqrt{\kappa})$ steps
For nonlinear functions: locally, $f \approx$ quadratic near the minimum, so CG inherits these properties.
Nonlinear CG can lose conjugacy. Remedies: 1. Periodic restart: Every $n$ steps, reset $d = -g$ 2. PR+ automatic restart: $\beta^{PR+} = \max(0, \beta^{PR})$ — restarts when directions become poor 3. Powell restart: Restart when $|g_{k+1}^T g_k| > 0.2 \|g_{k+1}\|^2$ — detects loss of orthogonality
| Method | Per-step cost | Storage | Convergence |
|---|---|---|---|
| GD | $O(n)$ | $O(n)$ | Linear, $O(\kappa)$ steps |
| CG | $O(n)$ | $O(n)$ | $n$-step for quadratic, $O(\sqrt{\kappa})$ |
| L-BFGS | $O(mn)$ | $O(mn)$ | Superlinear |
| Newton | $O(n^3)$ | $O(n^2)$ | Quadratic |
CG has the same $O(n)$ cost as GD but much better convergence on well-conditioned problems.
# See: code/week03/conjugate_gradient.py
Implement both CG variants with Wolfe line search.
Test CG on a quadratic $f = \frac{1}{2}x^TAx - b^Tx$ with $n = 50$. Verify it converges in exactly $n$ steps (or fewer with clustered eigenvalues).
Compare on large-scale Rosenbrock ($n = 100$). Plot iterations vs $\|g\|$.
Compare FR, PR, PR+, and periodic restart on a nonlinear function. Show PR+ is most robust.
Problem: For $f(x)$ with condition number $\kappa$, CG needs $O(\sqrt{\kappa})$ steps. Use a preconditioner $M \approx H^{-1}$: replace $g$ with $Mg$ in the CG update. Implement using diagonal Hessian approximation $M = \text{diag}(H)^{-1}$ and compare iteration count.
Solution: Preconditioned CG: $d_{k+1} = -Mg_{k+1} + \beta_k d_k$ with $\beta_k^{PR} = g_{k+1}^T(Mg_{k+1}-Mg_k) / (g_k^T Mg_k)$. The diagonal preconditioner captures scale differences. For Rosenbrock with scaling $[1, 100]$, this reduces iterations by ~3-5×. For SLAM, the block-diagonal of $J^TJ$ is a natural preconditioner (each pose block is easy to invert).
Problem: Use CG to inexactly solve $H_k \delta = -g_k$ (the Newton system). Stop CG early when: (a) $\|r_j\| \leq \eta_k\|g_k\|$ (forcing sequence), (b) negative curvature detected ($d^THd < 0$), or (c) boundary of trust region reached. Implement and compare with full Newton.
Solution: The forcing sequence $\eta_k = \min(0.5, \sqrt{\|g_k\|})$ gives superlinear convergence (Eisenstat-Walker choice). Negative curvature direction is valuable: step to trust region boundary along it. This combines Newton’s local rate with CG’s $O(n)$ per-step cost. Used in large-scale optimization libraries (L-BFGS-B, IPOPT inner solver).
Problem: For least squares $\min \|Ax - b\|^2$, apply CG to the normal equations $A^TAx = A^Tb$ without forming $A^TA$. Implement CGNR (CG on Normal equations, Residual form) and compare with direct SVD solve. This is exactly what Ceres does with CGNR solver type.
Solution: CGNR: the matrix-vector product $A^TA v$ is computed as two operations: $w = Av$ then $A^Tw$. Never form $A^TA$. For sparse $A$ (Jacobian in SLAM), this preserves sparsity. Cost: 2 sparse mat-vecs per CG step. With preconditioning (Jacobi or block-diagonal), CGNR converges in $O(\sqrt{\kappa(A^TA)})$ steps. Ceres config: linear_solver_type = CGNR; preconditioner_type = JACOBI.