-
Notifications
You must be signed in to change notification settings - Fork 114
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
Implement Kve Op and Kv helper #1081
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1081 +/- ##
=======================================
Coverage 82.10% 82.11%
=======================================
Files 183 183
Lines 47930 47959 +29
Branches 8633 8635 +2
=======================================
+ Hits 39354 39381 +27
- Misses 6410 6411 +1
- Partials 2166 2167 +1
|
Is it worth exposing Kve as well? I think you can use it to prevent some underflow for large argument in your logp in pymc-devs/pymc-extras#389 |
You think so? Do we even need kv then or should it just be a helper around kve? Reducing core Ops is always nice |
Maybe not! Include a stabilising rewrite for Kv -> Kve * exp(-|x|) |
I notice TFP also has log_bessel_kve which should be more numerically stable again in this use case. That's probably a good candidate as a rewrite target. |
Kv = Kve * exp(x) not -x. I'm not sure it's like the Iv/Ive combo. There must be cases where kv is preferred otherwise why would it be offered? If it's never preferred there's no reason to have it offered. Re log, I've also came across a paper that implements a stable form iteratively (see linked discussion), but seems like quite some complexity. I would wait until there's demand for it. |
The scipy page shows an example of underflowing earlier, so you're probably right. https://docs.scipy.org/doc/scipy/reference/generated/scipy.special.kve.html Is kv more stable for smaller x, or does kve win across the whole range. If so I'll just ditch kv altogether |
Other way around is it not? Kv ~ exp(-x)/sqrt(z) for large z, so Kve = Kv * exp(x) is the "non exponential" part
I'm sure there must be but at least since we're mostly dealing with logps, I'd say we'll want to factor out the exponential term most of the time for stability and kve will probably get much more use. Maybe a good argument for having Kve and not Kv is someone without experience with numerical analysis is more likely to write something stable if they only have Kve 😀
For a log(kve) specialisation, we could use the TFP implementation (which claims to be more stable) in jax and fallback to the naive implementation in scipy/c. That said, for the range of values I tried with the TFP log(kve), I didn't see any difference between it and the naive implementation, so maybe not worth it. |
I didn't think it through with 1/exp(x) = exp(-x) :P
Those are different things. JAX/TFP cannot be used instead of scipy, they are different backends. But yes we can offer kve, and kv as |
@dehorsley I switched so we have a core kve, and a helper kv based on kve. I also added a stabilization rewrite for A specialization for Should we do something similar for Iv/Ive (and remove the core Iv Op)? If so, I'll open an issue for this, it need not block this PR |
Btw @dehorsley if you want to hang out in the devs discord server here is a link: https://discord.gg/vYkmxNuF Say hi when / if you join so we can grant you access to the private channels |
Sounds good to me!
Sure. For reference, it looks like the TFP implementation actually calculates log(Kve)/log(Ive), then applies Incidentally, it seems like TFP's Kve implementation is more numerically stable than SciPy's:
Yeah I think so. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just a couple of nit-picks 😁
Thanks @dehorsley, nits addressed |
Needed for pymc-devs/pymc-extras#389
Related to #1038
CC @AuguB
📚 Documentation preview 📚: https://pytensor--1081.org.readthedocs.build/en/1081/