# Brent algorithm supporting automatic differentiation

I painfully implemented automatic differentiation (AD) and a Gauss-Kronrod numerical integration routine working with it with AD. (Fully tested etc, perfectly working.) Needing a root finding algorithm for the Jamshidian's trick for a work around swaptions, I implemented a Brent algorithm perfectly working in the classical case without AD but not with it (wrong derivative of the parametric root).

As far as I know instrumentated code doesn't differentiate well if statements when they apply to functions we want to auto differentiate. Is there any trivial pointer I should check before debugging my auto diff code, or maybe any library supporting auto diff and implementing Brent ?

• This is one of the reasons I have avoided Brent in favour of Newton-Raphson, that as well as finding it harder to constrain to an initial interval (for Brent) than find a reasonable initial guess (for Newton). One thing I do when using Newton is solve the solution using floats and then as a last step perform one more iteration with AD to capture the sensitivities. Perhaps you can use Brent with floats to solve the problem and then at solution use one Newton iteration to capture the sensitivities?
– Attack68
Commented Apr 29 at 7:22
• @Attack68 : that was indeed my policy would I not solve my bug/problem, but I would like it to work cross template parameter specialization. Commented Apr 29 at 7:48
• You haven't mentioned whether you're doing it in Python, or C++, or something else. FWIW, I asked Google's Gemini AI: "is there open source library supporting automatic differentiation and implementing Brent solver". Gemini listed Google's own TensorFlow and PyTorch as supporting AD; also mentioned minimize_brent in SciPy's optimize; and printed out Brent code for TensorFlow, which I haven't tried running. It may be worth trying to run this TF code and see whether TF's AD works correctly. Commented Apr 29 at 13:33
• I am using C++. As far as I remember TF has a C++ lib and py wrappers so I could try that for benchmarking only, as I am a bit (in fact not just a bit, but all lot) capped on my DLLs sizes ... Commented Apr 29 at 15:10
• Please post if you discover a solution yourself. I am interested.
– Attack68
Commented Apr 29 at 16:55