-
Notifications
You must be signed in to change notification settings - Fork 431
Open
Description
The current definition of Uniform is not type stable. I tried to make a method in #1035 to make it usable, but it doesn't help.
using Distributions
set = Uniform(Float32(0.0), Float32(2.0))
v = rand(set, 3)julia> eltype(v)
Float64The definition should not be dependent on the type of a and b. From the mathematical point of view, a and b bounds may not be part of the distribution and so their type shouldn't matter. Actually their probability is equal to 0!
https://github.com/JuliaStats/Distributions.jl/blob/master/src/univariate/continuous/uniform.jl#L26
Instead, the user should provide the type explicitly. It should use Float64 if no type is provided.
The following definition is my proposed definition:
for array like constructor (Uniform{T}(a, b))
struct Uniform{T<:Real} <: ContinuousUnivariateDistribution
a::T1 where {T1 <: Real}
b::T2 where {T2 <: Real}
Uniform{T}(a, b) where {T <: Real} = new{T}(a, b)
endfor zeros like constructor (Uniform(T, a, b))
function Uniform(::Type{T}, a, b) where {T <: Real}
return Uniform{T}(a, b)
endNo type specified constructor:
function Uniform(a, b)
return Uniform{Float64}(a, b)
endAPI examples:
julia> Uniform(1,2)
Uniform{Float64}(a=1, b=2)
julia> Uniform(Float32, 1,2)
Uniform{Float32}(a=1, b=2)
julia> Uniform{Int64}(1,2)
Uniform{Int64}(a=1, b=2)Metadata
Metadata
Assignees
Labels
No labels