@@ -33,6 +33,7 @@ This package was formerly called FDM.jl. We recommend users of FDM.jl [update to
3333
3434* [ Scalar Derivatives] ( #scalar-derivatives )
3535 - [ Dealing with Singularities] ( #dealing-with-singularities )
36+ - [ Dealing with Numerical Noise] ( #dealing-with-numerical-noise )
3637 - [ Richardson Extrapolation] ( #richardson-extrapolation )
3738 - [ A Finite Difference Method on a Custom Grid] ( #a-finite-difference-method-on-a-custom-grid )
3839* [ Multivariate Derivatives] ( #multivariate-derivatives )
@@ -136,6 +137,38 @@ julia> central_fdm(5, 1, max_range=9e-1)(ε -> logdet(x .+ ε .* v), 0) - sum(1
136137- 1.283417816466681e-13
137138```
138139
140+ ### Dealing with Numerical Noise
141+
142+ It could be the case that the function ` f ` you'd like compute the derivative of
143+ suffers from numerical noise.
144+ For example, ` f(x) ` could be computed through some iterative procedure with some
145+ error tolerance ` ε ` .
146+ In such cases, finite difference estimates can fail catastrophically.
147+ To illustrate this, consider ` sin_noisy(x) = sin(x) * (1 + 1e-6 * randn()) ` .
148+ Then
149+
150+ ``` julia
151+ julia> central_fdm (5 , 1 )(sin_noisy, 1 ) - cos (1 )
152+ - 0.027016678790599657
153+ ```
154+
155+ which is a terrible performance.
156+ To deal with this, you can set the keyword argument ` factor ` , which specifies
157+ the level of numerical noise on the function evaluations relative to the
158+ machine epsilon.
159+ In this example, the relative error on the function evaluations
160+ is ` 2e-6 ` (` 1e-6 * randn() ` roughly produces a number in ` [-2e-6, 2e-6] ` )
161+ and the machine epsilon is ` eps(Float64) ≈ 2.22e-16 ` , so
162+ ` factor = 2e-6 / 2e-16 = 1e10 ` should be appropriate:
163+
164+ ``` julia
165+ julia> central_fdm (5 , 1 ; factor= 1e10 )(sin_noisy, 1 ) - cos (1 )
166+ - 1.9243663490486895e-6
167+ ```
168+
169+ As a rule of thumb, if you're dealing with numerical noise and ` Float64 ` s,
170+ ` factor = 1e6 ` is not a bad first attempt.
171+
139172### Richardson Extrapolation
140173
141174The finite difference methods defined in this package can be extrapolated using
0 commit comments