Skip to content

Commit

Permalink
Prevent rejection sampling if normal variance is 0
Browse files Browse the repository at this point in the history
  • Loading branch information
quildtide committed Feb 4, 2021
1 parent 863844c commit d88926c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/truncated/normal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,14 @@ function rand(rng::AbstractRNG, d::Truncated{Normal{T},Continuous}) where T <: R
μ = mean(d0)
σ = std(d0)
if isfinite(μ)
a = (d.lower - μ) / σ
b = (d.upper - μ) / σ
z = randnt(rng, a, b, d.tp)
return μ + σ * z
if σ > 0
a = (d.lower - μ) / σ
b = (d.upper - μ) / σ
z = randnt(rng, a, b, d.tp)
return μ + σ * z
else
return μ
end
else
return clamp(μ, d.lower, d.upper)
end
Expand Down

0 comments on commit d88926c

Please sign in to comment.