Distributions.jl
Distributions.jl copied to clipboard
Infinite loop in truncated normal distribution
Using Distributions v0.25.87, the following results in an infinite loop:
julia> d = Normal(204.764679086758, 0.0)
Normal{Float64}(μ=204.764679086758, σ=0.0)
julia> rand(d)
204.764679086758
julia> d2 = truncated(d, 204.764679086758, 204.764679086758)
Truncated(Normal{Float64}(μ=204.764679086758, σ=0.0); lower=204.764679086758, upper=204.764679086758)
julia> rand(d2)
# hangs
Perhaps setting standard deviation to 0.0 is an odd thing to do, but I was expecting it to just return 204.764679086758 on all calls to rand.
It seems there are two cases where 0 standard deviation is problematic:
- the truncation points are equal
- they're unequal but the mean does not lie between them
If the truncation points are unequal and the mean does lie between them, a standard deviation of 0 work just fine and rand always produces the mean. I think in your case, where the mean and both truncation points are all equal, rand should indeed produce the mean. If the mean lies outside of the truncated region and the standard deviation is 0, since no value within the region has nonzero probability, the result should probably be NaN.
Duplicate with #1264 in terms of underlying cause.