Skip to content

Commit

Permalink
Move to StableRNGs for testing (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
lrnv authored Nov 16, 2023
1 parent d930e22 commit 0d739de
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 46 deletions.
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ MvNormalCDF = "0.2, 0.3"
Random = "1.6"
Roots = "1, 2"
SpecialFunctions = "2"
StableRNGs = "1"
StatsBase = "0.33, 0.34"
TaylorSeries = "0.12, 0.13, 0.14, 0.15"
Test = "1.6"
Expand All @@ -45,8 +46,9 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
HypothesisTests = "09f84164-cd44-5f33-b23f-e6b0d136a0d5"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestItemRunner = "f8b46487-2199-4994-9208-9a1283c18c0a"

[targets]
test = ["Test", "TestItemRunner", "InteractiveUtils", "LinearAlgebra", "HypothesisTests", "Aqua"]
test = ["Test", "TestItemRunner", "InteractiveUtils", "LinearAlgebra", "HypothesisTests", "Aqua", "StableRNGs"]
55 changes: 34 additions & 21 deletions test/archimedean_tests.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

@testitem "Test of τ ∘ τ_inv bijection" begin
using Random
Random.seed!(123)
using Random
using StableRNGs
rng = StableRNG(123)
taus = [0.0, 0.1, 0.5, 0.9, 1.0]

for T in (
Expand All @@ -22,64 +23,76 @@ end


@testitem "AMHCopula - Test sampling all cases" begin
using StableRNGs
rng = StableRNG(123)
for d in 2:10
for θ [-1.0,-rand(),0.0,rand()]
rand(AMHCopula(d,θ),10)
for θ [-1.0,-rand(rng),0.0,rand(rng)]
rand(rng,AMHCopula(d,θ),10)
end
end
@test true
end
@testitem "ClaytonCopula - Test sampling all cases" begin
using Random
Random.seed!(123) # to avoid weird bug on 1.6
rand(ClaytonCopula(2,-1),10)
using StableRNGs
rng = StableRNG(123)
rand(rng,ClaytonCopula(2,-1),10)
for d in 2:10
for θ [-1/(d-1) * rand(),0.0,-log(rand()), Inf]
rand(ClaytonCopula(d,θ),10)
for θ [-1/(d-1) * rand(rng),0.0,-log(rand(rng)), Inf]
rand(rng,ClaytonCopula(d,θ),10)
end
end
@test true
end
@testitem "FrankCopula - Test sampling all cases" begin
rand(FrankCopula(2,-Inf),10)
rand(FrankCopula(2,log(rand())),10)
using StableRNGs
rng = StableRNG(123)
rand(rng,FrankCopula(2,-Inf),10)
rand(rng,FrankCopula(2,log(rand(rng))),10)
for d in 2:10
for θ [0.0,rand(),1.0,-log(rand()), Inf]
rand(FrankCopula(d,θ),10)
for θ [0.0,rand(rng),1.0,-log(rand(rng)), Inf]
rand(rng,FrankCopula(d,θ),10)
end
end
@test true
end
@testitem "GumbelCopula - Test sampling all cases" begin
using StableRNGs
rng = StableRNG(123)
for d in 2:10
for θ [1.0,1-log(rand()), Inf]
rand(GumbelCopula(d,θ),10)
for θ [1.0,1-log(rand(rng)), Inf]
rand(rng,GumbelCopula(d,θ),10)
end
end
@test true
end
@testitem "JoeCopula - Test sampling all cases" begin
using StableRNGs
rng = StableRNG(123)
for d in 2:10
for θ [1.0,1-log(rand()), Inf]
rand(JoeCopula(d,θ),10)
for θ [1.0,1-log(rand(rng)), Inf]
rand(rng,JoeCopula(d,θ),10)
end
end
@test true
end

@testitem "GumbelBarnettCopula - Test sampling all cases" begin
using StableRNGs
rng = StableRNG(123)
for d in 2:10
for θ [0.0,rand(),1.0]
rand(GumbelBarnettCopula(d,θ),10)
for θ [0.0,rand(rng),1.0]
rand(rng,GumbelBarnettCopula(d,θ),10)
end
end
@test true
end

@testitem "InvGaussianCopula - Test sampling all cases" begin
using StableRNGs
rng = StableRNG(123)
for d in 2:10
for θ [rand(),1.0, Inf]
rand(InvGaussianCopula(d,θ),10)
for θ [rand(rng),1.0, Inf]
rand(rng,InvGaussianCopula(d,θ),10)
end
end
@test true
Expand Down
5 changes: 3 additions & 2 deletions test/biv_Plackett.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ end
end

@testitem "PlackettCopula Sampling" begin
using Random
using StableRNGs
rng = StableRNG(123)
n_samples = 100
C = PlackettCopula(0.8)
samples = rand(C, n_samples)
samples = rand(rng,C, n_samples)
@test size(samples) == (2, n_samples)
end
6 changes: 4 additions & 2 deletions test/biv_clayton.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ end

@testitem "sample" begin
using StatsBase, Random
using StableRNGs
rng = StableRNG(123)
n = 10^5
θ = [-0.5, 2, 10]

for ϑ in θ
c = ClaytonCopula(2,ϑ)
# if ϑ < 0
# @test_broken rand(c,n)
# @test_broken rand(rng,c,n)
# else
u = rand(c, n)
u = rand(rng,c, n)
@test corkendall(u') [1.0 Copulas.τ(c); Copulas.τ(c) 1.0] atol = 0.01
# end
end
Expand Down
11 changes: 6 additions & 5 deletions test/coherence_archimedeans.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

@testitem "Test of coherence for archimedeans" begin
using HypothesisTests, Distributions, Random
Random.seed!(123)
using StableRNGs
rng = StableRNG(123)
cops = (
# true represent the fact that cdf(williamson_dist(C),x) is defined or not.
(AMHCopula(3,0.6), true),
Expand All @@ -24,17 +25,17 @@
(InvGaussianCopula(3,8),true)
)
n = 1000
spl = rand(n)
spl2 = rand(n)
spl = rand(rng,n)
spl2 = rand(rng,n)
for (C,will_dist_has_a_cdf_implemented) in cops
spl .= dropdims(sum(Copulas.ϕ⁻¹.(Ref(C),rand(C,n)),dims=1),dims=1)
spl .= dropdims(sum(Copulas.ϕ⁻¹.(Ref(C),rand(rng,C,n)),dims=1),dims=1)
will_dist = Copulas.williamson_dist(C)
if will_dist_has_a_cdf_implemented
pval = pvalue(ExactOneSampleKSTest(spl, will_dist),tail=:right)
@test pval > 0.01
end
# even without a cdf we can still test approximately:
spl2 .= rand(will_dist,n)
spl2 .= rand(rng,will_dist,n)
pval2 = pvalue(ApproximateTwoSampleKSTest(spl,spl2),tail=:right)
@test pval2 > 0.01
end
Expand Down
8 changes: 6 additions & 2 deletions test/fgmCopula_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ end
end

@testset "FGMCopula PDF" begin
using StableRNGs
rng = StableRNG(123)
examples = [
([0.1, 0.2, 0.3], [1.308876232, 1e-4], [0.1,0.2,0.5,0.4]),
([0.5, 0.4, 0.3], [1.024123232, 1e-4], [0.3,0.3,0.3,0.3]),
([0.1, 0.1], [0.01, 1e-4], 0.0),
([0.5, 0.4], [1, 1e-4], rand()),
([0.5, 0.4], [1, 1e-4], rand(rng)),
]

for (u, expected) in examples
Expand All @@ -34,8 +36,10 @@ end
end

@testitem "FGMCopula Sampling" begin
using StableRNGs
rng = StableRNG(123)
n_samples = 100
F = FGMCopula(3,[0.1,0.2,0.3,0.4])
samples = rand(F, n_samples)
samples = rand(rng,F, n_samples)
@test size(samples) == (3, n_samples)
end
5 changes: 3 additions & 2 deletions test/margins_uniformity.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@testitem "Test samples have uniform maginals in [0,1]" begin
using HypothesisTests, Distributions, Random
Random.seed!(1234)
using StableRNGs
rng = StableRNG(123)
cops = (
# true represent the fact that cdf(williamson_dist(C),x) is defined or not.
AMHCopula(3,0.6),
Expand Down Expand Up @@ -32,7 +33,7 @@
nfail = 0
d = length(C)
@show C
spl = rand(C,n)
spl = rand(rng,C,n)
@assert all(0 <= x <= 1 for x in spl)
for i in 1:d
@test pvalue(ApproximateOneSampleKSTest(spl[i,:], U),tail=:right) > 0.01 # quite weak but enough at these samples sizes to detect really bad behaviors.
Expand Down
6 changes: 4 additions & 2 deletions test/old_precompile_workflow.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

@testitem "standard functionality test" begin
using Random, Distributions
using StableRNGs
rng = StableRNG(123)
biv_cops = [
GaussianCopula([1 0.7; 0.7 1]),
TCopula(2,[1 0.7; 0.7 1]),
Expand All @@ -12,11 +14,11 @@
AMHCopula(2,0.7)
]
for C in biv_cops
u = Random.rand(C,10)
u = Random.rand(rng,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)
u = Random.rand(rng,D,10)
pdf(D,[0.5,0])
cdf(D,[0.5,0])
end
Expand Down
16 changes: 10 additions & 6 deletions test/some_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using Distributions
using Random
using StableRNGs
rng = StableRNG(123)
MyD = SklarDist(ClaytonCopula(3,7),(LogNormal(),Pareto(),Beta()))
u = rand(MyD,10000)
u = rand(rng,MyD,10000)
rand!(MyD,u)
fit(SklarDist{ClaytonCopula,Tuple{LogNormal,Pareto,Beta}},u)
fit(SklarDist{GaussianCopula,Tuple{LogNormal,Pareto,Beta}},u)
Expand All @@ -24,7 +26,7 @@ end
# M1 = Beta(2,3)
# M2 = LogNormal(2,3)
# D = SklarDist(C,(M1,M2))
# X = rand(D,1000)
# X = rand(rng,D,1000)
# loglikelihood(D,X)
# fit(SklarDist{TCopula,Tuple{Beta,LogNormal}},X) # should give a very high \nu for the student copula.
# end
Expand Down Expand Up @@ -117,6 +119,8 @@ end
@testitem "working measure" begin
using Distributions
using Random
using StableRNGs
rng = StableRNG(123)

for C in (ClaytonCopula(4,7.0),GumbelCopula(2, 1.2))
d = length(C)
Expand All @@ -126,8 +130,8 @@ end
@test Copulas.measure(C,u,v) >= 0

for i in 1:d
u[i] = rand()
v[i] = u[i] + rand()*(1-u[i])
u[i] = rand(rng)
v[i] = u[i] + rand(rng)*(1-u[i])
end
@test Copulas.measure(C,u,v) >= 0
end
Expand All @@ -140,8 +144,8 @@ end
@test_broken Copulas.measure(C,u,v) >= 0

for i in 1:d
u[i] = rand()
v[i] = u[i] + rand()*(1-u[i])
u[i] = rand(rng)
v[i] = u[i] + rand(rng)*(1-u[i])
end
@test_broken Copulas.measure(C,u,v) >= 0
end
Expand Down
4 changes: 3 additions & 1 deletion test/survivaltesting.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
@testitem "Testing survival stuff" begin
using Distributions
using StableRNGs
rng = StableRNG(123)
C = ClaytonCopula(2,3.0) # bivariate clayton with theta = 3.0
C90 = SurvivalCopula(C,(1,)) # flips the first dimension
C270 = SurvivalCopula(C,(2,)) # flips only the second dimension.
C180 = SurvivalCopula(C,(1,2)) # flips both dimensions.

u1,u2 = rand(2)
u1,u2 = rand(rng,2)
c = cdf(C,[u1,u2])
p = pdf(C,[u1,u2])
@test cdf(C90,[1-u1,u2]) == c
Expand Down
5 changes: 3 additions & 2 deletions test/williamson_test.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

@testitem "williamson test" begin
using Distributions, Random
Random.seed!(123)
using StableRNGs
rng = StableRNG(123)
taus = [0.0, 0.1, 0.5, 0.9, 1.0]

ϕ_clayton(x, θ) = max((1 + θ * x),zero(x))^(-1/θ)
Expand All @@ -13,6 +14,6 @@
WilliamsonCopula(x -> ϕ_clayton(x,-0.3),2)
)
for C in Cops
x = rand(C,1000)
x = rand(rng,C,1000)
end
end

0 comments on commit 0d739de

Please sign in to comment.