Phase II — Unconstrained Optimization | Week 3 | 2.5 hours Simple, universal, and the foundation for everything that follows.
Gradient descent underlies every optimizer you’ll use. Ceres starts each LM iteration with a gradient computation $g = J^Tr$. Understanding why plain GD is slow on SLAM problems (ill-conditioned $J^TJ$, $\kappa \sim 10^6$) explains why LM/trust-region methods exist. The convergence rate $\rho = (\kappa - 1)/(\kappa + 1)$ predicts exactly how many iterations you’d waste.
Before starting Phase II, verify Phase I mastery. Score yourself:
jax.grad handle $f: \mathbb{R}^n \to \mathbb{R}$?Score < 7/10? Review the relevant Phase I day before continuing.
That’s it. Move in the direction of steepest descent, with step size $\alpha_k$.
For $L$-smooth functions ($\|\nabla f(x) - \nabla f(y)\| \leq L\|x-y\|$):
guarantees convergence. For quadratic $f(x) = \frac{1}{2}x^TAx$, $L = \lambda_{\max}(A)$.
Optimal fixed step: $\alpha^* = \frac{2}{\lambda_{\max} + \lambda_{\min}}$
| Assumption | Rate | Iterations for $\epsilon$ accuracy |
|---|---|---|
| Convex | $O(1/k)$ | $O(1/\epsilon)$ |
| Strongly convex | $O(\rho^k)$, $\rho = \frac{\kappa-1}{\kappa+1}$ | $O(\kappa \log(1/\epsilon))$ |
| Quadratic | Exact: $O(\rho^k)$ | Proportional to $\kappa$ |
For quadratic $f(x) = \frac{1}{2}x^TAx - b^Tx$:
Example: $\kappa = 1000$ → need ~7000 iterations for $10^{-6}$ accuracy. Newton needs ~5.
Instead of fixed $\alpha$, choose $\alpha_k$ to satisfy the Armijo condition:
Start with $\alpha = 1$ (or some large value) and halve until the condition holds. Typical $c_1 = 10^{-4}$.
# See: code/week03/gradient_descent.py
Implement GD with fixed step size. Test on $f(x) = \frac{1}{2}x^TAx$ for various $\kappa$.
Implement GD with Armijo backtracking. Compare convergence with fixed step on Rosenbrock.
Test on: (a) quadratic, (b) Rosenbrock $f = (1-x)^2 + 100(y-x^2)^2$, (c) log-sum-exp.
Plot $f(x_k) - f^*$ vs iteration for each test function and each step-size strategy.
Vary $\kappa \in \{1, 10, 100, 1000\}$: show GD iterations grow linearly with $\kappa$.
Problem: For $f(x) = \frac{1}{2}x^T\text{diag}(\lambda_1, \lambda_2)x$ with $\lambda_1 = 1, \lambda_2 = \kappa$, show that GD with optimal step size $\alpha = 2/(\kappa + 1)$ satisfies $f(x_k) \leq \left(\frac{\kappa-1}{\kappa+1}\right)^{2k} f(x_0)$.
Solution: In each eigendirection: $x_k^{(i)} = (1 - \alpha\lambda_i)^k x_0^{(i)}$. With $\alpha = 2/(\kappa+1)$: for $\lambda_1 = 1$, ratio $= 1 - 2/(\kappa+1) = (\kappa-1)/(\kappa+1) \equiv \rho$. For $\lambda_2 = \kappa$, ratio $= 1 - 2\kappa/(\kappa+1) = -(\kappa-1)/(\kappa+1) = -\rho$. So $|\text{ratio}| = \rho$ in both directions, and $f(x_k)/f(x_0) \leq \rho^{2k}$.
Problem: Run GD on $f(x) = |x_1| + |x_2|$ (L1 norm, non-smooth at origin). What happens? Why? What’s the fix?
Solution: The gradient $\nabla f = [\text{sign}(x_1), \text{sign}(x_2)]$ is discontinuous at the origin and magnitude-1 everywhere else. GD with fixed step oscillates across the origin without converging. With diminishing step size ($\alpha_k = 1/k$), it converges but slowly. The correct approach is the proximal gradient method: $x_{k+1} = \text{prox}_{\alpha\|\cdot\|_1}(x_k - \alpha \nabla g(x_k))$ where $g$ is the smooth part. The prox of L1 is soft-thresholding: $\text{prox}_{\alpha\|\cdot\|_1}(v)_i = \text{sign}(v_i)\max(|v_i| - \alpha, 0)$.
Problem: Standard GD uses the Euclidean norm for “steepest descent.” Show that using the norm $\|v\|_P = \sqrt{v^TPv}$ where $P \succ 0$ gives the update $x_{k+1} = x_k - \alpha P^{-1}\nabla f(x_k)$. Choosing $P = H(x_k)$ recovers Newton’s method.
Solution: Steepest descent in $\|\cdot\|_P$: $\Delta x = \arg\min_{\|v\|_P \leq 1} \nabla f^T v$. By Cauchy-Schwarz in $P$-norm: $\nabla f^T v \geq -\|\nabla f\|_{P^{-1}} \|v\|_P$. Equality at $v = -P^{-1}\nabla f / \|P^{-1}\nabla f\|_P$. So steepest descent direction is $-P^{-1}\nabla f$. With $P = H$: direction is $-H^{-1}\nabla f$ = Newton step. This is the preconditioned gradient — connecting Day 12’s preconditioning to optimization.