Skip to content

Commit 227fef9

Browse files
committed
fix: rand for AlphaStable
1 parent f5b082c commit 227fef9

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/AlphaStableDistributions.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,29 +290,29 @@ This implementation is based on the method in J.M. Chambers, C.L. Mallows
290290
and B.W. Stuck, "A Method for Simulating Stable Random Variables," JASA 71 (1976): 340-4.
291291
McCulloch's MATLAB implementation (1996) served as a reference in developing this code.
292292
"""
293-
function Base.rand(rng::AbstractRNG, d::AlphaStable{T}) where {T<:AbstractFloat}
294-
α=d.α; β=d.β; scale=d.scale; loc=d.location
293+
function Base.rand(rng::AbstractRNG, d::AlphaStable{T}) where {T<:AbstractFloat}
294+
α=d.α; β=d.β; sc=d.scale; loc=d.location
295295
< 0.1 || α > 2) && throw(DomainError(α, "α must be in the range 0.1 to 2"))
296296
abs(β) > 1 && throw(DomainError(β, "β must be in the range -1 to 1"))
297-
ϕ = (rand(rng, T) - 0.5) * π
297+
# added eps(T) to prevent DomainError: x ^ y where x < 0
298+
ϕ = (rand(rng, T) - T(0.5)) * π * (one(T) - eps(T))
298299
if α == one(T) && β == zero(T)
299-
return loc + scale * tan(ϕ)
300+
return loc + sc * tan(ϕ)
300301
end
301302
w = -log(rand(rng, T))
302-
α == 2 && (return loc + 2*scale*sqrt(w)*sin(ϕ))
303-
β == zero(T) && (return loc + scale * ((cos((1-α)*ϕ) / w)^(one(T)/α - one(T)) * sin* ϕ) / cos(ϕ)^(one(T)/α)))
303+
α == 2 && (return loc + 2*sc*sqrt(w)*sin(ϕ))
304+
β == zero(T) && (return loc + sc * ((cos((one(T)-α)*ϕ) / w)^(one(T)/α - one(T)) * sin* ϕ) / cos(ϕ)^(one(T)/α)))
304305
cosϕ = cos(ϕ)
305306
if abs- one(T)) > 1e-8
306307
ζ = β * tan* α / 2)
307308
= α * ϕ
308309
a1ϕ = (one(T) - α) * ϕ
309-
return loc + scale * (( (sin(aϕ) + ζ * cos(aϕ))/cosϕ * ((cos(a1ϕ) + ζ*sin(a1ϕ))) / ((w*cosϕ)^((1-α)/α)) ))
310+
return loc + sc * ((((sin(aϕ) + ζ * cos(aϕ))/cosϕ) * ((cos(a1ϕ) + ζ*sin(a1ϕ)) / (w*cosϕ))^((one(T)-α)/α)))
310311
end
311312
= π/2 + β*ϕ
312313
x = 2/π * (bϕ * tan(ϕ) - β * log/2*w*cosϕ/bϕ))
313314
α == one(T) || (x += β * tan*α/2))
314-
315-
return loc + scale * x
315+
return loc + sc * x
316316
end
317317

318318
Base.eltype(::Type{<:AlphaStable{T}}) where {T<:AbstractFloat} = T

0 commit comments

Comments
 (0)