-
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 Dirichlet
return type by adding Dirichlet(d::Integer, alpha::Integer)
constructor.
#1338
Conversation
Hmm, I agree that the parameters can be kept as integer-valued, but then it seems like there needs to be a way for julia> eltype(Dirichlet(5, 2))
Int64 This doesn't seem to meet the documented behavior for """ eltype(::Type{Sampleable})
The default element type of a sample. This is the type of elements of the samples generated
by the `rand` method. However, one can provide an array of different element types to
store the samples using `rand!`.""" This affects downstream code I have, which determines the expected type of the samples based on Base.eltype(::Type{<:Dirichlet{T}}) where {T} = T but this special case will handle the case when there are Base.eltype(::Type{<:Dirichlet{<:Integer}}) = Float64 |
It also looks like the default implementation of Distributions.jl/src/common.jl Lines 58 to 66 in b35f091
|
In my experience,
Yes, it should be redefined for most distributions since e.g. otherwise usually automatic differentiation is broken. There were (and probably are) multiple bugs caused by missing |
And I am still convinced that one should not convert the parameters only to "fix" a then still broken |
Yup, I'm fine with keeping type parameters as integers, but will the following suggested fix somehow break AD? It seems like Base.eltype(::Type{<:Dirichlet{<:Integer}}) = Float64 I'm happy to create a separate issue / PR about this. This is important for some work I'm doing in https://github.com/probcomp/Genify.jl. |
This definition would be not consistent with the current behaviour but would have to be Base.eltype(::Type{<:Dirichlet{T}}) where {T<:Integer} = float(T) This was actually (for general
|
The same argument has been brought forward multiple times, e.g., in #1071 (comment):
So I think the documentation should be fixed. |
In my experience, the only reliable way to determine the type of the samples is to draw a sample with |
Thanks for sharing this context! It's bizarre to me that |
After #1243 was merged, it looks like the old constructor for
Dirichlet(d::Integer, alpha::Integer)
was removed, and the new constructor introduced creates a situation where theeltype
ofDirichlet
ends up beingInt64
ifalpha
is given as anInt64
(see ccebbd7#diff-65f8aff731d04673339bb6e204537e9f99b6ac88797bd476a816ceaab046077aL52-R41 ).This was causing issues in some downstream code I've been using, which converts the return value of
rand(dist::Dirichlet{T}, ...)
to typeT
. WhenT
wasInt64
, this created errors, because the conversion from a float wasn't possible.This PR should address the issue by re-introducing a constructor that handles the case where
alpha
is anInteger
, and convertsalpha
toFloat64
.