Skip to content

Commit

Permalink
[Tests] Large refactor and upgrade of the testing framework. (#232)
Browse files Browse the repository at this point in the history
* Commit every changes availiables...

* let's comit everything before having issuesw with it

* Restore specific tests

* restore tests

* bugfix
  • Loading branch information
lrnv authored Sep 22, 2024
1 parent cc56fb9 commit b803ddf
Show file tree
Hide file tree
Showing 29 changed files with 778 additions and 1,254 deletions.
2 changes: 1 addition & 1 deletion src/SubsetCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ end
function SubsetCopula(C::FGMCopula{d,Tθ}, dims::Tuple{Int64, Int64}) where {d,Tθ}
i = 1
for indices in Combinatorics.combinations(1:d, 2)
all(indices .∈ dims) && return FGMCopula(2,C.θ[i])
all(indices .∈ Ref(dims)) && return FGMCopula(2,C.θ[i])
i = i+1
end
@error("Somethings wrong...")
Expand Down
115 changes: 115 additions & 0 deletions test/ArchimedeanCopulas.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

@testitem "Archimedeans - Fix sampled values" begin
using Random, Distributions
using StableRNGs
rng = StableRNG(123)
@test rand(rng, AMHCopula(3,0.6) ) [0.6100194232653313, 0.2812460189425596, 0.10333020320732342]
@test rand(rng, AMHCopula(4,-0.3) ) [0.6794797200689908, 0.2392452398851621, 0.6025427563179961, 0.2844305227337651]
@test rand(rng, ClaytonCopula(2,-0.7) ) [0.6275520653528824, 0.163616179546588]
@test rand(rng, ClaytonCopula(3,-0.1) ) [0.3881011762518264, 0.5656986324924199, 0.2940237224994986]
@test rand(rng, ClaytonCopula(4,7) ) [0.4939026426425556, 0.5327344049793008, 0.508779929573844, 0.458289642358837]
@test rand(rng, FrankCopula(2,-5) ) [0.7336303867890769, 0.19556711408763564]
@test rand(rng, FrankCopula(3,12) ) [0.705387567921179, 0.8641668940713177, 0.815244058863104]
@test rand(rng, FrankCopula(4,6) ) [0.9003228944757687, 0.8260436103707566, 0.9133247903512886, 0.7994891654043613]
@test rand(rng, FrankCopula(4,30) ) [0.3923476356094088, 0.4382518570496335, 0.39700820876188975, 0.3908555957587312]
@test rand(rng, FrankCopula(4,37) ) [0.7303681473348445, 0.6898271742903759, 0.7264896311455771, 0.7294073988977854]
@test rand(rng, FrankCopula(4,150) ) [0.43642655973527883, 0.42188653827683725, 0.41316130988739547, 0.42762193255859465]
@test rand(rng, JoeCopula(3,7) ) [0.5527859698950577, 0.5839844253671289, 0.6067932545327556]
@test rand(rng, GumbelCopula(4,7) ) [0.781761681793509, 0.7444203095073173, 0.8307664734410207, 0.7495821288234644]
@test rand(rng, GumbelCopula(4,20) ) [0.5482947180691102, 0.5460490383319109, 0.5159827606532166, 0.6002166337095293]
@test rand(rng, GumbelCopula(4,100) ) [0.8199684741926241, 0.81488028576667, 0.8161867411368382, 0.8187239187339411]
@test rand(rng, GumbelBarnettCopula(3,0.7)) [0.33125926946816414, 0.31478129636784674, 0.48594013921379653]
@test rand(rng, InvGaussianCopula(4,0.05) ) [0.4777472572394352, 0.5714072783562308, 0.8312697205666382, 0.5291602862226205]
@test rand(rng, InvGaussianCopula(3,8) ) [0.5325775326824974, 0.4876131855666078, 0.5207906451760111]
end

@testitem "Archimedean - Fix Kendall correlation" begin
using Random
using StableRNGs
rng = StableRNG(123)

@test Copulas.Debye(0.5,1) 0.8819271567906056
@test Copulas.τ⁻¹(FrankCopula, 0.6) 7.929642284264058
@test Copulas.τ⁻¹(GumbelCopula, 0.5) 2.
@test Copulas.τ⁻¹(ClaytonCopula, 1/3) 1.
@test Copulas.τ⁻¹(AMHCopula, 1/4) 0.8384520912688538
@test Copulas.τ⁻¹(AMHCopula, 0.) 0.
@test Copulas.τ⁻¹(AMHCopula, 1/3+0.0001) 1.
@test Copulas.τ⁻¹(AMHCopula, -2/11) -1.
@test Copulas.τ⁻¹(AMHCopula, -0.1505) -0.8 atol=1.0e-3
@test Copulas.τ⁻¹(FrankCopula, -0.3881) -4. atol=1.0e-3
@test Copulas.τ⁻¹(ClaytonCopula, -1/3) -.5 atol=1.0e-5
end

@testitem "Archimedeans - Fix Spearman correlation" begin

@test Copulas.ρ(ClaytonCopula(2,3.)) 0.78645 atol=1.0e-4
@test Copulas.ρ(ClaytonCopula(2,0.001)) 0. atol=1.0e-2
@test Copulas.ρ(GumbelCopula(2,3.)) 0.8489 atol=1.0e-4

@test_broken Copulas.ρ⁻¹(ClaytonCopula, 1/3) 0.58754 atol=1.0e-5
@test_broken Copulas.ρ⁻¹(ClaytonCopula, 0.01) 0. atol=1.0e-1
@test_broken Copulas.ρ⁻¹(ClaytonCopula, -0.4668) -.5 atol=1.0e-3

@test_broken Copulas.ρ⁻¹(GumbelCopula, 0.5) 1.5410704204332681
@test_broken Copulas.ρ⁻¹(GumbelCopula, 0.0001) == 1.

@test_broken Copulas.ρ⁻¹(FrankCopula, 1/3) 2.116497 atol=1.0e-5
@test_broken Copulas.ρ⁻¹(FrankCopula, -0.5572) -4. atol=1.0e-3

@test_broken Copulas.ρ⁻¹(AMHCopula, 0.2) 0.5168580913147318
@test_broken Copulas.ρ⁻¹(AMHCopula, 0.) 0. atol=1.0e-4
@test_broken Copulas.ρ⁻¹(AMHCopula, 0.49) 1 atol=1.0e-4
@test_broken Copulas.ρ⁻¹(AMHCopula, -0.273) -1 atol=1.0e-4
@test_broken Copulas.ρ⁻¹(AMHCopula, -0.2246) -0.8 atol=1.0e-3
end

@testitem "Testing empirical tail values of certain copula samples" begin
using StableRNGs
using Distributions
rng = StableRNG(123)

function tail(v1::Vector{T}, v2::Vector{T}, tail::String, α::T = 0.002) where T <: Real
if tail == "l"
return sum((v1 .< α) .* (v2 .< α))./(length(v1)*α)
elseif tail == "r"
return sum((v1 .> (1-α)) .* (v2 .> (1-α)))./(length(v1)*α)
end
0.
end

@testset "tail dependencies test" begin
v1 = vcat(zeros(5), 0.5*ones(5), zeros(5), 0.5*ones(70), ones(5), 0.5*ones(5), ones(5));
v2 = vcat(zeros(10), 0.5*ones(80), ones(10))
@test tail(v1, v2, "l", 0.1) 0.5
@test tail(v1, v2, "r", 0.1) 0.5
end

@testset "Gumbel" begin
rng = StableRNG(123)
x = rand(rng,GumbelCopula(3,2.), 100_000)
@test_broken tail(x[:,1], x[:,2], "r") 2-2^(1/2) atol=1.0e-1
@test_broken tail(x[:,1], x[:,2], "r") 2-2^(1/2) atol=1.0e-1
@test tail(x[:,1], x[:,2], "l", 0.00001) 0.
@test tail(x[:,1], x[:,3], "l", 0.00001) 0.
end
@testset "Clayton" begin
rng = StableRNG(123)
x = rand(rng,ClaytonCopula(3,1.), 100_000)
@test_broken tail(x[:,1], x[:,2], "l") 2.0^(-1) atol=1.0e-1
@test_broken tail(x[:,1], x[:,3], "l") 2.0^(-1) atol=1.0e-1
@test tail(x[:,1], x[:,2], "r", 0.0001) 0
end
@testset "AMH" begin
rng = StableRNG(123)
x = rand(rng,AMHCopula(3,0.8), 100_000)
@test tail(x[:,1], x[:,2], "l", 0.0001) 0
@test tail(x[:,1], x[:,2], "r", 0.0001) 0
end
@testset "Frank" begin
rng = StableRNG(123)
x = rand(rng,FrankCopula(3,0.8), 100_000)
@test tail(x[:,1], x[:,2], "l", 0.0001) 0
@test tail(x[:,1], x[:,2], "r", 0.0001) 0
end
end
28 changes: 28 additions & 0 deletions test/ClaytonCopula.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@testitem "Fix values of bivariate ClaytonCopula: τ, cdf, pdf and contructor" begin
using Distributions

# Fix a few cdf and pdf values:
x = [0:0.25:1;]
y = x
cdf1 = [0.0, 0.1796053020267749, 0.37796447300922725, 0.6255432421712244, 1.0]
cdf2 = [0.0, 0.0, 0.17157287525381, 0.5358983848622453, 1.0]
pdf1 = [Inf, 2.2965556205046926, 1.481003649342278, 1.614508582188617, 3.0]
pdf2 = [Inf, 0.0, 1.0, 2 / 3, 0.5]
for i in 1:5
@test cdf(ClaytonCopula(2,2),[x[i],y[i]]) cdf1[i]
@test cdf(ClaytonCopula(2,-0.5),[x[i],y[i]]) cdf2[i]
@test pdf(ClaytonCopula(2,2),[x[i],y[i]]) pdf1[i]
@test pdf(ClaytonCopula(2,-0.5),[x[i],y[i]]) pdf2[i]
end

# Fix a few tau values:
@test Copulas.τ(ClaytonCopula(2,-0.5)) == -1 / 3
@test Copulas.τ(ClaytonCopula(2,2)) == 0.5
@test Copulas.τ(ClaytonCopula(2,10)) == 10 / 12

# Fix constructor behavior:
@test isa(ClaytonCopula(2,0), IndependentCopula)
@test isa(ClaytonCopula(2,-0.7), ClaytonCopula)
@test isa(ClaytonCopula(2,-1), WCopula)
@test isa(ClaytonCopula(2,Inf), MCopula)
end
10 changes: 10 additions & 0 deletions test/Constructor_Errors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@testitem "Constructors errors on wrong inputs" begin
using LinearAlgebra
@test_throws LinearAlgebra.PosDefException GaussianCopula([1 2.0; 2 1])
@test_throws LinearAlgebra.PosDefException TCopula(10,[1 2.0; 2 1])
@test_throws ArgumentError PlackettCopula(-0.5)
@test_throws ArgumentError FGMCopula(1,0.5)
@test_throws ArgumentError FGMCopula(3,[-1.5,2.0,3.1,1.2])
@test_throws ArgumentError FGMCopula(1,[0.8,0.2,0.5,0.4])
end

182 changes: 0 additions & 182 deletions test/Extreme_value_test.jl

This file was deleted.

Loading

0 comments on commit b803ddf

Please sign in to comment.