Numerical differentiation
Derivatives are everywhere in numerical computation: Newton’s method for equations needs them, optimisation algorithms follow gradients, stiff ODE solvers need Jacobians, and sensitivity analysis asks how outputs change with inputs. When a formula for the derivative is not available, or the function is a procedure rather than a formula, the derivative must be computed numerically.
Differentiation is in one sense the opposite of integration, and its numerical behaviour is opposite too. Integration averages values and is forgiving: errors in individual function values are smoothed out. Differentiation subtracts nearby values and divides by a small number: it amplifies errors. Every numerical differentiation method is a compromise between the error of approximating the derivative (which wants small steps) and the amplification of rounding errors (which wants large ones).
Choosing a method
| Your situation | Recommended | Entry |
|---|---|---|
| A function you can only evaluate, moderate accuracy | Central differences with a balanced step | Finite difference derivatives |
| Higher accuracy from function values alone | Five-point stencils, or Richardson extrapolation | Finite difference derivatives |
| Gradients, Hessians and Jacobians | Coordinate-wise central differences | Finite difference derivatives |
| Near machine precision with an error estimate, no step to choose | Ridders’ extrapolation: adaptive::derivative, jacobian, hessian | Adaptive derivatives |
| An analytic function you can evaluate at complex points | Cauchy integral formula | Complex contour integration |
| A smooth periodic function or one on an interval, sampled on a grid | Spectral differentiation | Spectral methods |
Testing against SciPy
SciPy 1.17.1’s scipy.differentiate estimates derivatives adaptively: it applies central differences with a sequence of shrinking steps and extrapolates (Richardson), stopping when the estimates agree. The library uses one fixed step chosen to balance truncation and rounding. The references come from book/generator/data/make_derivatives_refs.py; the tables give relative errors against the exact derivatives:
Table 1.1. Scalar derivatives against SciPy's adaptive `derivative`.
| function | f’: central difference | f’: five-point stencil | f’: SciPy derivative | f’’: five-point stencil | f’’: SciPy, nested |
|---|---|---|---|---|---|
| exp(x) at 1 | 8e-12 | 3e-14 | 1e-14 (2e-13 with a first step of 0.01) | 7e-11 | 5e-10 |
| sin(x) at 1 | 4e-12 | 4e-14 | 1e-14 (2e-13 with a first step of 0.01) | 2e-11 | 2e-10 |
| ln(x) at 0.1 | 1e-9 | 2e-9 | NaN (5e-15 with a first step of 0.01) | 5e-7 | 4e-13 |
| 1/(1 + 25 x^2) at 0.3 | 2e-10 | 3e-11 | 8e-11 (9e-15 with a first step of 0.01) | 7e-9 | 1e-12 |
| x^(5/2) at 2 | 5e-12 | 7e-14 | 4e-15 (1e-13 with a first step of 0.01) | 2e-12 | 5e-10 |
Table 1.2. Gradients, Jacobians and Hessians against SciPy.
| derivative (SciPy reference) | library: relative error | SciPy: relative error |
|---|---|---|
gradient of Rosenbrock’s function at (-1.2, 1) (differentiate.jacobian) | 1e-10 | 4e-16 |
its Hessian (differentiate.hessian) | 3e-9 | 2e-15 |
Jacobian of polar coordinates at (2, 0.5) (differentiate.jacobian) | 8e-12 | 1e-14 |
Adaptive extrapolation reaches 13 to 15 digits where the fixed-step formulas give 9 to 14, at the cost of many more evaluations, but it needs a sensible first step. SciPy’s default first step of 0.5 jumps past zero for at and returns NaN; the library’s step is relative to , so it stays near the point, but for the same reason it is too large for a function that varies on a scale much smaller than 1, which costs it accuracy at . The second derivatives of the five-point stencil lose more digits, as the step must be larger to keep rounding error, now divided by , in check.
In the library
The differentiation crate is lib_jsl_derivatives
(API reference).