-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from lrnv/remove_alphastable_dep
Remove precompilation and AlphaStableDistributions.jl dependency
- Loading branch information
Showing
5 changed files
with
88 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
########## | ||
########## Copied from https://github.com/org-arl/AlphaStableDistributions.jl | ||
########## To avoid too many dependencies and fasten the package. | ||
########## Probably we can do better by integrating with them... but they use too much. | ||
########## | ||
|
||
Base.@kwdef struct AlphaStable{T} <: Distributions.ContinuousUnivariateDistribution | ||
α::T = 1.5 | ||
β::T = zero(α) | ||
scale::T = one(α) | ||
location::T = zero(α) | ||
end | ||
|
||
AlphaStable(α::Integer, β::Integer, scale::Integer, location::Integer) = AlphaStable(float(α), float(β), float(scale), float(location)) | ||
function AlphaStable(α,β,scale,location) | ||
αT,βT,scaleT,locationT = promote(α,β,scale,location) | ||
AlphaStable(αT,βT,scaleT,locationT) | ||
end | ||
Distributions.params(d::AlphaStable) = (d.α, d.β, d.scale, d.location) | ||
|
||
""" | ||
Generate independent stable random numbers. | ||
:param α: characteristic exponent (0.1 to 2.0) | ||
:param β: skew (-1 to +1) | ||
:param scale: scale parameter | ||
:param loc: location parameter (mean for α > 1, median/mode when β=0) | ||
This implementation is based on the method in J.M. Chambers, C.L. Mallows | ||
and B.W. Stuck, "A Method for Simulating Stable Random Variables," JASA 71 (1976): 340-4. | ||
McCulloch's MATLAB implementation (1996) served as a reference in developing this code. | ||
""" | ||
function Base.rand(rng::Distributions.AbstractRNG, d::AlphaStable{T}) where {T<:AbstractFloat} | ||
α=d.α; β=d.β; sc=d.scale; loc=d.location | ||
(α < 0.1 || α > 2) && throw(DomainError(α, "α must be in the range 0.1 to 2")) | ||
abs(β) > 1 && throw(DomainError(β, "β must be in the range -1 to 1")) | ||
# added eps(T) to prevent DomainError: x ^ y where x < 0 | ||
ϕ = (rand(rng, T) - T(0.5)) * π * (one(T) - eps(T)) | ||
if α == one(T) && β == zero(T) | ||
return loc + sc * tan(ϕ) | ||
end | ||
w = -log(rand(rng, T)) | ||
α == 2 && (return loc + 2*sc*sqrt(w)*sin(ϕ)) | ||
β == zero(T) && (return loc + sc * ((cos((one(T)-α)*ϕ) / w)^(one(T)/α - one(T)) * sin(α * ϕ) / cos(ϕ)^(one(T)/α))) | ||
cosϕ = cos(ϕ) | ||
if abs(α - one(T)) > 1e-8 | ||
ζ = β * tan(π * α / 2) | ||
aϕ = α * ϕ | ||
a1ϕ = (one(T) - α) * ϕ | ||
return loc + sc * ((((sin(aϕ) + ζ * cos(aϕ))/cosϕ) * ((cos(a1ϕ) + ζ*sin(a1ϕ)) / (w*cosϕ))^((one(T)-α)/α))) | ||
end | ||
bϕ = π/2 + β*ϕ | ||
x = 2/π * (bϕ * tan(ϕ) - β * log(π/2*w*cosϕ/bϕ)) | ||
α == one(T) || (x += β * tan(π*α/2)) | ||
return loc + sc * x | ||
end | ||
|
||
Base.eltype(::Type{<:AlphaStable{T}}) where {T<:AbstractFloat} = T | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
@testitem "standard functionality test" begin | ||
using Random, Distributions | ||
biv_cops = [ | ||
GaussianCopula([1 0.7; 0.7 1]), | ||
TCopula(2,[1 0.7; 0.7 1]), | ||
ClaytonCopula(2,7), | ||
JoeCopula(2,3), | ||
GumbelCopula(2,8), | ||
FrankCopula(2,0.5), | ||
AMHCopula(2,0.7) | ||
] | ||
for C in biv_cops | ||
u = Random.rand(C,10) | ||
pdf(C,[0.5,0.5]) | ||
cdf(C,[0.5,0.5]) | ||
D = SklarDist(C,[Gamma(1,1),Normal(1,1)]) | ||
u = Random.rand(D,10) | ||
pdf(D,[0.5,0]) | ||
cdf(D,[0.5,0]) | ||
end | ||
@test true | ||
end |