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
752 stars 188 forks source link

Add distribution quantile functions (inverse CDFs) #2517

Open andrjohns opened 3 years ago

andrjohns commented 3 years ago

Description

I'm opening this as a reference issue for implementing the distribution quantile functions, since there are likely to be quite a few PRs along the way.

I'll be starting with the distributions in the boost documentation

Points for discussion

Let me know if there are any suggestions or any issues I'm likely to run into

Current Version:

v4.1.0

andrjohns commented 2 years ago

@spinkney I was looking at the quantile function for the negative-binomial, and it appears to require the inverse of the incomplete beta function, but when the function is inverted on the b parameter (the ibeta_invb function).

The tricky part is that this doesn't appear to have a closed-form, and is instead computed numerically through a root-finding algorithm, so I haven't been able to find any kind of derivatives for the inputs. At the moment it looks like we'll have to resort to using finite-differencing in Boost, but this is obviously not ideal. Any chance you know of an alternative for the negative-binomial (or gradients for the ibeta_invb)?

spinkney commented 2 years ago

Quantile functions for discrete distributions are tricky. See https://www.boost.org/doc/libs/1_78_0/libs/math/doc/html/math_toolkit/pol_tutorial/understand_dis_quant.html.

The way Stan currently handles cdfs for discrete distributions is that the input must be an integer type. That means we cannot have a continuous quantile function. So really no derivatives.

There is a discussion in a math issue about allowing real inputs in discrete cdfs. The problem is that the real values between each integer are interpolated and there are an infinite number of ways to do that. The current design choice is to not build it in because we would effectively choose the interpolation and we'd be hiding the fact that it's truly not continuous from the definition.

If we do include the quantile functions for discrete distributions this opens up an interesting question about how we classify a noncontinuous function with real outputs. Or do we interpolate, in which case, we should allow reals into the discrete cdfs as well.

andrjohns commented 2 years ago

Ahh of course, forgot about all of that. Will just stick with the continuous distributions!