Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
145 commits
Select commit Hold shift + click to select a range
f4670ae
first shot for add vcov
Santymax98 Oct 2, 2025
dd51a7c
first shot
lrnv Oct 3, 2025
62bf463
clear up archimedean too
lrnv Oct 3, 2025
2f419cd
clear up archimedans.
lrnv Oct 3, 2025
e857df6
refactor -- first pass
lrnv Oct 3, 2025
3aef744
simplified entropy and lower gaussian burden
lrnv Oct 3, 2025
3ae5c59
clear out gaussian and elliptical
lrnv Oct 3, 2025
ad339fe
add godambe_pairwise
lrnv Oct 3, 2025
62b4a5e
clear up EVC
lrnv Oct 3, 2025
c56e565
Refactor and simplify the code
lrnv Oct 3, 2025
1f38f66
clear out global diff
lrnv Oct 3, 2025
985ab30
debugging...
lrnv Oct 3, 2025
1b7c079
working!
lrnv Oct 4, 2025
c9b734a
enhance show function
lrnv Oct 4, 2025
c9c3ac9
first shot for add vcov
Santymax98 Oct 4, 2025
9aef2c7
first shot
lrnv Oct 4, 2025
375819d
clear up archimedean too
lrnv Oct 4, 2025
a3cd77c
clear up archimedans.
lrnv Oct 4, 2025
d4f071f
refactor -- first pass
lrnv Oct 4, 2025
a440c85
simplified entropy and lower gaussian burden
lrnv Oct 4, 2025
76d6bb0
clear out gaussian and elliptical
lrnv Oct 4, 2025
de38b3b
add godambe_pairwise
lrnv Oct 4, 2025
a7fc773
clear up EVC
lrnv Oct 4, 2025
e4cf3d8
Refactor and simplify the code
lrnv Oct 4, 2025
eb0a251
clear out global diff
lrnv Oct 4, 2025
af6cee4
debugging...
lrnv Oct 4, 2025
c08765d
working!
lrnv Oct 4, 2025
6879586
enhance show function
lrnv Oct 4, 2025
b5311e8
Merge branch 'add_vcov_clean' of https://github.com/santymax98/copula…
lrnv Oct 4, 2025
3cae77b
up
lrnv Oct 4, 2025
f544ae4
one typo
lrnv Oct 4, 2025
5689490
correct docs
lrnv Oct 4, 2025
923a956
typo...
lrnv Oct 4, 2025
0eb68ce
fix archimax typign issue
lrnv Oct 4, 2025
fa9fbde
add any test for vcov and inference...
Santymax98 Oct 5, 2025
482e8f6
adding test and new functions StatsBase
Santymax98 Oct 5, 2025
2aad887
up
Santymax98 Oct 5, 2025
e313efe
Comment out filter in runtests.jl
lrnv Oct 5, 2025
979752b
Fix Gaussian & Survival
lrnv Oct 5, 2025
cd18cf1
Merge branch 'add_vcov_clean' of https://github.com/santymax98/copula…
lrnv Oct 5, 2025
9e2b6be
typo
lrnv Oct 5, 2025
e75469e
Disable vcov for TCopula
lrnv Oct 5, 2025
dbfb071
remove interactiveutils cause unused ?
lrnv Oct 5, 2025
4980365
remove flay test
lrnv Oct 5, 2025
51ce740
fixing FGMCOPULA
Santymax98 Oct 5, 2025
35cf2a5
add a dimension parameter to _availiale_fitting_methods
lrnv Oct 5, 2025
9c931be
Specialize fitting methods for FGM
lrnv Oct 5, 2025
a381ce5
Disable :irho for Gumbel and FGM mle vcov
lrnv Oct 5, 2025
f6770f0
Merge branch 'add_vcov_clean' of https://github.com/santymax98/copula…
lrnv Oct 5, 2025
2e45b1a
restore previous constructor.
lrnv Oct 5, 2025
41b458d
typo...
lrnv Oct 5, 2025
b7bba4a
fu...
lrnv Oct 6, 2025
3e8c79c
fix FGM
lrnv Oct 6, 2025
aeff408
Remove soem Float64 enforcements.
lrnv Oct 6, 2025
0978647
Fix EV output of paramters
lrnv Oct 6, 2025
c39c3f6
fix vcov retrieving paramters
lrnv Oct 6, 2025
6e34de7
fixed mle evd
Santymax98 Oct 6, 2025
8e273e1
Robust hessian _vcov
Santymax98 Oct 6, 2025
629abe9
delete simlink
lrnv Oct 6, 2025
c636a0c
frank is very slow
Santymax98 Oct 6, 2025
bcf7276
Merge branch 'add_vcov_clean' of https://github.com/santymax98/copula…
lrnv Oct 6, 2025
4c9ea7d
fix a few things around
lrnv Oct 6, 2025
d1ac9e8
trying vcov with bootstrap
Santymax98 Oct 6, 2025
3316af8
try to implement kth derivatives for BBx
Santymax98 Oct 7, 2025
cc4ee21
relaxing tolerance
Santymax98 Oct 7, 2025
1fd1179
stable bivariate bbx for archimax
Santymax98 Oct 7, 2025
0afbb1e
Revise introduction in documentation for Copulas (#312)
lrnv Oct 7, 2025
1bdaf47
tolerance fixing...
Santymax98 Oct 7, 2025
9f35da2
[Internals] Back to TaylorSeries.jl (#313)
lrnv Oct 7, 2025
62f2f9f
restore removed changes sorry
lrnv Oct 7, 2025
31812d0
first shot for add vcov
Santymax98 Oct 7, 2025
8c9c19b
first shot
lrnv Oct 7, 2025
caf0181
clear up archimedean too
lrnv Oct 7, 2025
b374b8c
clear up archimedans.
lrnv Oct 7, 2025
c7779aa
refactor -- first pass
lrnv Oct 7, 2025
e0c9148
simplified entropy and lower gaussian burden
lrnv Oct 7, 2025
baedbad
clear out gaussian and elliptical
lrnv Oct 7, 2025
78382c9
add godambe_pairwise
lrnv Oct 7, 2025
820c563
clear up EVC
lrnv Oct 7, 2025
9b64698
Refactor and simplify the code
lrnv Oct 7, 2025
8c9ce26
clear out global diff
lrnv Oct 7, 2025
6ad2485
debugging...
lrnv Oct 7, 2025
ca0355a
working!
lrnv Oct 7, 2025
7850341
enhance show function
lrnv Oct 7, 2025
9b2f625
first shot for add vcov
Santymax98 Oct 7, 2025
495c79b
first shot
lrnv Oct 7, 2025
2971b7f
clear up archimedean too
lrnv Oct 7, 2025
00c2894
clear up archimedans.
lrnv Oct 7, 2025
0a1ef68
refactor -- first pass
lrnv Oct 7, 2025
44e129e
simplified entropy and lower gaussian burden
lrnv Oct 7, 2025
0ae36be
clear out gaussian and elliptical
lrnv Oct 7, 2025
e2ef403
add godambe_pairwise
lrnv Oct 7, 2025
7fbca10
clear up EVC
lrnv Oct 7, 2025
8b6b164
Refactor and simplify the code
lrnv Oct 7, 2025
2a546f2
clear out global diff
lrnv Oct 7, 2025
7fbff09
debugging...
lrnv Oct 7, 2025
9cbac6b
working!
lrnv Oct 7, 2025
1c1aac6
enhance show function
lrnv Oct 7, 2025
6fc677b
up
lrnv Oct 7, 2025
4dd2413
one typo
lrnv Oct 7, 2025
0aca73d
correct docs
lrnv Oct 7, 2025
2c5b7bd
typo...
lrnv Oct 7, 2025
f70c829
fix archimax typign issue
lrnv Oct 7, 2025
5ac7e88
Fix Gaussian & Survival
lrnv Oct 7, 2025
0678445
add any test for vcov and inference...
Santymax98 Oct 7, 2025
ace3bc7
adding test and new functions StatsBase
Santymax98 Oct 7, 2025
683f1c0
up
Santymax98 Oct 7, 2025
fafabe0
Comment out filter in runtests.jl
lrnv Oct 7, 2025
cbcb78c
typo
lrnv Oct 7, 2025
a1d5a90
Disable vcov for TCopula
lrnv Oct 7, 2025
1880fb6
remove interactiveutils cause unused ?
lrnv Oct 7, 2025
0ae8c98
remove flay test
lrnv Oct 7, 2025
870a68a
add a dimension parameter to _availiale_fitting_methods
lrnv Oct 7, 2025
b9e173e
Specialize fitting methods for FGM
lrnv Oct 7, 2025
14659cb
Disable :irho for Gumbel and FGM mle vcov
lrnv Oct 7, 2025
4172707
fixing FGMCOPULA
Santymax98 Oct 7, 2025
cec0660
restore previous constructor.
lrnv Oct 7, 2025
144ebf4
typo...
lrnv Oct 7, 2025
d50b0aa
fu...
lrnv Oct 7, 2025
1aed2b4
fix FGM
lrnv Oct 7, 2025
95a89ca
Remove soem Float64 enforcements.
lrnv Oct 7, 2025
bc74d9e
Fix EV output of paramters
lrnv Oct 7, 2025
7f05449
fix vcov retrieving paramters
lrnv Oct 7, 2025
b9ddb58
delete simlink
lrnv Oct 7, 2025
1ac52f9
fixed mle evd
Santymax98 Oct 7, 2025
95ec113
Robust hessian _vcov
Santymax98 Oct 7, 2025
d283ed8
frank is very slow
Santymax98 Oct 7, 2025
45d5df8
fix a few things around
lrnv Oct 7, 2025
2c882fa
trying vcov with bootstrap
Santymax98 Oct 7, 2025
d5ebb35
try to implement kth derivatives for BBx
Santymax98 Oct 7, 2025
47ae6d7
relaxing tolerance
Santymax98 Oct 7, 2025
a103a75
stable bivariate bbx for archimax
Santymax98 Oct 7, 2025
48a15d2
tolerance fixing...
Santymax98 Oct 7, 2025
7a81250
restore removed changes sorry
lrnv Oct 7, 2025
c11cbe2
Merge branch 'add_vcov_clean' of https://github.com/santymax98/copula…
lrnv Oct 7, 2025
95d40c5
fix a few Val{} still there
lrnv Oct 7, 2025
236e131
small changes
lrnv Oct 7, 2025
7965bfc
one first merging error
lrnv Oct 7, 2025
d1d13d0
Try to disable upper generator for BBs to see if it works.
lrnv Oct 7, 2025
fcb4c7e
typo
lrnv Oct 7, 2025
d617ae2
test everything now
lrnv Oct 7, 2025
0d27a2a
no vcov for tEV
lrnv Oct 7, 2025
3d28092
new dependence metrics tests
lrnv Oct 8, 2025
2844def
back to previous tests
lrnv Oct 8, 2025
fc12c12
avoid inverting zeros in sklardist
lrnv Oct 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/ArchimedeanCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ function _fit(CT::Type{<:ArchimedeanCopula{d, GT} where {d, GT<:UnivariateGenera
θs = map(v -> invf(GT, clamp(v, -1, 1)), upper_triangle_flat)

θ = clamp(Statistics.mean(θs), _θ_bounds(GT, d)...)
return CT(d, θ), (; θ̂=θ, eps)
return CT(d, θ), (; θ̂=(θ=θ,))
end
function _fit(CT::Type{<:ArchimedeanCopula{d, GT} where {d, GT<:UnivariateGenerator}}, U, ::Val{:ibeta})
d = size(U,1); δ = 1e-8; GT = generatorof(CT)
Expand All @@ -226,7 +226,7 @@ function _fit(CT::Type{<:ArchimedeanCopula{d, GT} where {d, GT<:UnivariateGenera
βmin, βmax = fβ(a0), fβ(b0)
if βmin > βmax; βmin, βmax = βmax, βmin; end
θ = βobs ≤ βmin ? a0 : βobs ≥ βmax ? b0 : Roots.find_zero(θ -> fβ(θ)-βobs, (a0,b0), Roots.Brent(); xatol=1e-8, rtol=0)
return CT(d,θ), (; θ̂=θ)
return CT(d,θ), (; θ̂=(θ=θ,))
end

function _fit(CT::Type{<:ArchimedeanCopula{d, GT} where {d, GT<:UnivariateGenerator}}, U, ::Val{:mle}; start::Union{Symbol,Real}=:itau, xtol::Real=1e-8)
Expand All @@ -245,8 +245,8 @@ function _fit(CT::Type{<:ArchimedeanCopula{d, GT} where {d, GT<:UnivariateGenera
θ₀[1] = clamp(θ₀[1], lo, hi)
f(θ) = -Distributions.loglikelihood(CT(d, θ[1]), U)
res = Optim.optimize(f, lo, hi, θ₀, Optim.Fminbox(Optim.LBFGS()), autodiff = :forward)
θ̂ = Optim.minimizer(res)[1]
return CT(d, θ̂), (; θ̂=θ̂, optimizer=:GradientDescent,
θ = Optim.minimizer(res)[1]
return CT(d, θ), (; θ̂=(θ=θ,), optimizer=Optim.summary(res),
xtol=xtol, converged=Optim.converged(res),
iterations=Optim.iterations(res))
end
101 changes: 21 additions & 80 deletions src/Copula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,15 @@ function β(C::Copula{d}) where {d}
Cbar0 = Distributions.cdf(SurvivalCopula(C, Tuple(1:d)), u)
return (2.0^(d-1) * C0 + Cbar0 - 1) / (2^(d-1) - 1)
end
function γ(C::Copula{d}; nmc::Int=100_000, rng::Random.AbstractRNG=Random.MersenneTwister(123)) where {d}
d ≥ 2 || throw(ArgumentError("γ(C) requires d≥2"))
if d == 2
f(t) = Distributions.cdf(C, [t, t]) + Distributions.cdf(C, [t, 1 - t])
I, _ = QuadGK.quadgk(f, 0.0, 1.0; rtol=sqrt(eps()))
return -2 + 4I
end
@inline _A(u) = (minimum(u) + max(sum(u) - d + 1, 0.0)) / 2
@inline _Abar(u) = (1 - maximum(u) + max(1 - sum(u), 0.0)) / 2
@inline invfac(k::Integer) = exp(-SpecialFunctions.logfactorial(k))
s = 0.0
@inbounds for i in 0:d
s += (isodd(i) ? -1.0 : 1.0) * binomial(d, i) * invfac(i + 1)
end
a_d = 1/(d + 1) + 0.5*invfac(d + 1) + 0.5*s
b_d = 2/3 + 4.0^(1 - d) / 3
U = rand(rng, C, nmc)
m = 0.0
@inbounds for j in 1:nmc
u = @view U[:, j]
m += _A(u) + _Abar(u)
end
m /= nmc
return (m - a_d) / (b_d - a_d)
function γ(C::Copula{d}) where {d}
_integrand(u) = (1 + minimum(u) - maximum(u) + max(abs(sum(u) - d/2) - (d - 2)/2, 0.0)) / 2
I = Distributions.expectation(_integrand, C; nsamples=1e5)
a = 1/(d+1) + 1/factorial(d+1) # independence
b = (2 + 4.0^(1-d)) / 3 # comonotonicity
return (I - a) / (b - a)
end

function ι(C::Copula{d}; nmc::Int=100_000, rng::Random.AbstractRNG=Random.MersenneTwister(123)) where {d}
U = rand(rng, C, nmc)
s = 0.0
@inbounds for j in 1:nmc
u = @view U[:, j]
lp = Distributions.logpdf(C, u)
isfinite(lp) || throw(DomainError(lp, "logpdf(C,u) non-finite."))
s -= lp
end
H = s / nmc
t = clamp(2H, -700.0, 0.0)
r = sqrt(max(0.0, 1 - exp(t)))
return (H = H, I = -H, r = r)
function ι(C::Copula{d}) where {d}
return Distributions.expectation(u -> -logpdf(C, u), C; nsamples=10^4)
end
function λₗ(C::Copula{d}; ε::Float64 = 1e-10) where {d}
g(e) = Distributions.cdf(C, fill(e, d)) / e
Expand Down Expand Up @@ -130,51 +100,24 @@ function ρ(U::AbstractMatrix)
return h * (2.0^d * μ - 1.0)
end
function γ(U::AbstractMatrix)
# Assumes pseudo-data given. Multivariate Gini’s gamma (Behboodian–Dolati–Úbeda, 2007)
d, n = size(U)
if d == 2
# Schechtman–Yitzhaki symmetric Gini over ranks (copular invariant)
r1 = StatsBase.tiedrank(@view U[1, :])
r2 = StatsBase.tiedrank(@view U[2, :])
m = n
h = m + 1
acc = 0.0
@inbounds @simd for k in 1:m
acc += abs(r1[k] + r2[k] - h) - abs(r1[k] - r2[k])
end
return 2*acc / (m*h)
else
@inline _A(u) = (minimum(u) + max(sum(u) - d + 1, 0.0)) / 2
@inline _Abar(u) = (1 - maximum(u) + max(1 - sum(u), 0.0)) / 2
invfac(k::Integer) = exp(-SpecialFunctions.logfactorial(k))
s = 0.0
binomf(d,i) = exp(SpecialFunctions.loggamma(d+1) - SpecialFunctions.loggamma(i+1) - SpecialFunctions.loggamma(d-i+1))
@inbounds for i in 0:d
s += (isodd(i) ? -1.0 : 1.0) * binomf(d,i) * invfac(i + 1)
end
a_d = 1/(d + 1) + 0.5*invfac(d + 1) + 0.5*s
b_d = 2/3 + 4.0^(1 - d) / 3
m = 0.0
@inbounds for j in 1:n
u = @view U[:, j]
m += _A(u) + _Abar(u)
end
m /= n
return (m - a_d) / (b_d - a_d)
I = zero(eltype(U))
for j in 1:n
u = U[:,j]
I += (1 + minimum(u) - maximum(u) + max(abs(sum(u) - d/2) - (d - 2)/2, 0.0)) / 2
end
I /= n
a = 1/(d+1) + 1/factorial(d+1)
b = (2 + 4.0^(1-d)) / 3
return (I - a) / (b - a)
end
function _λ(U::AbstractMatrix; t::Symbol=:upper, p::Union{Nothing,Real}=nothing)
# Assumes pseudo-data given. Multivariate tail’s lambda (Schmidt, R. & Stadtmüller, U. 2006)
d, m = size(U)
m ≥ 4 || throw(ArgumentError("At least 4 observations are required"))
p === nothing && (p = 1/sqrt(m))
p === nothing && (p = 1/sqrt(size(U, 2)))
(0 < p < 1) || throw(ArgumentError("p must be in (0,1)"))
V = t === :upper ? (1 .- Float64.(U)) : Float64.(U)
cnt = 0
@inbounds @views for j in 1:m
cnt += all(V[:, j] .<= p) # vista sin copiar gracias a @views
end
return clamp(cnt / (p*m), 0.0, 1.0)
in_tail = t=== :upper ? Base.Fix2(>=, 1-p) : Base.Fix2(<=, p)
prob = Statistics.mean(all(in_tail, U, dims=1))
return clamp(prob/p, 0.0, 1.0)
end
λₗ(U::AbstractMatrix; p::Union{Nothing,Real}=nothing) = _λ(U; t=:lower, p=p)
λᵤ(U::AbstractMatrix; p::Union{Nothing,Real}=nothing) = _λ(U; t=:upper, p=p)
Expand Down Expand Up @@ -280,9 +223,7 @@ function ι(U::AbstractMatrix; k::Int=5, p::Real=Inf, leafsize::Int=32)
logcd = d*log(2*SpecialFunctions.gamma(1 + 1/p)) - SpecialFunctions.loggamma(1 + d/p)
H += logcd + (d / n) * sum(log.(ρ))
end
t = clamp(2H, -700.0, 0.0)
r = sqrt(max(0.0, 1 - exp(t)))
return (H = H, I = -H, r = r)
return H
end

# Measure function.
Expand Down
6 changes: 3 additions & 3 deletions src/EllipticalCopulas/GaussianCopula.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ N(::Type{T}) where T<: GaussianCopula = Distributions.MvNormal
function _cdf(C::CT,u) where {CT<:GaussianCopula}
x = StatsBase.quantile.(Distributions.Normal(), u)
d = length(C)
return MvNormalCDF.mvnormcdf(C.Σ, fill(-Inf, d), x, m=10_0000d)[1]
return MvNormalCDF.mvnormcdf(C.Σ, fill(-Inf, d), x)[1]
end

function rosenblatt(C::GaussianCopula, u::AbstractMatrix{<:Real})
Expand Down Expand Up @@ -139,6 +139,6 @@ end
function _fit(CT::Type{<:GaussianCopula}, u, ::Val{:mle})
dd = Distributions.fit(N(CT), StatsBase.quantile.(U(CT),u))
Σ = Matrix(dd.Σ)
return GaussianCopula(Σ), (;)
return GaussianCopula(Σ), (; θ̂ = (; Σ = Σ))
end
_available_fitting_methods(::Type{<:GaussianCopula}) = (:itau, :irho, :ibeta, :mle)
_available_fitting_methods(::Type{<:GaussianCopula}) = (:mle, :itau, :irho, :ibeta)
Loading