Skip to content

Commit

Permalink
Merge pull request #37 from lrnv/remove_alphastable_dep
Browse files Browse the repository at this point in the history
Remove precompilation and AlphaStableDistributions.jl dependency
  • Loading branch information
lrnv authored Oct 25, 2023
2 parents 363afac + 9288f33 commit 2f86e83
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 26 deletions.
4 changes: 0 additions & 4 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,23 @@ authors = ["Oskar Laverny"]
version = "0.1.11"

[deps]
AlphaStableDistributions = "f20549b4-2d50-407f-863c-cdd202ba59a3"
Cubature = "667455a9-e2ce-5579-9412-b964f529a492"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
GSL = "92c85e6c-cbff-5e0c-80f7-495c94daaecd"
MvNormalCDF = "37188c8d-bc69-4638-b057-733e744175ec"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
TaylorSeries = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"

[compat]
AlphaStableDistributions = "1"
Cubature = "1.5"
Distributions = "0.25"
ForwardDiff = "0.10"
GSL = "1"
MvNormalCDF = "0.2, 0.3"
PrecompileTools = "1"
Roots = "1, 2"
SpecialFunctions = "2"
StatsBase = "0.33, 0.34"
Expand Down
2 changes: 1 addition & 1 deletion src/ArchimedeanCopulas/GumbelCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ GumbelCopula(d,θ) = θ >= 1 ? GumbelCopula{d,typeof(θ)}(θ) : @error "Theta mu
τ(C::GumbelCopula) = ifelse(isfinite(C.θ), (C.θ-1)/C.θ, 1)
τ⁻¹(::Type{GumbelCopula},τ) =ifelse== 1, Inf, 1/(1-τ))

radial_dist(C::GumbelCopula) = AlphaStableDistributions.AlphaStable= 1/C.θ, β = 1,scale = cos/(2C.θ))^C.θ, location = (C.θ == 1 ? 1 : 0))
radial_dist(C::GumbelCopula) = AlphaStable= 1/C.θ, β = 1,scale = cos/(2C.θ))^C.θ, location = (C.θ == 1 ? 1 : 0))


# S(α, β, γ , δ) denotes a stable distribution in
Expand Down
25 changes: 4 additions & 21 deletions src/Copulas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ module Copulas


@doc let
path = joinpath(dirname(@__DIR__), "README.md")
include_dependency(path)
read(path, String)
path = joinpath(dirname(@__DIR__), "README.md")
include_dependency(path)
read(path, String)
end Copulas

import Base
Expand All @@ -18,8 +18,6 @@ end Copulas
import ForwardDiff
import Cubature
import MvNormalCDF
import AlphaStableDistributions
using PrecompileTools

# Standard copulas and stuff.
include("utils.jl")
Expand All @@ -33,6 +31,7 @@ end Copulas
# These three distributions might be merged in Distrbutions.jl one day.
include("univariate_distributions/Sibuya.jl")
include("univariate_distributions/Logarithmic.jl")
include("univariate_distributions/AlphaStable.jl")

# Elliptical copulas
include("EllipticalCopula.jl")
Expand Down Expand Up @@ -64,20 +63,4 @@ end Copulas
WCopula,
SurvivalCopula

# PrecompileTools stuff
@setup_workload begin
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)]
@compile_workload begin
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.5])
cdf(D,[0.5,0.5])
end
end
end

end
60 changes: 60 additions & 0 deletions src/univariate_distributions/AlphaStable.jl
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)
= α * ϕ
a1ϕ = (one(T) - α) * ϕ
return loc + sc * ((((sin(aϕ) + ζ * cos(aϕ))/cosϕ) * ((cos(a1ϕ) + ζ*sin(a1ϕ)) / (w*cosϕ))^((one(T)-α)/α)))
end
= π/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

23 changes: 23 additions & 0 deletions test/old_precompile_workflow.jl
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

0 comments on commit 2f86e83

Please sign in to comment.