stan-dev / math

The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
https://mc-stan.org
BSD 3-Clause "New" or "Revised" License
758 stars 188 forks source link

Use finite-differences where forward-mode not implementable #2842

Open andrjohns opened 2 years ago

andrjohns commented 2 years ago

Description

As discussed over in #2839, the ODE methods are not fvar<T> compatible, and it would be a significant amount of work to do so. This means that any downstream methods depending on fvar<> or higher-order autodiff will fail to compile, a current example of this is the new $hessian() method in cmdstanr.

In these instances it would be great to have a method that "works", even if it's slow - better to have in a limited form than to not have at all (imo).

Boost Math has existing optimised routines for finite-differencing, including the use of the complex step approximation for any complex-compatible functions (allowing for estimating the derivative with a single function evaluation)

Current Version:

v4.4.0

WardBrian commented 7 months ago

Here's a list of functions exposed in the Stan language which would be candidates to use with stan/math/fwd/functor/finite_diff.hpp: