Skip to content

Commit

Permalink
start changing code for ::Real type
Browse files Browse the repository at this point in the history
  • Loading branch information
kdomino committed Dec 2, 2020
1 parent e6b10c6 commit f7706f1
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 324 deletions.
33 changes: 0 additions & 33 deletions README.md
Expand Up @@ -902,39 +902,6 @@ julia> quantile.(Levy(0, 1), U)
0.582946 3.52799
```

## Old dispatching, will be removed in future

```julia
julia> gausscopulagen(t::Int, Σ::Matrix{Float64})
```

```julia
julia> archcopulagen(t::Int, n::Int, θ::Union{Float64, Int}, copula::String; rev::Bool = false, cor::String = "")
```

```julia
julia> nestedarchcopulagen(t::Int, n::Vector{Int}, ϕ::Vector{Float64}, θ::Float64, copula::String, m::Int = 0)
```

```julia
julia> chaincopulagen(t::Int, θ::Union{Vector{Float64}, Vector{Int}}, copula::Vector{String}; rev::Bool = false, cor::String = "")
```

```julia
julia> marshallolkincopulagen(t::Int, λ::Vector{Float64})
```

```julia
julia> frechetcopulagen(t::Int, n::Int, α::Union{Int, Float64})
```

```julia
julia> frechetcopulagen(t::Int, n::Int, α::Union{Int, Float64}, β::Union{Int, Float64})
```

```julia
julia> chainfrechetcopulagen(t::Int, α::Vector{Float64}, β::Vector{Float64} = zeros(α))
```

# Citing this work

Expand Down
4 changes: 2 additions & 2 deletions src/DatagenCopulaBased.jl
Expand Up @@ -15,7 +15,7 @@ module DatagenCopulaBased
include("sampleunivdists.jl")

# dispatching of the generator
include("copulagendat.jl")
#include("copulagendat.jl")

# axiliary function for correlqations
include("corgen.jl")
Expand Down Expand Up @@ -47,5 +47,5 @@ module DatagenCopulaBased
export simulate_copula, simulate_copula!

# obsolete implemntations
export tstudentcopulagen, gausscopulagen, frechetcopulagen, marshallolkincopulagen
#export tstudentcopulagen, gausscopulagen, frechetcopulagen, marshallolkincopulagen
end
22 changes: 11 additions & 11 deletions src/add_higher_order_cors.jl
Expand Up @@ -36,7 +36,7 @@ julia> gcop2tstudent(x, [1,2], 6)
-0.710786 0.239012 -1.54419
```
"""
function gcop2tstudent(x::Matrix{Real}, ind::Vector{Int}, ν::Int; naive::Bool = false, rng = Random.GLOBAL_RNG)
function gcop2tstudent(x::Matrix{T}, ind::Vector{Int}, ν::Int; naive::Bool = false, rng = Random.GLOBAL_RNG) where T <: Real
unique(ind) == ind || throw(AssertionError("indices must not repeat"))
y = copy(x)
Σ = cov(x)
Expand Down Expand Up @@ -97,7 +97,7 @@ julia> gcop2arch(x, ["clayton" => [1,2]]; naive::Bool = false, notnested::Bool =
-0.657297 -0.339814 -1.54419
```
"""
function gcop2arch(x::Matrix{Real}, inds::VP; naive::Bool = false, notnested::Bool = false, rng = Random.GLOBAL_RNG)
function gcop2arch(x::Matrix{T}, inds::VP; naive::Bool = false, notnested::Bool = false, rng = Random.GLOBAL_RNG) where T <: Real
testind(inds)
S = transpose(sqrt.(diag(cov(x))))
μ = mean(x, dims=1)
Expand Down Expand Up @@ -154,7 +154,7 @@ julia> gcop2frechet(x, [1,2])
-0.7223 -0.172507 -1.54419
```
"""
function gcop2frechet(x::Matrix{Real}, inds::Vector{Int}; naive::Bool = false, rng = Random.GLOBAL_RNG)
function gcop2frechet(x::Matrix{T}, inds::Vector{Int}; naive::Bool = false, rng = Random.GLOBAL_RNG) where T <: Real
unique(inds) == inds || throw(AssertionError("indices must not repeat"))
S = transpose(sqrt.(diag(cov(x))))
μ = mean(x, dims = 1)
Expand Down Expand Up @@ -202,7 +202,7 @@ julia> gcop2marshallolkin(x, [1,2], 1., 1.5; naive = false)
-0.867606 -0.589929 -1.54419
```
"""
function gcop2marshallolkin(x::Matrix{Real}, inds::Vector{Int}, λ1::Real = 1., λ2::Real = 1.5; naive::Bool = false, rng = Random.GLOBAL_RNG)
function gcop2marshallolkin(x::Matrix{T}, inds::Vector{Int}, λ1::T = 1., λ2::T = 1.5; naive::Bool = false, rng = Random.GLOBAL_RNG) where T <: Real
unique(inds) == inds || throw(AssertionError("indices must not repeat"))
length(inds) == 2 || throw(AssertionError("not supported for |inds| > 2"))
λ1 >= 0 || throw(DomainError("not supported for λ1 < 0"))
Expand Down Expand Up @@ -238,7 +238,7 @@ end
Return uniformly distributed data from x[:,i] given a copula familly.
"""
function norm2unifind(x::Matrix{Real}, i::Vector{Int}, cop::String = "")
function norm2unifind(x::Matrix{T}, i::Vector{Int}, cop::String = "") where T <: Real
x = (cop == "frechet") ? x[:,i] : hcat(x[:,i], randn(size(x,1),1))
a, s = eigen(cor(x))
w = x*s./transpose(sqrt.(a))
Expand All @@ -259,14 +259,14 @@ julia> meanΣ(s)
0.3
```
"""
meanΣ::Matrix{Real}) = mean(abs.(Σ[findall(tril-Matrix(I, size(Σ))).!=0)]))
meanΣ::Matrix{T}) where T <: Real = mean(abs.(Σ[findall(tril-Matrix(I, size(Σ))).!=0)]))

"""
mean_outer(Σ::Matrix{Real}, part::Vector{Vector{Int}})
returns a mean correlation excluding internal one is subsets determined by part
"""
function mean_outer::Matrix{Real}, part::Vector{Vector{Int}})
function mean_outer::Matrix{T}, part::Vector{Vector{Int}}) where T <: Real
Σ_copy = copy(Σ)-Matrix(I, size(Σ))
for ind=part
Σ_copy[ind,ind] = zeros(length(ind),length(ind))
Expand All @@ -280,7 +280,7 @@ end
Returns parametrization by correlation for data `x` and partition `part` for nested copulas.
"""
function parameters::Matrix{Real}, part::Vector{Vector{Int}})
function parameters::Matrix{T}, part::Vector{Vector{Int}}) where T <: Real
ϕ = [meanΣ(Σ[ind,ind]) for ind=part]
θ = mean_outer(Σ, part)
ϕ, θ
Expand All @@ -291,7 +291,7 @@ end
tests sufficient nesting condition given parameters, returns bool
"""
function are_parameters_good::Vector{Real}, θ::Real)
function are_parameters_good::Vector{T}, θ::T) where T <: Real
θ < minimum(filter(x->!isnan(x), ϕ))
end

Expand All @@ -301,7 +301,7 @@ end
returns a matrix indicating a theoretical correlation according togiven parameters
and partition
"""
function Σ_theor::Vector{Real}, θ::Real, part::Vector{Vector{Int}})
function Σ_theor::Vector{T}, θ::T, part::Vector{Vector{Int}}) where T <: Real
n = sum(length.(part))
result = fill(θ, n, n)
for (ϕind,ind)=zip(ϕ,part)
Expand All @@ -318,7 +318,7 @@ end
clusters data on a basis of a correlation
"""
function getcors_advanced(x::Matrix{Real})
function getcors_advanced(x::Matrix{T}) where T <: Real
Σ = corspearman(x)
var_no = size(Σ, 1)
partitions = collect(Combinatorics.SetPartitions(1:var_no))[2:end-1] #TODO popraw first is trivial, last is nested
Expand Down
2 changes: 1 addition & 1 deletion src/archcopcorrelations.jl
Expand Up @@ -19,7 +19,7 @@ Debye(x::Real, k::Int=1) = k/x^k*(quadgk(i -> i^k/(exp(i)-1), 0, x)[1])
Returns Float, a single parameter of Archimedean copula, given the Kenalss τ correlation
"""
function τ2θ::Real, copula::String)
function τ2θ::T, copula::String) where T <: Real
if copula == "gumbel"
return 1/(1-τ)
elseif copula == "clayton"
Expand Down
4 changes: 3 additions & 1 deletion src/archcopulagendat.jl
Expand Up @@ -68,7 +68,9 @@ Given a vector of random numbers r of size n+1, return the sample from the Clayt
copula parametrised by θ of the size n.
"""
function clayton_gen(r::Vector{T}, θ::T) where T <: Real
u = -log.(r[1:end-1])./quantile.(Gamma{T}(1/θ, 1), r[end])
gamma_vec = quantile.(Gamma{T}(1/θ, 1), r[end])
#gamma_vec = [gamma_inc_inv(1/θ, T(1.), q) for q in r[end]]
u = -log.(r[1:end-1])./gamma_vec
return (1 .+ u).^(-1/θ)
end

Expand Down
4 changes: 2 additions & 2 deletions src/chaincopulagendat.jl
Expand Up @@ -105,7 +105,7 @@ struct Chain_of_Archimedeans{T}
copula in ["clayton", "amh", "frank"] || throw(AssertionError("$(copula) copula is not supported"))
new{T}(n, θ, [copula for i in 1:n-1])
end
function(::Type{Chain_of_Archimedeans})(θ::Vector{Real}, copula::String, cor::Type{<:CorrelationType}) where T <: Real
function(::Type{Chain_of_Archimedeans})(θ::Vector{T}, copula::String, cor::Type{<:CorrelationType}) where T <: Real
n = length(θ)+1
map(i -> testbivθ(θ[i], copula), 1:n-1)
copula in ["clayton", "amh", "frank"] || throw(AssertionError("$(copula) copula is not supported"))
Expand Down Expand Up @@ -321,7 +321,7 @@ function simulate_copula(t::Int, copula::Chain_of_Frechet{T}; rng::AbstractRNG =
α = copula.α
β = copula.β
n = copula.n
fncopulagen(α, β, rand(rng, t, n))
fncopulagen(α, β, rand(rng, T, t, n))
end

"""
Expand Down
110 changes: 0 additions & 110 deletions src/copulagendat.jl

This file was deleted.

4 changes: 2 additions & 2 deletions src/eliptic_fr_mo_copulas.jl
Expand Up @@ -56,7 +56,7 @@ julia> simulate_copula(10, Gaussian_cop([1. 0.5; 0.5 1.]))
0.190283 0.594451
```
"""
function simulate_copula(t::Int, copula::Gaussian_cop; rng::AbstractRNG = Random.GLOBAL_RNG)
function simulate_copula(t::Int, copula::Gaussian_cop{T}; rng::AbstractRNG = Random.GLOBAL_RNG) where T <: Real
Σ = copula.Σ
z = transpose(rand(rng, MvNormal(Σ),t))
for i in 1:size(Σ, 1)
Expand Down Expand Up @@ -254,7 +254,7 @@ function simulate_copula!(U::Matrix{T}, copula::Frechet_cop{T}; rng::AbstractRNG
if> 0) & (n == 2)
for j in 1:size(U,1)
u_el = rand(rng, T, n)
frechet_el2!(u_el, α, β, rand(rng))
frechet_el2!(u_el, α, β, rand(rng, T))
U[j,:] = u_el
end
else
Expand Down
8 changes: 4 additions & 4 deletions src/nestedarchcopulagendat.jl
Expand Up @@ -349,7 +349,7 @@ julia> simulate_copula(4, cp)
function simulate_copula(t::Int, copula::Nested_AMH_cop{T}; rng::AbstractRNG = Random.GLOBAL_RNG) where T <: Real
n = [ch.n for ch in copula.children]
n2 = sum(n)+copula.m
U = zeros(t, n2)
U = zeros(T, t, n2)
simulate_copula!(U, copula; rng = rng)
return U
end
Expand Down Expand Up @@ -728,7 +728,7 @@ function simulate_copula!(U::Matrix{T}, copula::Double_Nested_Gumbel_cop{T}; rng
size(U, 2) == dims || throw(AssertionError("n.o. margins in pre allocated output and copula not equal"))

for j in 1:size(U,1)
X = Real[]
X = T[]
for k in 1:length(v)
n = ns[k]
n1 = vcat([collect(1:n[1])], [collect(cumsum(n)[i]+1:cumsum(n)[i+1]) for i in 1:length(n)-1])
Expand Down Expand Up @@ -810,8 +810,8 @@ julia> simulate_copula(3, c)
0.73617 0.347349 0.168348 0.410963
```
"""
function simulate_copula(t::Int, copula::Hierarchical_Gumbel_cop; rng::AbstractRNG = Random.GLOBAL_RNG)
U = zeros(t, copula.n)
function simulate_copula(t::Int, copula::Hierarchical_Gumbel_cop{T}; rng::AbstractRNG = Random.GLOBAL_RNG) where T <: Real
U = zeros(T, t, copula.n)
simulate_copula!(U, copula; rng = rng)
return U
end
Expand Down

0 comments on commit f7706f1

Please sign in to comment.