Distributions.jl icon indicating copy to clipboard operation
Distributions.jl copied to clipboard

Infinite loop in truncated normal distribution

Open danielmatz opened this issue 2 years ago • 2 comments

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.

danielmatz avatar Apr 28 '23 18:04 danielmatz

It seems there are two cases where 0 standard deviation is problematic:

  1. the truncation points are equal
  2. 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.

ararslan avatar May 11 '23 22:05 ararslan

Duplicate with #1264 in terms of underlying cause.

quildtide avatar Dec 21 '23 07:12 quildtide