Repository navigation
Continuous modified second kind Bessel function #3306
Description
Activity
@currocam This is something that I could do. Are you hiring?
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
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
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.I believe @jaburgoyne was also interested in this function (forgive me if it was a different related one)
Unless I missed something the underlying function used here actually accepts continuous arguments:
return boost::math::cyl_neumann(v, z); Meat and potatoes in Boost here where types are calculated:
https://codebrowser.dev/quantlib/include/boost/math/special_functions/bessel.hpp.htmlIt 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 .: 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
Sorry, my mistake trusting Claude without actually implementing the PR :/. I removed that comment
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 sonuis 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
nuis a function of your parameter of interest.I’ve ported my Stan models to Turing / Julia so I could use:
https://github.com/cgeoga/BesselK.jl
Which implements exactly this feature.
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
https://github.com/stan-dev/stan/wiki/Contributing-New-Functions-to-Stan
such
https://mc-stan.org/math/
Best,
Curro