-
Notifications
You must be signed in to change notification settings - Fork 422
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
fix type stability of sampling from Chisq
, TDist
, Gamma
#1885
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1885 +/- ##
=======================================
Coverage 85.99% 85.99%
=======================================
Files 144 144
Lines 8666 8666
=======================================
Hits 7452 7452
Misses 1214 1214 ☔ View full report in Codecov by Sentry. |
Should probably add method There are many side effects associated with changing Gamma. The following list is not exhaustive, but I think they will be impacted:
Chisq will have some side effects too, e.g.:
|
Hi @quildtide , Okay so to be precise, I'll do the following:
Would that be sufficient? |
Not a maintainer, so don't take my word for granted. If the route this pull request takes is chosen, then the things you propose doing would indeed be a good idea. Beta has two sampling paths, IIRC, where it sometimes uses a Gamma sampler, and sometimes uses a different sampler, so you will probably need to alter the other path too, if the Gamma path is returning partype values now (I think it is). I know that some of the maintainers have pushed back to some degree in the past on making |
@@ -105,7 +105,7 @@ cf(d::Exponential, t::Real) = 1/(1 - t * im * scale(d)) | |||
|
|||
|
|||
#### Sampling | |||
rand(rng::AbstractRNG, d::Exponential) = xval(d, randexp(rng)) | |||
rand(rng::AbstractRNG, d::Exponential{T}) where {T} = xval(d, randexp(rng, T)) |
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 is a slightly different case and easily broken (e.g., when T
is not a floating point number type). In the TDist
and Gamma
case we just try to avoid promotions of a sample from another rand
call, whereas this case goes deeper into the question of how rand
should behave wrt parameters etc. (see also #1433 (comment)).
rand(rng::AbstractRNG, d::Exponential{T}) where {T} = xval(d, randexp(rng, T)) | |
rand(rng::AbstractRNG, d::Exponential) = xval(d, randexp(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.
If we do this though, the return type of rand(Gamma(Float32, Float32))
changes depending on the value of the shape parameter because shape == 1
samples from Exponential
. (This is why the tests are currently failing.) Should we let this happen? I imagine some people will be super surprised by such behavior.
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.
Hi @devmotion could you comment on this?
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.
That's a good point. I didn't realize that GammaMTSampler
already respects the parameter types (but samples are not necessarily of the parameter type:
Distributions.jl/src/samplers/gamma.jl
Lines 175 to 182 in 13029c0
d = shape(g) - 1//3 | |
c = inv(3 * sqrt(d)) | |
# Pre-compute scaling factor | |
κ = d * scale(g) | |
# We also pre-compute the factor in the squeeze function | |
return GammaMTSampler(promote(d, c, κ, 331//10_000)...) |
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 think that's an argument for using the same approach as for Normal
here, until we move to a better/different API:
rand(rng::AbstractRNG, d::Exponential{T}) where {T} = xval(d, randexp(rng, T)) | |
rand(rng::AbstractRNG, d::Exponential{T}) where {T} = xval(d, randexp(rng, float(T))) |
Co-authored-by: David Widmann <[email protected]>
Co-authored-by: David Widmann <[email protected]>
Co-authored-by: David Widmann <[email protected]>
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.
Can you also add a test for Exponential
?
Co-authored-by: David Widmann <[email protected]>
Co-authored-by: David Widmann <[email protected]>
|
This addresses #1884