Skip to content
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

Computed field len and rand now returns type T in Uniform{T} #1796

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions src/univariate/continuous/uniform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ External links
struct Uniform{T<:Real} <: ContinuousUnivariateDistribution
a::T
b::T
Uniform{T}(a::Real, b::Real) where {T <: Real} = new{T}(a, b)
len::T
Uniform{T}(a::Real, b::Real) where {T <: Real} = new{T}(a, b, b-a)
end

function Uniform(a::T, b::T; check_args::Bool=true) where {T <: Real}
Expand All @@ -47,11 +48,11 @@ Base.convert(::Type{Uniform{T}}, d::Uniform{T}) where {T<:Real} = d

#### Parameters

params(d::Uniform) = (d.a, d.b)
params(d::Uniform) = (d.a, d.b, d.len)
partype(::Uniform{T}) where {T<:Real} = T

location(d::Uniform) = d.a
scale(d::Uniform) = d.b - d.a
scale(d::Uniform) = d.len


#### Statistics
Expand All @@ -61,12 +62,12 @@ median(d::Uniform) = mean(d)
mode(d::Uniform) = mean(d)
modes(d::Uniform) = Float64[]

var(d::Uniform) = (w = d.b - d.a; w^2 / 12)
var(d::Uniform) = (w = d.len; w^2 / 12)

skewness(d::Uniform{T}) where {T<:Real} = zero(T)
kurtosis(d::Uniform{T}) where {T<:Real} = -6/5*one(T)

entropy(d::Uniform) = log(d.b - d.a)
entropy(d::Uniform) = log(d.len)


#### Evaluation
Expand All @@ -86,21 +87,21 @@ end
gradlogpdf(d::Uniform, x::Real) = zero(partype(d)) / oneunit(x)

function cdf(d::Uniform, x::Real)
a, b = params(d)
return clamp((x - a) / (b - a), 0, 1)
a, _, len = params(d)
return clamp((x - a) / len, 0, 1)
end
function ccdf(d::Uniform, x::Real)
a, b = params(d)
return clamp((b - x) / (b - a), 0, 1)
_, b = params(d)
return clamp((b - x) / len, 0, 1)
end

quantile(d::Uniform, p::Real) = d.a + p * (d.b - d.a)
cquantile(d::Uniform, p::Real) = d.b + p * (d.a - d.b)
quantile(d::Uniform, p::Real) = d.a + p * d.len
cquantile(d::Uniform, p::Real) = d.b + p * -d.len


function mgf(d::Uniform, t::Real)
(a, b) = params(d)
u = (b - a) * t / 2
(a, b, len) = params(d)
u = len * t / 2
u == zero(u) && return one(u)
v = (a + b) * t / 2
exp(v) * (sinh(u) / u)
Expand All @@ -117,28 +118,28 @@ end

function cgf(d::Uniform, t)
# log((exp(t*b) - exp(t*a))/ (t*(b-a)))
a,b = params(d)
x = t*(b-a)
_,_,len = params(d)
x = t*len
if abs(x) <= sqrt(eps(float(one(x))))
cgf_around_zero(d, t)
else
cgf_away_from_zero(d, t)
end
end
function cgf_around_zero(d::Uniform, t)
a,b = params(d)
x = t*(b-a)
a,_,len = params(d)
x = t*len
t*a + log1p(cgf_uniform_around_zero_kernel(x))
end
function cgf_away_from_zero(d::Uniform, t)
a,b = params(d)
x = t*(b-a)
a,b,len = params(d)
x = t*len
logsubexp(t*b, t*a) - log(abs(x))
end

function cf(d::Uniform, t::Real)
(a, b) = params(d)
u = (b - a) * t / 2
(a, b, len) = params(d)
u = len * t / 2
u == zero(u) && return complex(one(u))
v = (a + b) * t / 2
cis(v) * (sin(u) / u)
Expand All @@ -151,7 +152,7 @@ Base.:*(c::Real, d::Uniform) = Uniform(minmax(c * d.a, c * d.b)...)

#### Sampling

rand(rng::AbstractRNG, d::Uniform) = d.a + (d.b - d.a) * rand(rng)
rand(rng::AbstractRNG, d::Uniform{T}) where T = d.a + d.len * rand(T, rng)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rand(rng::AbstractRNG, d::Uniform{T}) where T = d.a + d.len * rand(T, rng)
rand(rng::AbstractRNG, d::Uniform{T}) where T = d.a + d.len * rand(float(T), rng)

You need to promote T to a floating-point type.


_rand!(rng::AbstractRNG, d::Uniform, A::AbstractArray{<:Real}) =
A .= quantile.(d, rand!(rng, A))
Expand Down
Loading