-
Notifications
You must be signed in to change notification settings - Fork 9
/
TCopula.jl
57 lines (47 loc) · 1.82 KB
/
TCopula.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"""
TCopula{d,MT}
Fields:
- df::Int - number of degree of freedom
- Σ::MT - covariance matrix
Constructor
TCopula(df,Σ)
The Student's [T Copula](https://en.wikipedia.org/wiki/Multivariate_t-distribution#Copulas_based_on_the_multivariate_t) is the
copula of a [Multivariate Student distribution](https://en.wikipedia.org/wiki/Multivariate_t-distribution). It is constructed as :
```math
C(\\mathbf{x}; \\boldsymbol{n,\\Sigma}) = F_{n,\\Sigma}(F_{n,\\Sigma,i}^{-1}(x_i),i\\in 1,...d)
```
where ``F_{n,\\Sigma}`` is a cdf of a multivariate student random vector with covariance matrix ``\\Sigma`` and ``n`` degrees of freedom. and `F_{n,\\Sigma,i}` is the ith marignal cdf.
It can be constructed in Julia via:
```julia
C = TCopula(2,Σ)
```
You can sample it, compute pdf and cdf, or even fit the distribution via:
```julia
u = rand(C,1000)
Random.rand!(C,u) # other calling syntax for rng.
pdf(C,u) # to get the density
cdf(C,u) # to get the distribution function
Ĉ = fit(TCopula,u) # to fit on the sampled data.
```
Except that currently it does not work since `fit(Distributions.MvTDist,data)` does not dispatch.
References:
* [nelsen2006](@cite) Nelsen, Roger B. An introduction to copulas. Springer, 2006.
"""
struct TCopula{d,df,MT} <: EllipticalCopula{d,MT}
Σ::MT
function TCopula(df,Σ)
make_cor!(Σ)
N(TCopula{size(Σ,1),df,typeof(Σ)})(Σ)
return new{size(Σ,1),df,typeof(Σ)}(Σ)
end
end
U(::Type{TCopula{d,df,MT}}) where {d,df,MT} = Distributions.TDist(df)
N(::Type{TCopula{d,df,MT}}) where {d,df,MT} = function(Σ)
Distributions.MvTDist(df,Σ)
end
function Distributions.fit(::Type{CT},u) where {CT<:TCopula}
N = Distributions.fit(N(CT), quantile.(U(CT),u))
Σ = N.Σ
df = N.df
return TCopula(df,Σ)
end