-
Notifications
You must be signed in to change notification settings - Fork 428
Use native samplers for Poisson distribution #1021
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
Codecov Report
@@ Coverage Diff @@
## master #1021 +/- ##
==========================================
+ Coverage 77.92% 78.17% +0.25%
==========================================
Files 112 112
Lines 5391 5325 -66
==========================================
- Hits 4201 4163 -38
+ Misses 1190 1162 -28
Continue to review full report at Codecov.
|
Samplers are not my area, but do we have a way to test for correctness of the sampler? |
It seems they are tested in Distributions.jl/test/samplers.jl Lines 62 to 74 in 802a65b
|
LGTM, I'll wait for another review before merging though, to get more educated opinions |
|
||
if G >= 0.0 | ||
K = floor(Int,G) | ||
if G >= zero(G) |
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.
You can just compare with 0
?
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.
I guess that could be used here as well. I think it makes only a different if one works with non-standard number types such as unitful numbers - e.g., u"1.0m" > 0
errors whereas u"1.0m" > zero(u"1.0m")
works as expected.
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.
Ah, good point, let's be defensive then.
else # Case B | ||
# Ahrens & Dieter use a sequential method for tabulating and looking up quantiles. | ||
# TODO: check which is more efficient. | ||
return quantile(d,rand(rng)) |
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.
So this is the sub approach we will not use anymore?
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.
Yes, instead of the quantile-based generation this PR uses the PoissonCountSampler
for small rates which just counts exponentially distributed random numbers.
src/univariate/discrete/poisson.jl
Outdated
px = -μ | ||
py = μ^K/factorial(K) # replace with loopup? | ||
function sampler(d::Poisson) | ||
if rate(d) < 6 |
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.
this magic threshold appears twice, make it a constant?
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.
I guess it's even better to not define rand
at all and remove the code duplication.
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.
bump on this? Maybe naming the constant is still a nice idea
Co-Authored-By: Moritz Schauer <moritzschauer@web.de>
@mschauer @devmotion good to merge? |
Yes 👍 I guess, I adjusted the PR according to your suggestions and comments |
thanks for the PR :) |
I noticed that
sampler
is not implemented for Poisson distributions although two native samplersPoissonCountSampler
andPoissonADSampler
exist. Moreover, currently R is used for sampling from Poisson distributions, which is quite slow. For now I used the same cut-off value of 6 for both samplers, as chosen in PoissonRandom.jl.I repeated the benchmarks
from SciML/PoissonRandom.jl#6 with this PR. I get
and
Using the native samplers leads to a significant speed-up and a performance which is on par with PoissonRandom.jl.