-
Notifications
You must be signed in to change notification settings - Fork 421
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
Type of sample of Exponential{T}
is not T
#1902
Comments
This is problematic here
|
Please search the issue tracker for several similar issues. I think this issue might be redundant. |
Unfortunately it is not. It was introduced in 3946acc which was committed last month. We found this in our downstream test pipeline, so unfortunately this is a new issue. |
Which version of Distributions are you using? On master I'm getting julia> typeof(rand(Exponential(θ))) == typeof(θ)
true and that is a consequence of the commit that you link to (which I had forgotten). |
You are right, on master this seems to be fixed. Reading the discussion in #1885 improved my understanding. The issue that is found by our tests is that Edit: in the documentation it says the following:
|
Indeed for Base.eltype(::Type{<:Sampleable{F,Continuous}}) where {F} = Float64 and for Base.eltype(::Type{Normal{T}}) where {T} = T in our code base we basically implicitly tested that julia> eltype(Gamma(1f0, 1f0)) == typeof(rand(Gamma(1f0, 1f0)))
false Also this behaviour leads to this inconsistency, where semantically similar code returns different container types julia> rand(Gamma(1f0, 1f0), 1)
1-element Vector{Float64}:
1.2745195627212524
julia> [ rand(Gamma(1f0, 1f0)) ]
1-element Vector{Float32}:
3.234097 |
I think it's best to avoid julia> eltype(Normal(1//1, 1//1))
Rational{Int64}
julia> rand(Normal(1//1, 1//1))
0.3227598879915683 so |
You cannot expect users to refer to long discussion in the issue tracker. The issue tracker is for developers, not for users. Users are looking at the documentation of the package. And the documentation is misleading. If the documentation is lying it must be changed. |
As a side note, obtaining the exact sample type without having to call julia> distribution = Normal(1//1, 1//1)
Normal{Rational{Int64}}(μ=1//1, σ=1//1)
julia> container = zeros(eltype(distribution), 10);
julia> Distributions.rand!(distribution, container)
ERROR: MethodError: no method matching randn(::Random.TaskLocalRNG, ::Type{Rational{Int64}}) This issue makes it challenging to write generic code that preallocates containers for samples from arbitrary distributions, as |
My point is that this is an open issue and part of the discussion here is just repeating arguments from other issues.
This is the eternal challenge. There is no easy way to know this type. You can
When you try to write generic code then 3 almost always ends up being wrong at some point. Part of the issue here is that Distributions was original written just for Float64 and Int64 parameters and random variates and then later made generic which invalidated some of the original assumptions. But now I'm repeating explanations already available in other issues. |
I understand your point, but the documentation is a primary source of information for most users (and not GitHub issues) and seems to be incorrect in its current form. If the previous original assumptions were invalidated it should be reflected in the documentation too. Now the documentation states that |
MWE:
returns
false
Because Exponential sampling is also used when sampling
Gamma
with shape 1, the problem also occurs there. ForGamma
this introduces a type instability, since the return type for sampling for aGamma
depends on whether or not the alpha parameter is equal to1.0
.The text was updated successfully, but these errors were encountered: