-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,136 +1,71 @@ | ||
@testitem "Exemple 2.1" begin | ||
@testitem "Exemple 2.1 - WCopula, dimension 2" begin | ||
using Distributions | ||
d=10 | ||
ϕ = 𝒲(Dirac(1),d) | ||
generator_ex2_2(x) = max((1-x)^(d-1),0) | ||
@test all(ϕ(x) ≈ generator_ex2_2(x) for x in 0:0.01:10) | ||
d=2 | ||
X = Dirac(1) | ||
ϕ(x, d) = max((1-x)^(d-1),zero(x)) | ||
Xhat = 𝒲₋₁(x -> ϕ(x,d),d) | ||
ϕhat = 𝒲(X,d) | ||
|
||
@test maximum(abs.([cdf(X,x) - cdf(Xhat,x) for x in 0:0.01:10*d])) <= sqrt(eps(Float64)) | ||
@test maximum(abs.([ϕ(x, d) - ϕhat(x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) | ||
end | ||
|
||
@testitem "Exemple 3.2" begin | ||
@testitem "Exemple 3.2 - IndependantCopula, dimension 10" begin | ||
using Distributions | ||
d=10 | ||
ϕ = 𝒲(Erlang(10),d) | ||
gen_indep(x) = exp(-x) | ||
@test maximum(abs.([ϕ(x) - gen_indep(x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) | ||
# @test all(ϕ(x) ≈ gen_indep(x) for x in 0:0.01:10) | ||
for d in 3:20 | ||
X = Erlang(d) | ||
ϕ(x) = exp(-x) | ||
Xhat = 𝒲₋₁(ϕ,d) | ||
ϕhat = 𝒲(X,d) | ||
|
||
@test maximum(abs.([cdf(X,x) - cdf(Xhat,x) for x in 0:0.01:3*d])) <= sqrt(eps(Float64)) | ||
@test maximum(abs.([ϕ(x) - ϕhat(x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) | ||
end | ||
end | ||
|
||
|
||
# @testitem "Exemple 3.3: inverse williamson clayton" begin | ||
# using SpecialFunctions | ||
@testitem "Exemple 3.3: ClaytonCopula" begin | ||
using SpecialFunctions, Distributions | ||
|
||
# # exemple 3.3. : back to clayton. | ||
# gen_clayton(x,θ) = (1 + θ * x)^(-1/θ) | ||
# function true_radial_cdf_for_clayton(x,θ,d) | ||
# if x < 0 | ||
# return zero(x) | ||
# end | ||
# if θ < 0 | ||
# α = -1/θ | ||
# if x >= α | ||
# return one(x) | ||
# end | ||
# rez = zero(x) | ||
# θx = θ*x | ||
# cst = log(-θx/(1+θx)) | ||
# @show x, cst | ||
# for k in 0:(d-1) | ||
# rez += exp(loggamma(α+k+1) - loggamma(k+1) + k*cst) | ||
# end | ||
# rez *= (1+θx)^α/gamma(α+1) | ||
# return 1-rez | ||
# elseif θ == 0 | ||
# return exp(-x) | ||
# else | ||
# rez = zero(x) | ||
# for k in 0:(d-1) | ||
# rez += prod(1+j*θ for j in 0:(k-1))/factorial(k) * x^k * (1+θ*x)^(-(1/θ+k)) | ||
# end | ||
# return 1-rez | ||
# end | ||
# end | ||
# θ = -0.3 | ||
# X = 𝒲₋₁(x -> gen_clayton(x,θ),2) | ||
|
||
# @test maximum(abs.([true_radial_cdf_for_clayton(x,θ,2) - X.F(x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) | ||
# end | ||
|
||
|
||
|
||
|
||
|
||
|
||
# # An easy case: | ||
# F1(x) = (1 - exp(-x)) * (x > 0) | ||
# X = FromCDF(F1) | ||
# x = rand(X,10000) | ||
# Plots.plot(t -> StatsBase.ecdf(x)(t), 0, 10) | ||
# Plots.plot!(F1) | ||
|
||
# # A more involved one: | ||
# F2(x) = 1*(x >= 2) # Dirac(1) | ||
# X = FromCDF(F2) | ||
# x = rand(X,10000) | ||
# Plots.plot(t -> StatsBase.ecdf(x)(t), 0, 4) | ||
# Plots.plot!(F2) | ||
|
||
# # A more involved one: | ||
# F3(x) = Distributions.cdf(Distributions.Binomial(10,0.7),x) | ||
# X = FromCDF(F3) | ||
# x = rand(X,10000) | ||
# Plots.plot(t -> StatsBase.ecdf(x)(t), 0, 12) | ||
# Plots.plot!(F3) | ||
|
||
|
||
# # Final try: | ||
# F4(x) = (F1(x)+F2(x)+F3(x) + x>=>)/3 | ||
# X = FromCDF(F4) | ||
# x = rand(X,10000) | ||
# Plots.plot(t -> StatsBase.ecdf(x)(t), 0, 12) | ||
# Plots.plot!(F4) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
# # ϕ = 𝒲(Gamma(1,2),10) | ||
# ϕ = 𝒲(Dirac(1),10) | ||
# ϕ(0.0) | ||
# ϕ(Inf) | ||
# using Plots | ||
# plot(x -> ϕ(x),xlims=(0,10)) | ||
|
||
|
||
# # Now we could implement the inverse case | ||
# # which allows to construct the random variable corresponding to a generator. | ||
# # some precomputations might be necessary. | ||
# # and a struct. | ||
|
||
# # a certain generator from Ex 2.1: | ||
# gen_ex22(x,d) = max((1-x)^(d-1),0) | ||
# # the clayton gen from ex 2.3: | ||
# gen_clayton(x,d,θ) = (1 + θ * x)^(-1/θ) | ||
# # Note: θ = 0 generates the independence copula. | ||
# # θ < 0 is interesting, as it is equal to gen_ex22 at the lower bound θ = -1/(d-1) | ||
|
||
# # example 3.1 : | ||
# # X = Dirac(1) correspond to gen_ex22 | ||
# ϕ = 𝒲(Dirac(1),10) | ||
# plot(x -> ϕ(x),xlims=(0,10)) | ||
# plot!(x->gen_ex22(x,10)) | ||
# # indeed same plot ! | ||
|
||
|
||
# # exemple 3.2: | ||
# # independent copula correspond to erlang distribution with parameter d | ||
# ϕ = 𝒲(Erlang(10),10) | ||
# gen_indep(x) = exp(-x) | ||
# plot(x -> ϕ(x),xlims=(0,10)) | ||
# plot!(gen_indep) | ||
|
||
|
||
# we should check if a williamson d-transform of this distribution recovers the generator correctly. | ||
|
||
|
||
# exemple 3.3. : back to clayton. | ||
ϕ(x, θ) = max((1 + θ * x),zero(x))^(-1/θ) | ||
function F(x, θ, d) | ||
if x < 0 | ||
return zero(x) | ||
end | ||
α = -1/θ | ||
if θ < 0 | ||
if x >= α | ||
return one(x) | ||
end | ||
rez = zero(x) | ||
x_α = x/α | ||
for k in 0:(d-1) | ||
rez += gamma(α+1)/gamma(α-k+1)/gamma(k+1) * (x_α)^k * (1 - x_α)^(α-k) | ||
end | ||
return 1-rez | ||
elseif θ == 0 | ||
return exp(-x) | ||
else | ||
rez = zero(x) | ||
for k in 0:(d-1) | ||
pr = one(θ) | ||
for j in 0:(k-1) | ||
pr *= (1+j*θ) | ||
end | ||
rez += pr / gamma(k+1) * x^k * (1 + θ * x)^(-(1/θ+k)) | ||
end | ||
return 1-rez | ||
end | ||
end | ||
|
||
for (d, θ) in ( | ||
(3, 1/7), | ||
(2, -0.2), | ||
(10, -1/10), | ||
(2, -1.0) | ||
) | ||
Xhat = 𝒲₋₁(x -> ϕ(x,θ),d) | ||
@test maximum(abs.([F(x,θ,d) - cdf(Xhat,x) for x in 0:0.01:10])) <= sqrt(eps(Float64)) | ||
end | ||
|
||
end |
9c26381
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
Release notes:
First version !
9c26381
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/94221
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: