Finite differences
A centered finite difference estimates a derivative by dividing the output difference at symmetric input points by their separation.
On this page 8 sections
01A finite difference estimates a derivative by evaluating the function at nearby inputs#
A finite difference estimates a derivative by evaluating the function at nearby inputs. It can check a derivative implementation because it uses function values rather than the same symbolic derivative rules. The result is an approximation with a step-size tradeoff.
A centered finite difference samples the function on both sides of the input. With a positive step , the endpoints are and .
Estimate by dividing the output difference by the full spacing :
For , values and give an estimate .
For at with , the centered numerator is . Divide by to get 4. The denominator is the full distance between the two sampled inputs, not the distance from the center to one sample.
With , and . Find the centered derivative estimate.
Show answer and explanation
Divide the output difference by the full spacing: .
02A finite step gives an estimate, which need not equal the exact derivative#
A finite step gives an estimate, which need not equal the exact derivative. For at with , the endpoint outputs are and . The estimate is ; the exact derivative there is .
Use both endpoint evaluations even when the value at the center is known.
A scalar response is . Estimate its derivative at with centered step .
Show answer and explanation
The endpoint outputs are and . Their difference divided by gives .
03Both endpoints must lie in the domain#
Both endpoints must lie in the domain. A centered slope across a corner need not be a derivative. For smooth functions, subtracting nearly equal floating-point outputs can lose accuracy.
Smaller steps can reduce approximation error while increasing roundoff. They do not always improve the estimate.
With , and . The calculation divides by . Repair the centered estimate.
Show answer and explanation
Divide the output difference by the full spacing: .
04A closer look#
A large step may average over curvature; an extremely small one can magnify numerical rounding when nearly equal outputs are subtracted. Finite-difference agreement is useful evidence for a gradient implementation, but one successful point does not check every input or edge case.
Subtract the two symmetric outputs and divide by 2h; the result is a finite-step estimate.
- Estimate a derivative with a centered finite difference.
Sources & further reading
- [1]Stanford CS231n: Gradient Checks ↗Stanford CS231n · Article