Skip to content

Commit

Permalink
Special case Geometric(OneHalf()) (#1934)
Browse files Browse the repository at this point in the history
* Special case Geometric(OneHalf())

* Remove OneHalf and branch at sample time

* Use 1//2 instead of 0.5
  • Loading branch information
LilithHafner authored Jan 8, 2025
1 parent ceb6343 commit e767d3e
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/univariate/discrete/geometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,14 @@ cf(d::Geometric, t::Real) = laplace_transform(d, -t*im)

### Sampling

rand(rng::AbstractRNG, d::Geometric) = floor(Int,-randexp(rng) / log1p(-d.p))
# Inlining is required to hoist the d.p == 1//2 check when generating in bulk
@inline function rand(rng::AbstractRNG, d::Geometric)
if d.p == 1//2
leading_zeros(rand(rng, UInt)) # This branch is a performance optimization
else
floor(Int,-randexp(rng) / log1p(-d.p))
end
end

### Model Fitting

Expand Down

0 comments on commit e767d3e

Please sign in to comment.