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

Fix rand for truncated normal with 0 variance #1721

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Distributions"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
authors = ["JuliaStats"]
version = "0.25.110"
version = "0.25.111"

[deps]
AliasTables = "66dad0bd-aa9a-41b7-9441-69ab47430ed8"
Expand Down
18 changes: 13 additions & 5 deletions src/truncated/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,22 @@ function rand(rng::AbstractRNG, d::Truncated{<:Normal{<:Real},Continuous})
d0 = d.untruncated
μ = mean(d0)
σ = std(d0)
a, b = extrema(d)
if isfinite(μ)
lower, upper = extrema(d)
a = (lower - μ) / σ
b = (upper - μ) / σ
z = randnt(rng, a, b, d.tp)
if iszero(σ)
if a <= μ <= b
z = 0.0
Copy link
Member

Choose a reason for hiding this comment

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

I assume this will cause type instabilities, e.g., if parameters of Normal and Truncated are Float32?

Copy link
Member Author

@ararslan ararslan Aug 16, 2024

Choose a reason for hiding this comment

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

z is always Float64, both here and the branch that calls randnt (that function requires all inputs be Float64 and it returns a Float64). But if µ and the truncation points are wider, say BigFloat, then you'll get a MethodError from randnt when isfinite(µ) && σ > 0 and a BigFloat result otherwise. (That behavior predates this PR.) Float32 works because extrema promotes the truncation points to Float64, so every branch will return Float64.

else
throw(ArgumentError("cannot sample from distribution with 0 mass"))
end
else
a′ = (a - μ) / σ
b′ = (b - μ) / σ
z = randnt(rng, a′, b′, d.tp)
end
return μ + σ * z
else
return clamp(μ, extrema(d)...)
return clamp(μ, a, b)
end
end

Expand Down
14 changes: 13 additions & 1 deletion test/truncated/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ end
[(r = rand, r! = rand!),
(r = ((d, n) -> rand(rng, d, n)), r! = ((d, X) -> rand!(rng, d, X)))]
repeats = 1000000

@test abs(mean(func.r(trunc, repeats))) < 0.01
@test abs(median(func.r(trunc, repeats))) < 0.01
@test abs(var(func.r(trunc, repeats)) - var(trunc)) < 0.01
Expand All @@ -69,3 +69,15 @@ end
@test isfinite(pdf(trunc, x))
end
end

@testset "Degenerate truncated normal" begin
# https://github.com/JuliaStats/Distributions.jl/issues/1712
d = Normal(2, 0)
@test rand(truncated(d, 2, 2)) == 2 # a == μ == b
@test rand(truncated(d, 1, 3)) == 2 # a <= μ <= b
@test_throws ArgumentError rand(truncated(d, 6, 9)) # μ ∉ [a, b]
# https://github.com/JuliaStats/Distributions.jl/issues/1867
d = Normal(1, 0)
@test rand(truncated(Normal(1, 0), 0, 1)) == 1
@test rand(truncated(Normal(0, 0), 0, 1)) == 0
end
Loading