Skip to content

Commit

Permalink
Merge pull request #36 from lrnv/fix_miscelaneous
Browse files Browse the repository at this point in the history
Adding special routes for the independent copulas sampling cdf and pdf, fix a few typos, add a test
  • Loading branch information
lrnv authored Oct 24, 2023
2 parents c47893d + 101e800 commit 98d6443
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/ArchimedeanCopulas/IndependentCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ It happends to be an Archimedean Copula, with generator :
```
"""
struct IndependentCopula{d} <: ArchimedeanCopula{d} end
IndependentCopula(d) = IndependentCopula{d}
function Distributions._logpdf(::IndependentCopula{d},u) where d
return all(0 .<= u .<= 1) ? 1 : 0
IndependentCopula(d) = IndependentCopula{d}()
function Distributions._logpdf(C::IndependentCopula{d},u) where d
return all(0 .<= u .<= 1) ? zero(eltype(u)) : -Inf
end
function Distributions.cdf(::IndependentCopula{d},u) where d
return all(0 .<= u .<= 1) ? prod(u) : 0
function Distributions.cdf(C::IndependentCopula{d},u) where d
return prod(u)
end
ϕ(::IndependentCopula,t) = exp(-t)
ϕ⁻¹(::IndependentCopula,t) = -log(t)
τ(::IndependentCopula) = 0

# Exceptionally we overload the functions as we dont want to take the slow route of archemedean copulas for the independant copula.

function Distributions._rand!(rng::Distributions.AbstractRNG, C::IndependentCopula{d}, x::AbstractVector{T}) where {T<:Real,d}
Random.rand!(rng,x)
end
function Base.rand(rng::Distributions.AbstractRNG,C::IndependentCopula{d}) where {d}
rand(rng,length(C))
end
4 changes: 2 additions & 2 deletions src/Copulas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ end Copulas
include("MiscellaneousCopulas/WCopula.jl")
include("MiscellaneousCopulas/SurvivalCopula.jl")
export MCopula,
Wcopula,
SurvivalCopula
WCopula,
SurvivalCopula

# PrecompileTools stuff
@setup_workload begin
Expand Down
2 changes: 1 addition & 1 deletion src/MiscellaneousCopulas/MCopula.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct MCopula{d} <: Copula{d} end
MCopula(d) = MCopula{d}()
Distributions.cdf(::MCopula{d},u) where {d} = min(u)
Distributions.cdf(::MCopula{d},u) where {d} = minimum(u)
function Distributions._rand!(rng::Distributions.AbstractRNG, ::MCopula{d}, x::AbstractVector{T}) where {d,T<:Real}
x .= rand(rng)
end
Expand Down
71 changes: 71 additions & 0 deletions test/sampling_test
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# @testitem "test default parametrisation of every concrete copula types" begin
# for T in subtypes(Copulas)
# if isconcretetype(T)
# rand(T(),100)
# @test true
# end
# end
# end

# @testitem "test sampling of every concrete copula type" begin
# for T in subtypes(Copulas)
# if isconcretetype(T)
# rand(T(),100)
# @test true
# end
# end
# end


@testitem "small sampling test" begin
# test constructed from https://github.com/lrnv/Copulas.jl/issues/35
using Copulas, Distributions
rand(GaussianCopula([1.0 0.5; 0.5 1.0]),1)
rand(IndependentCopula(2),1)
rand(MCopula(2),1)
rand(WCopula(2),1)

@test true
end


@testitem "test cdf for three copulas." begin
# test constructed from https://github.com/lrnv/Copulas.jl/issues/35
using Copulas, Distributions

u = range(0, stop=1, length=100)

for G in (
GaussianCopula([1.0 0.5; 0.5 1.0]),
IndependentCopula(2),
MCopula(2),
WCopula(2)
)
for uᵢ in u
for vᵢ in u
cdf(G,[uᵢ,vᵢ])
end
end
end
@test true
end

@testitem "test pdf for three copulas." begin
# test constructed from https://github.com/lrnv/Copulas.jl/issues/35
using Copulas, Distributions

u = range(0, stop=1, length=100)

for G in (
GaussianCopula([1.0 0.5; 0.5 1.0]),
IndependentCopula(2),
# MCopula(2)
)
for uᵢ in u
for vᵢ in u
pdf(G,[uᵢ,vᵢ])
end
end
end
@test true
end

0 comments on commit 98d6443

Please sign in to comment.