Improved finite differencing #4
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I made some improvements in the finite differencing. Here are some brief explanations:
2 * sqrt(1e-12) * (1 + norm(x)), forabs(x) < 1is approximately equal to 2e-6. The formula I used previously,eps(max(1.0, abs(x)))^(1/3), forabs(x) < 1is equal to 6e-6, not very different. So contrary to your in-code comment, the smallxbehavior actually can't be very different for these two.1e-12implicitly assumesFloat64, whereas using the type-dispatched version ofepsdoes not. I definitely run optimization withFloat32s sometimes (when using huge datasets where all the computations areFloat32).I also removed the dtype parameter from
finite_difference_hessian(f, x)because you basically have to use centered differencing for direct Hessian evaluation.For your cases in
test/finite_difference.jl, I think you'll mostly see improved accuracy. For central differencing the change is quite modest (usually less than one order of magnitude), but for forward differencing and the Hessian it can be dramatic. For example, with the old versionand with the new
Presumably it should also be more robust in real-world problems, although that remains to be seen under usage.