@@ -290,29 +290,29 @@ This implementation is based on the method in J.M. Chambers, C.L. Mallows
290290and B.W. Stuck, "A Method for Simulating Stable Random Variables," JASA 71 (1976): 340-4.
291291McCulloch'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 aϕ = α * ϕ
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 bϕ = π/ 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
316316end
317317
318318Base. eltype (:: Type{<:AlphaStable{T}} ) where {T<: AbstractFloat } = T
0 commit comments