Skip to content

Continuous modified second kind Bessel function #3306

Description

@currocam

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:

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

Activity

  1. drezap commented on Apr 23, 2026

    @drezap

    @currocam This is something that I could do. Are you hiring?

  2. SteveBronder commented on Apr 23, 2026

    @SteveBronder
    Collaborator

    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

  3. currocam commented on Apr 23, 2026

    @currocam
    Author

    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

  4. bgoodri commented on Apr 24, 2026

    @bgoodri
    Contributor

    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.

  5. WardBrian commented on Sep 1, 2026

    @WardBrian
    Member

    I believe @jaburgoyne was also interested in this function (forgive me if it was a different related one)

  6. sakrejda commented on Sep 19, 2026

    @sakrejda
    Contributor

    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.html

  7. bgoodri commented on Sep 19, 2026

    @bgoodri
    Contributor

    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 .

  8. WardBrian commented on Oct 8, 2026

    @WardBrian
    Member

    : 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

  9. avehtari commented on Oct 8, 2026

    @avehtari
    Member

    Sorry, my mistake trusting Claude without actually implementing the PR :/. I removed that comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions