-
-
Notifications
You must be signed in to change notification settings - Fork 183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inefficient modified bessel function and von_mises_lpdf derivative calculation #3008
Comments
Fantastic catch, thanks! I'll put together a PR for this now so it will be in the 2.35 release |
Awesome, thanks! I was just about to try to figure out how to do that myself, but never having contributed before, it was gonna take some time :) |
No worries! Did you want me to leave it for you still? Or just tag you once the PR is up so you can see what would be needed for next time? |
And to clarify, what would be needed for a PR would be to:
|
Yes, please do it and tag me! I opened a second issue for a more general application for other models that use the modified bessel function of order 0 (which is what actually led me to discover this speed up), so I can perhaps tackle that after seeing how you do it here and reading the docs. Although in the new issue I guess first the devs have to agree on the corrects implementation. |
Description
The current implementation of the von_mises_lpdf can be sped up ~4 times with a minor change to the derivative calculation that doesn't change the numerical output in any way.
The relevant code is:
For integer order,$I_n(x) = I_{-n}(x)$ (see 10.27.1: https://dlmf.nist.gov/10.27). Thus
modified_bessel_first_kind(-1, kappa_val)
is the same asmodified_bessel_first_kind(1, kappa_val)
.modified_bessel_first_kind() calls boost::math::cyl_bessel_i(), which has a special optimizations for order 0 and 1 (see. When cyl_bessel_i() is called with order -1, it uses a much more inefficient calculation.
Calculating the modified_bessel_function is the most expensive part of calculating the von_mises_lpdf, taking ~80% of the time.
Example
The following speed test shows that both functions are equal, but modified_bessel_first_kind(1,x) is ~10 times faster:
Expected Output
Change modified_bessel_first_kind(-1, kappa_val)
to
modified_bessel_first_kind(1, kappa_val) hereCurrent Version:
v4.8.0
The text was updated successfully, but these errors were encountered: