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 |
|
|
||
| #### 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.
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.
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.
Hi @devmotion could you comment on this?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 <devmotion@users.noreply.github.com>
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
devmotion
left a comment
There was a problem hiding this comment.
Can you also add a test for Exponential?
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
Co-authored-by: David Widmann <devmotion@users.noreply.github.com>
|
This addresses #1884