Numerical Derivatives
Intuition
The derivative is the slope of the tangent line at . We cannot draw a tangent with a computer, but we can draw a secant through two nearby points and use its slope. The closer the points, the better the approximation — up to the limits of floating-point arithmetic.
Precise explanation
Definition: .
- Forward difference: , error proportional to (first order).
- Central difference: , error proportional to (second order), because the terms in the Taylor expansions cancel.
Too-small causes cancellation error: and agree in most digits, so their difference keeps few significant digits. With 64-bit floats, is a good default for central differences.
Figure (original): the central-difference secant through x−h and x+h is nearly parallel to the tangent at x.
Worked example
at (exact ), : forward: ; central: .
Common mistakes
- Using "for accuracy" — rounding error dominates.
- Dividing the central difference by instead of .
- Expecting an exact match with the analytic derivative; always compare with a tolerance.
Why this matters
Finite differences check that hand-written gradients are correct (DL-13) and power the numerical gradient (MA-14) and integrated-gradients approximations (XA-14).
References
- Mathematics for Machine Learning — Marc Peter Deisenroth, A. Aldo Faisal, Cheng Soon Ong (Cambridge University Press) (
S009) - Dive into Deep Learning — Aston Zhang, Zachary C. Lipton, Mu Li, Alexander J. Smola (
S013)