Phase I — Mathematical Foundations | Week 2 | 2.5 hours Trust but verify: every hand-derived Jacobian should be checked numerically.
Ceres provides NumericDiffCostFunction as a fallback when analytic or auto-diff Jacobians aren’t available. Understanding the accuracy limits of numerical differentiation tells you why Ceres auto-diff is almost always preferred, and why the numerical Jacobian checker is indispensable for debugging.
First-order accurate. Error has two components: - Truncation error: $O(h)$ — decreases as $h \to 0$ - Rounding error: $O(\epsilon_{\text{mach}} / h)$ — increases as $h \to 0$
Second-order accurate. Two function evaluations per component.
No cancellation error! Works because $f(x+ih) = f(x) + ihf'(x) - \frac{h^2}{2}f''(x) + \ldots$
The imaginary part gives $hf'(x) + O(h^3)$. Since there’s no subtraction of nearly-equal numbers, $h$ can be tiny ($10^{-30}$) without rounding problems.
Limitation: Requires $f$ to be implementable with complex arithmetic (no abs, max, boolean branches on values).
| Method | Optimal $h$ | Achievable error |
|---|---|---|
| Forward | $\sqrt{\epsilon_{\text{mach}}} \approx 10^{-8}$ | $\sqrt{\epsilon} \approx 10^{-8}$ |
| Central | $\epsilon^{1/3} \approx 10^{-5.3}$ | $\epsilon^{2/3} \approx 10^{-10.7}$ |
| Complex-step | As small as you want ($10^{-30}$) | Machine precision $\approx 10^{-16}$ |
For $f: \mathbb{R}^n \to \mathbb{R}^m$, the Jacobian $J \in \mathbb{R}^{m \times n}$ requires: - Forward/central: $n$ (or $2n$) function evaluations (perturb each input dimension) - Cost: $O(mn)$ per function evaluation → total $O(mn^2)$
This is why AD (Day 11) is preferred for large $n$.
# See: code/week02/numerical_differentiation.py
Plot error vs $h$ for forward, central, and complex-step on $f(x) = \sin(x)$. Show the V-shaped error curve.
For each method, sweep $h$ from $10^{-1}$ to $10^{-16}$ and find the $h$ that minimizes error.
Implement a complex-step Jacobian for vector functions $f: \mathbb{R}^n \to \mathbb{R}^m$.
Test all three methods on $f(x) = x + 10^{-12}$ near $x=1$. The gradient is 1, but numerical methods struggle when $f(x)$ values are very close.
NumericDiffCostFunction uses central differences by default. What step size does it use?Problem: Implement a gradient checker that automatically selects the optimal step size $h$ for each component by monitoring the error vs $h$ curve. Start with $h = 10^{-1}$ and halve until the error starts increasing (rounding regime).
Solution: For each component $i$: compute forward differences with $h, h/2, h/4, \ldots$. The sequence of derivative estimates converges, then diverges as rounding takes over. The optimal $h$ is where the Richardson extrapolation error estimate $|D(h) - D(h/2)|$ is minimized. In practice, starting with $h = |x_i| \cdot 10^{-4}$ (or $10^{-4}$ if $x_i = 0$) and using central differences works well. Ceres uses epsilon = sqrt(machine_epsilon) * max(abs(x_i), 1).
Problem: Compute the Hessian numerically. (a) Use the formula $H_{ij} \approx \frac{f(x+h e_i+h e_j)-f(x+h e_i)-f(x+h e_j)+f(x)}{h^2}$. (b) What is its error order? (c) How many function evaluations for the full Hessian? (d) Can you use the complex-step trick?
Solution: (a) This is the second-order forward difference for mixed partials. (b) Error is $O(h)$ — first-order only. For $O(h^2)$, use the central formula: $\frac{f(x+h e_i+h e_j)-f(x+h e_i-h e_j)-f(x-h e_i+h e_j)+f(x-h e_i-h e_j)}{4h^2}$. (c) Forward: $n(n+1)/2 + 1$ evaluations. Central: $2n(n+1)$ evaluations. (d) Complex-step can compute the Hessian using a “hyper-dual” number approach or by applying complex-step to the gradient (which itself can be computed analytically).
Problem: You’ve written a Ceres cost function with an analytic Jacobian. Before deploying, you want to verify it. Design a test that: (1) evaluates the analytic Jacobian, (2) computes a numerical Jacobian, (3) reports the maximum relative error, (4) flags components where the error exceeds a threshold.
Solution: In Ceres, use ceres::GradientChecker which does exactly this. In Python, the pattern is: for each parameter block, perturb each element by $h = \sqrt{\epsilon} \cdot \max(|x_i|, 1)$, compute central differences, compare to the analytic Jacobian. Report per-element relative errors $|J_a - J_n| / \max(|J_a|, |J_n|, 1)$. Flag if any exceed $10^{-4}$ (for central differences). This is the single most important debugging tool for optimization — a wrong Jacobian is the #1 cause of solver failure.