Skip to content

Commit

Permalink
finish archimeden copulas new dispatching
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Apr 21, 2020
1 parent 3ca7c99 commit f67f3f9
Show file tree
Hide file tree
Showing 5 changed files with 419 additions and 314 deletions.
7 changes: 4 additions & 3 deletions src/DatagenCopulaBased.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ module DatagenCopulaBased
# change Gaussian data by adding higher order cross-correlations
include("add_higher_order_cors.jl")

export clayton, amh, frank
export rev_clayton, rev_amh
#export clayton, amh, frank
#export rev_clayton, rev_amh

export Gaussian_cop, Student_cop, Frechet_cop, Marshall_Olkin_cop
export Gumbel_cop, Gumbel_cop_rev, Clayton_cop, Clayton_cop_rev, AMH_cop, AMH_cop_rev
export Gumbel_cop, Gumbel_cop_rev, Clayton_cop, Clayton_cop_rev, AMH_cop, AMH_cop_rev, Frank_cop

export archcopulagen, chaincopulagen, nestedarchcopulagen
export cormatgen, cormatgen_constant, cormatgen_toeplitz, convertmarg!, gcop2arch
export cormatgen_constant_noised, cormatgen_toeplitz_noised, cormatgen_rand
Expand Down
92 changes: 69 additions & 23 deletions src/archcopulagendat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ end
constructor Clayton_cop(n::Int, θ::Union{Float64, Int})
The Clayton n variate copula parametrised by θ::Union{Float64, Int}.
θ. Domain: θ ∈ (0, ∞) for n > 2 and θ ∈ [-1, 0) ∪ (0, ∞) for n = 2,
Domain: θ ∈ (0, ∞) for n > 2 and θ ∈ [-1, 0) ∪ (0, ∞) for n = 2,
supported for n::Int ≧ 2.
If Clayton_cop(n::Int, θ::Union{Float64, Int}, cor::String)
Expand Down Expand Up @@ -399,6 +399,7 @@ function simulate_copula1(t::Int, copula::AMH_cop)
end
end

#=
function amh(t::Int, n::Int, θ::Union{Float64, Int}; cor::String = "")
if (θ in [0,1]) | (n == 2)*(θ < 0)
return chaincopulagen(t, [θ], "amh"; cor=cor)
Expand All @@ -411,7 +412,7 @@ function amh(t::Int, n::Int, θ::Union{Float64, Int}; cor::String = "")
return arch_gen("amh", rand(t,n+1), θ)
end
end

=#

"""
AMH_cop_rev
Expand Down Expand Up @@ -479,43 +480,88 @@ function simulate_copula1(t::Int, copula::AMH_cop_rev)
1 .- simulate_copula1(t, AMH_cop(n, θ))
end

#=
function rev_amh(t::Int, n::Int, θ::Union{Float64, Int}; cor::String = "")
return 1 .- amh(t, n, θ; cor = cor)
end
=#

"""
struct Frank_cop
frank(t::Int, n::Int, θ::Union{Float64, Int}; cor::String = "")
constructor Frank_cop(n::Int, θ::Union{Float64, Int})
Returns: t x n Matrix{Float}, t realisations of n-variate data generated from the clayton copula
parametrised by θ. Domain θ ∈ (0, ∞) for n > 2 and θ ∈ (-∞, 0) ∪ (0, ∞) for n = 2.
The Frank n variate copula parametrised by θ::Union{Float64, Int}.
Domain: θ ∈ (0, ∞) for n > 2 and θ ∈ (-∞, 0) ∪ (0, ∞) for n = 2,
supported for n::Int ≧ 2.
It cor == "Spearman", "Kendall", uses these correlations for θ (these must be grater than zero).
If Frank_cop(n::Int, θ::Union{Float64, Int}, cor::String)
and cor == "Spearman", "Kendall", these correlations are used to compute θ,
correlations must be greater than zero.
```jldoctest
"""

julia> Random.seed!(43);

julia> frank(4, 2, 3.5)
4×2 Array{Float64,2}:
0.227231 0.363146
0.94705 0.979777
0.877446 0.824164
0.64929 0.140499
struct Frank_cop
n::Int
θ::Union{Float64, Int}
function(::Type{Frank_cop})(n::Int, θ::Union{Float64, Int})
n >= 2 || throw(AssertionError("not supported for n < 2"))
if n > 2
testθ(θ, "frank")
else
θ != 0 || throw(DomainError("bivariate frank not supported for θ = 0"))
end
new(n, θ)
end
function(::Type{Frank_cop})(n::Int, ρ::Union{Float64, Int}, cor::String)
n >= 2 || throw(AssertionError("not supported for n < 2"))
θ = getθ4arch(ρ, "frank", cor)
new(n, θ)
end
end

"""
simulate_copula1(t::Int, copula::Frank_cop)
julia> Random.seed!(43);
Returns: t x n Matrix{Float}, t realisations of n-variate Frank copula
julia> frank(4, 2, 0.2; cor = "Spearman")
4×2 Array{Float64,2}:
0.111685 0.277792
0.92239 0.97086
0.894941 0.840751
0.864546 0.271543
```jldoctest
```
julia> Random.seed!(43);
julia> simulate_copula1(4, Frank_cop(2, 3.5))
4×2 Array{Float64,2}:
0.227231 0.363146
0.94705 0.979777
0.877446 0.824164
0.64929 0.140499
julia> Random.seed!(43);
julia> simulate_copula1(4, Frank_cop(2, 0.2, "Spearman"))
4×2 Array{Float64,2}:
0.111685 0.277792
0.92239 0.97086
0.894941 0.840751
0.864546 0.271543
```
"""

function simulate_copula1(t::Int, copula::Frank_cop)
n = copula.n
θ = copula.θ
if (n == 2) &< 0)
return chaincopulagen(t, [θ], "frank")
else
return arch_gen("frank", rand(t,n+1), θ)
end
end

#=
function frank(t::Int, n::Int, θ::Union{Float64, Int}; cor::String = "")
if (n == 2)*(θ < 0)
return chaincopulagen(t, [θ], "frank"; cor=cor)
Expand All @@ -529,7 +575,7 @@ function frank(t::Int, n::Int, θ::Union{Float64, Int}; cor::String = "")
end
end

=#
"""
function testθ(θ::Union{Float64, Int}, copula::String)
Expand Down
2 changes: 1 addition & 1 deletion src/copulagendat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function archcopulagen(t::Int, n::Int, θ::Union{Float64, Int}, copula::String;
simulate_copula1(t, AMH_cop_rev(args...))
end
elseif copula == "frank"
simulate_copula(t, frank, n, θ; cor = cor)
simulate_copula1(t, Frank_cop(args...))

else
throw(AssertionError("$(copula) copula is not supported"))
Expand Down

0 comments on commit f67f3f9

Please sign in to comment.