You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Continuous modified second kind Bessel function #3306
Hello,
I'm a bit unsure whether to open a new issue or comment in one of the already closed issues (see #23 ), so please redirect me if needed.
I'm interested in using the modified second-kind Bessel function inside Stan and taking gradients with respect to the order parameter nu. My understanding is that it would require changing the signature of the function so nu is real and define efficient derivatives (see #1112).
It turns out that such BesselK function comes up often in population genetic predictions, and I’m not aware of ways to avoid this if, for example, the order nu is a function of your parameter of interest.
I’ve ported my Stan models to Turing / Julia so I could use:
I’m now thinking that perhaps one could port the Julia implementation (described in https://arxiv.org/abs/2201.00090) and implement it in the Stan math library.
I’m not very mathy, and I’m unsure how this feature relates to the rest of Bessel functions. From the API perspective, it doesn’t feel elegant to only implement the derivatives to one of the functions.
If that’s not an issue, I’m happy to make a PR provided you give me a bit of help on how to start. It seems like URL links in this wiki-page are broken
Hi! I know @bgoodri was looking at these for some time. I think the main issue was a difficulty in the calculation for the reverse mode adjoint. Trying to read through #1121 and I do not think any one has tried to implement @spinkney 's calculations in #1112 . We just updated the docs site which has a new contributor guide. Happy to help and jump on a call if you have any questions
Note that in the potential applications that I am most aware of having a function like log_besselK would be necessary to avoid some overflow or underflow issues. There have been a bunch of papers and some code implementations over the years, but we never quite succeeded in getting something worthwhile into Stan Math.
It does, but the Stan language only accepts integer values for v because it does not know how to differentiate with respect to a var. But the greater need is for the logarithm thereof because it can easily overflow, which is #1121 .
: a C++ port written generically in the scalar type can be run with Stan's forward-mode type fvar to compute dK/dnu, and the reverse-mode node then stores that number.
I don't think we can have code in rev depend on forward mode without it being very painful
Activity
drezap commentedon Apr 23, 2026
@currocam This is something that I could do. Are you hiring?
SteveBronder commentedon Apr 23, 2026
Hi! I know @bgoodri was looking at these for some time. I think the main issue was a difficulty in the calculation for the reverse mode adjoint. Trying to read through #1121 and I do not think any one has tried to implement @spinkney 's calculations in #1112 . We just updated the docs site which has a new contributor guide. Happy to help and jump on a call if you have any questions
https://mc-stan.org/math/md_doxygen_2contributor__help__pages_2getting__started.html
currocam commentedon Apr 23, 2026
Gotcha, i see now that the implementation I refered to only implements forward-mode.
Thanks for the pointing out the contributor guide! I will look into this
bgoodri commentedon Apr 24, 2026
Note that in the potential applications that I am most aware of having a function like
log_besselKwould be necessary to avoid some overflow or underflow issues. There have been a bunch of papers and some code implementations over the years, but we never quite succeeded in getting something worthwhile into Stan Math.WardBrian commentedon Sep 1, 2026
I believe @jaburgoyne was also interested in this function (forgive me if it was a different related one)
sakrejda commentedon Sep 19, 2026
Unless I missed something the underlying function used here actually accepts continuous arguments:
math/stan/math/prim/fun/bessel_second_kind.hpp
Line 42 in 5252d51
Meat and potatoes in Boost here where types are calculated:
https://codebrowser.dev/quantlib/include/boost/math/special_functions/bessel.hpp.html
bgoodri commentedon Sep 19, 2026
It does, but the Stan language only accepts integer values for
vbecause it does not know how to differentiate with respect to avar. But the greater need is for the logarithm thereof because it can easily overflow, which is #1121 .WardBrian commentedon Oct 8, 2026
I don't think we can have code in rev depend on forward mode without it being very painful
avehtari commentedon Oct 8, 2026
Sorry, my mistake trusting Claude without actually implementing the PR :/. I removed that comment