Minimal censored Gamma with VERY bad performance

But not the derivatives, right?

Ah yes, I can see the issue is likely coming from grad_reg_inc_gamma, might be more important to identify where that’s breaking

See the issue. Looks like better derivatives were calculated for gamma_p in an old PR but this was never implemented in the gamma_lcdf.

2 Likes

Ah got it, thanks!

@aammd this issue was just fixed courtesy of @andrjohns in PR Numerically Stable gamma_lcdf Gradients by andrjohns · Pull Request #2767 · stan-dev/math · GitHub.

The (log) cdfs can be numerically difficult to handle. The Gamma cdf in particular uses functions which can all be described by the hypergeometric family of functions which have no closed form solutions. The definitions lead to infinite series and rely on computational algorithms to evaluate quickly. In Stan, we further need the derivatives of these possibly compounding errors upon errors. In the case of the Gamma lcdf the grad_reg_inc_gamma function is used to determine the gradient. This function was unstable. Luckily, @sakrejda had a more stable version used in the cdf but was never updated in the lcdf.

Lastly, we found a slight improvement to the already improved method eeking out a bit more efficiency. Once the final 2.30 version is released please try this model again and let us know how it went.

5 Likes

This is incredible! thank you @spinkney and @andrjohns and everyone else who contributed here! This Stan community is amazing 💚

3 Likes

@aammd please try this model on 2.30. For me it ran super fast and recovered the parameters.

2 Likes

@spinkney , I can confirm that with 2.30.0 it works like a charm! Ran it on my machine and recovered the parameters with good diagnostics. Thanks again all!

3 Likes