Skip to content

Commit

Permalink
Add bindings and tests for WIlliamsonCopulas where needed by the arch…
Browse files Browse the repository at this point in the history
…idedans.
  • Loading branch information
lrnv committed Nov 7, 2023
1 parent 8202804 commit f846751
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Roots = "1, 2"
SpecialFunctions = "2"
StatsBase = "0.33, 0.34"
TaylorSeries = "0.12, 0.13, 0.14, 0.15"
WilliamsonTransforms = "0.0"
WilliamsonTransforms = "0.0.3 - 0"
julia = "1.6"

[extras]
Expand Down
4 changes: 3 additions & 1 deletion src/ArchimedeanCopulas/AMHCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ It has a few special cases:
struct AMHCopula{d,T} <: ArchimedeanCopula{d}
θ::T
function AMHCopula(d,θ)
if< -1) ||> 1)
if< -1) ||>= 1)
throw(ArgumentError("Theta must be in [-1,1)"))
elseif θ == 0
return IndependentCopula(d)
elseif θ < 0
return WilliamsonCopula(t -> (1-θ)/(exp(t)-θ),d)
else
return new{d,typeof(θ)}(θ)
end
Expand Down
7 changes: 5 additions & 2 deletions src/ArchimedeanCopulas/ClaytonCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct ClaytonCopula{d,T} <: ArchimedeanCopula{d}
throw(ArgumentError("Theta must be greater than -1/(d-1)"))
elseif θ == -1/(d-1)
return WCopula(d)
elseif θ < 0
return WilliamsonCopula(
t -> max(1+θ*t,zero(t))^(-1/θ),d)
elseif θ == 0
return IndependentCopula(d)
elseif θ == Inf
Expand All @@ -36,8 +39,8 @@ struct ClaytonCopula{d,T} <: ArchimedeanCopula{d}
end
end
end
ϕ( C::ClaytonCopula, t) = (1+sign(C.θ)*t)^(-1/C.θ)
ϕ⁻¹(C::ClaytonCopula, t) = sign(C.θ)*(t^(-C.θ)-1)
ϕ( C::ClaytonCopula, t) = max(1+C.θ*t,0)^(-1/C.θ)
ϕ⁻¹(C::ClaytonCopula, t) = (t^(-C.θ)-1)/C.θ
radial_dist(C::ClaytonCopula) = Distributions.Gamma(1/C.θ,1) # Currently fails for negative thetas ! thus negtatively correlated clayton copulas cannot be sampled...
τ(C::ClaytonCopula) = ifelse(isfinite(C.θ), C.θ/(C.θ+2), 1)
τ⁻¹(::Type{ClaytonCopula},τ) = ifelse== 1,Inf,2τ/(1-τ))
48 changes: 47 additions & 1 deletion test/archimedean_tests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

@testitem "test bijection" begin
@testitem "Test of τ ∘ τ_inv bijection" begin
using Random
Random.seed!(123)
taus = [0.0, 0.1, 0.5, 0.9, 1.0]
Expand All @@ -17,3 +17,49 @@
end
end
end


@testitem "AMHCopula - Test sampling all cases" begin
for d in 2:10
for θ [-1.0,-rand(),0.0,rand()]
rand(AMHCopula(d,θ),10)
end
end
@test true
end
@testitem "ClaytonCopula - Test sampling all cases" begin
rand(ClaytonCopula(2,-1),10)
for d in 2:10
for θ [-1/(d-1) * rand(),0.0,-log(rand()), Inf]
rand(ClaytonCopula(d,θ),10)
end
end
@test true
end
@testitem "FrankCopula - Test sampling all cases" begin
rand(FrankCopula(2,-Inf),10)
for d in 2:10
for θ [log(rand()),0.0,rand(),1.0,-log(rand()), Inf]
rand(FrankCopula(d,θ),10)
end
end
@test true
end
@testitem "GumbelCopula - Test sampling all cases" begin
for d in 2:10
for θ [1.0,1-log(rand()), Inf]
@show d, θ
rand(GumbelCopula(d,θ),10)
end
end
@test true
end
@testitem "JoeCopula - Test sampling all cases" begin
for d in 2:10
for θ [1.0,1-log(rand()), Inf]
rand(JoeCopula(d,θ),10)
end
end
@test true
end

0 comments on commit f846751

Please sign in to comment.