-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
ENH: add Wright's generalized Bessel function #11313
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
Conversation
I will note that a pure Python implementation is basically a nonstarter in special. Probably best to discuss this on the mailing list before proceeding further here. |
@lorentzenchr this will need unit tests. Given that this calls You'll want to consider typical case inputs as well as |
I Agree. |
@rlucas7 I'm happy to add tests once it is settled that this PR is not for nothing. @person142 @rlucas7 Why is a pure python implementation a nonstarter? My plan:
|
Main reasons are:
|
@person142 Thank you very much for your explanations! |
So this is where we might want to keep the end goal in mind-if we add this as a public function in special then it's going to have to be a ufunc (for API consistency reasons), so On the other hand, if we say "this is just for getting at the Tweedie distributions, where I was trying to get at something similar in my response on the mailing list:
|
🤔 Right now, I like the private special function. |
@person142 Meanwhile, I think this could start as a private function with limited range for arguments, e.g. all greater equal 0, but still Cython so that it would be easier to make it public. Currently, I'm working on a stable form for 0<z<=1. Large z seems to be challenging. |
Are you working towards support for complex values ?
If so, I’d recommend focus on real values only. Tweedie distribution is real valued and I haven’t seen any references to any complex values applications.
… On Jan 10, 2020, at 1:36 PM, Christian Lorentzen ***@***.***> wrote:
@person142 Meanwhile, I think this could start as a private function with limited range for arguments, e.g. all greater equal 0, but still Cython so that it would be easier to make it public.
Currently, I'm working on a stable form for 0<z<=1. Large z seems to be challenging.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.
|
Exactly, for the moment all values real, and even positive: (a, b, x) >= 0. Makes it a lot easier. |
For what it's worth, here are some functions that use
For example,
|
@WarrenWeckesser Thank you for the MPMATH examples. When I played around with my mpmath implementation, I noticed that |
@lorentzenchr yeah, an asymptotic series will almost surely be needed. Feel free to ping me about implementation details if it's not something you're familiar with. Other than that, let me know when this is ready for more review. I'm going to add the "needs work" label so that it stays off my review queue until then. |
@person142 Thanks for your guidance.
I have 4 questions right now:
Would I better address such questions on the mailing list? |
Ideally relative error
That's always a tough question. Sometimes you can prove things about rates of convergence, and that informs what domain you can use. See e.g. https://github.com/scipy/scipy/blob/master/scipy/special/_hypergeometric.pxd#L65 for an example of that. Sometimes you compute across a wide range of parameters using all methods, plot where the accuracy is high for each method, and use that to figure out your regions. See e.g. https://github.com/scipy/scipy/blob/master/scipy/special/_precompute/struve_convergence.py for an example of that. Another technique is computing with multiple methods at runtime and keeping running error estimates, and then returning the result that has the lowest error estimate. Obviously this one really slows down the computation, so it should be more of a last resort.
Using
Some tricks here:
|
- add 5th order in a - use polevl - use Horner's scheme for polynomials in x - precompute constants C[..] numerically - better choosen order parameter - better range of validity - added tests for new range limits
- test more data points - print failing data points in _precompute/wright_bessel_data.py
- fine tuning for choice of eps in integral representation - higher bound for use of asymptotic expansion meaning the integral representation is used more often
@person142 I addressed all of your comments, extended tests a lot and made improvements for extreme arguments. Open to me is, probably, to return more To have automatic Cython interface, as you proposed, the function name is now without leading The failing test is about too long lines in |
I'd put it in the Bessel functions section. |
There it is at the moment. |
@WarrenWeckesser @person142 May I kindly ping you. |
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.
Probably the best thing to do is merge and see what happens in the wild.
I'll leave it open for a few days in case someone else wants to comment; ping me again in a few days if I haven't merged it by then. (Looks like a lint error to fix in the meantime.)
@person142 Gentle ping. |
The lint failure is real I think-note that it's running a stricter linting check on just your diff (we added that so that we could converge towards cleaner code while not having big refactoring PRs). |
All green again. |
Awesome, in it goes. Thanks for sticking with it @lorentzenchr! |
@person142 Hitting the merge button for a +5000 line diff takes some courage/trust:smirk: I hope this function will help in a few places in the ecosytem around scipy. |
Awesome! @lorentzenchr I added an entry in https://github.com/scipy/scipy/wiki/Release-note-entries-for-SciPy-1.7.0#scipyspecial-improvements, if there's more to say please feel free to edit. |
Would anyone be interested in adding an enlightening example or two to the |
@WarrenWeckesser I opened #13738 and hope that this is what you had in mind with enlightening.:smirk: |
In case someone is interested, I wrote a blog post about this function with a focus on the integral representation and the magic with the free parameter: https://lorentzen.ch/index.php/2024/06/17/a-tweedie-trilogy-part-iii-from-wrights-generalized-bessel-function-to-tweedies-compound-poisson-distribution/ |
Reference issue
This function is one crucial step towards #11291. See also mailing list thread https://mail.python.org/pipermail/scipy-dev/2020-January/023918.html.
What does this implement/fix?
This PR adds Wright's generalized Bessel function as
scipy.special.wright_bessel
, see https://dlmf.nist.gov/10.46.E1.Additional information.
This is a pure Python implementation that aims for correctness and not for speed.I did not find any public implementation as a reference.As it is an entire function, the series representation is always valid.
I tried asymptotic expansions for large argument z, without any luck.Edit:
So far, only non-negative values of rho=a, beta=b and z=x are implemented. There are 5 different approaches depending on the ranges of the arguments.
Involves gamma funtions in each term.
Suitable for large x while still small a and b.
References:
dispersion model densities. Statistics and Computing 15 (2005): 267-280.
function. Proc. London Math. Soc. (2) 38, pp. 257-270.
https://doi.org/10.1112/plms/s2-38.1.257
Mathematica Aeterna, Vol. 7, 2017, no. 4, 381 - 406,
https://arxiv.org/abs/1711.03006
the Real Arguments' Values, Fractional Calculus and Applied Analysis 11(1)
http://sci-gems.math.bas.bg/jspui/bitstream/10525/1298/1/fcaa-vol11-num1-2008-57p-75p.pdf