TLDR: I am very pleased to announce my first registered package: Copulas.jl
!
What is it
This package aims at bringing into native Julia most of the standard copula features: random number generation, fitting, construction of copula-based multivariate distributions through Sklar’s theorem, etc. while fully complying with the Distributions.jl
API (after all, copulas are distributions functions).
Usually, people that use and work with copulas turn to R, because of the amazing R
package copula
.
While still perfectly maintained and updated today, the R
package copula
is full of obscured, heavily optimized, fast C
code on one hand, and obscure, heavily optimized slow R
code on the other hand (pun intended ).
This is an attempt to provide a very light, fast, reliable and maintainable copula implementation in native Julia (in particular, type-agnostic, so it’ll work with arbitrary type of floats like Float32
for speed, BigFloats
or DoubleFloats
or MultiFloats
for precision), with correct SIMD’sation, etc.
Two of the exported types are of most importance:
Copula
: this is an abstract mother type for all our copulas.SklarDist
: Allows to construct a multivariate distribution by specifying the copula and the marginals, through Sklar’s theorem.
What is already implemented
The API we implemented contains random number generations, cdf and pdf evaluations, and the fit
function from Distributions.jl
. Typical use case might look like this:
using Copulas, Distributions, Random
X₁ = Gamma(2,3)
X₂ = Pareto()
X₃ = LogNormal(0,1)
C = FranckCopula(3,7) # A 3-variate Franck Copula with θ = 7
D = SklarDist(C,(X₁,X₂,X₃)) # The final distribution
# This generates a (3,1000)-sized dataset from the multivariate distribution D
simu = rand(D,1000)
# While the following estimates the parameters of the model from a dataset :
D̂ = fit(SklarDist{ClaytonCopula,Tuple{Gamma,Normal,LogNormal}}, simu)
# Increase the number of observations to get a beter fit (well, might not work. can you spot why ?)
Atop from the very neat SklarDist
type, available copulas are :
EmpiricalCopula
GaussianCopula
TCopula
ArchimedeanCopula
(general, for any generator)ClaytonCopula
(as an instantiation ofArchimedeanCopula
, see after)
Next ones to be implemented will probably be :
- A few more archimedeans :
FranckCopula
,AMHCopula
,JoeCopula
,GumbelCopula
- Nested archimedeans (general, with the possibility to nest any family with any family, assuming it is possible, with parameter checks.)
- Bernstein copula and more general Beta copula as smoothing of the Empirical copula.
CheckerboardCopula
(and more generallyPatchworkCopula
)
Adding a new ArchimedeanCopula
is very easy. The Clayton
implementation is as short as :
struct ClaytonCopula{d,T} <: ArchimedeanCopula{d}
θ::T
end
ClaytonCopula(d,θ) = ClaytonCopula{d,typeof(θ)}(θ) # Constructor
radial_dist(C::ClaytonCopula) = Distributions.Gamma(1/C.θ,1) # Radial distribution
ϕ(C::ClaytonCopula, t) = (1+sign(C.θ)*t)^(-1/C.θ) # Generator
ϕ⁻¹(C::ClaytonCopula, t) = sign(C.θ)*(t^(-C.θ)-1) # Inverse Generator
τ(C::ClaytonCopula) = C.θ/(C.θ+2) # θ -> τ
τ⁻¹(::Type{ClaytonCopula},τ) = 2τ/(1-τ) # τ -> θ
The Archimedean API is modular :
- To sample an archimedean, only
radial_dist
andϕ
are needed. - To evaluate the cdf, only
ϕ
andϕ⁻¹
are needed - Currently to fit the copula,
τ⁻¹
is needed as we use inverted tau moment method. But we plan on also implementing inverse rho and MLE (density needed).
Both FranckCopula
, AMHCopula
, JoeCopula
, GumbelCopula
and many others are waiting for you to implement these methods !
Dev roadmap
With only a few dozens of lines of code, in a few hours, I reproduced a large part of the R copula
package, which used thousands of lines of code for the same functionality…
However, the release still lacks documentation and tests, which are on top of my todo-list. Nested archimedean copulas with very complient nesting (no more restriction to only nest from the same family, as in R) is my next goal. Later, I’d like Bernstein & Beta copulas, Checkerboards & Patchworks, etc… The rest of the todo-list can be found in the package’s README.
I hope you’ll enjoy the package !