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
737 stars 185 forks source link

integrate_1d fragile #2114

Closed bbbales2 closed 3 years ago

bbbales2 commented 3 years ago

Description

A few questions have come up on the forums lately regarding integrate_1d where the integrator fails and it is difficult to debug what is going on.

Specifically I am talking about:

  1. https://discourse.mc-stan.org/t/compilation-error-on-rstan/17972/

  2. https://discourse.mc-stan.org/t/integrate-1d-errors-in-stan/17975/9

and

  1. https://discourse.mc-stan.org/t/difference-in-behavior-between-integrate-1d-and-integrate/18259/

On top of being difficult to debug, in the last problem we ended up switching to the ODE integrator to solve the problem. It is entirely possible to ODE integrator is just returning the wrong integral, but debugging the two functions via expose_stan_functions suggests it's doing okay. Anyway, switching to the ODE integrator seems like the wrong thing. In the second thread the person implemented their own custom trapezoid quadrature.

I think the double exponential quadrature we're using is just a bit fragile with combined with reverse mode autodiff. The Boost docs warn about how aggressive these methods are about evaluating functions at endpoints (where numerics blow up): https://www.boost.org/doc/libs/1_74_0/libs/math/doc/html/math_toolkit/double_exponential/de_caveats.html

At this point we need some switches for people to press so they can figure out how to get their quadratures working. Maybe:

  1. Enabling other quadrature techniques (Boost's trapezoid quadrature in particular)

  2. Making it easier to control error in integrals, either by doing a fix number of integration steps or just making it easier to adjust the tolerances

Current Version:

v3.3.0

bbbales2 commented 3 years ago

Hopefully this is resolved by the temporary fix in #2183. The follow up issue to this is #2193 (once there's a fix in boost the #2183 patch can be reversed).