Skip to content

Uniform is not type stable + Bounds type should be independent of distribution type #1041

@aminya

Description

@aminya

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)
Float64

The 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)
end

for zeros like constructor (Uniform(T, a, b))

function Uniform(::Type{T}, a, b) where {T <: Real}
    return Uniform{T}(a, b)
end

No type specified constructor:

function Uniform(a, b)
    return Uniform{Float64}(a, b)
 end

API 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions