Skip to content

Pair copula basics

Program: example/ex01_pair_copula.f90, example/python/ex01_pair_copula.py, example/r/ex01_pair_copula.R  ยท  Topics: fv_bicop, density, inverse h-function, simulation, maximum likelihood, BIC family selection.

The program works with one bivariate copula from start to finish: it evaluates a Clayton copula, draws a sample from it, refits the parameter, and finally lets fortvine choose the family without being told.

Code

! ex01_pair_copula -- one bivariate copula: evaluate, simulate, fit a known family, select a family.
program ex01_pair_copula
  use fortvine_api
  implicit none
  integer :: ns
  integer, allocatable :: seed(:)
  integer, parameter :: dp = kind(1.0d0), n = 2000
  type(fv_bicop) :: truth, fitted, chosen
  real(dp) :: u1(n), w(n), u2(n), dens(3)

  ! a Clayton copula with theta = 2 (Kendall's tau = theta / (theta + 2) = 0.5)
  truth%family = FV_CLAYTON; truth%par = [2.0_dp, 0.0_dp, 0.0_dp]

  ! density at three points
  call truth%pdf([0.1_dp, 0.5_dp, 0.9_dp], [0.1_dp, 0.5_dp, 0.9_dp], dens)
  print '(a,3f10.4)', 'Clayton(2) density on the diagonal:', dens

  ! simulate by inverse Rosenblatt: u2 = h^-1(w | u1) with independent uniforms u1, w
  call random_seed(size=ns); allocate(seed(ns)); seed = 20261116; call random_seed(put=seed)   ! reproducible sample
  call random_number(u1); call random_number(w)
  call truth%hinv1(u1, w, u2)
  print '(a,f7.3)', 'empirical Kendall tau:', fv_kendall_tau(u1, u2)

  ! maximum likelihood fit of a given family
  fitted%family = FV_CLAYTON
  call fitted%fit(u1, u2)
  print '(a,f8.4,a,f10.2)', 'fitted theta:', fitted%par(1), '   log-likelihood:', fitted%loglik

  ! family selection by BIC over the default family set (with rotations)
  call chosen%select(u1, u2)
  print '(a,a,a,i0,a,3f8.3)', 'selected: ', trim(fv_fam_name(chosen%family)), '  rotation ', chosen%rotation, &
    '  par', chosen%par
end program ex01_pair_copula
"""ex01_pair_copula -- one bivariate copula: evaluate, simulate, fit a known family, select a family.

The Python version of example/ex01_pair_copula.f90 (pip install ./python).
"""
import numpy as np

import fortvine as fv

n = 2000

# a Clayton copula with theta = 2 (Kendall's tau = theta / (theta + 2) = 0.5)
truth = fv.Bicop("clayton", 0, [2.0])

# density at three points
g = np.array([0.1, 0.5, 0.9])
dens = truth.pdf(np.column_stack([g, g]))
print("Clayton(2) density on the diagonal:" + "".join(f"{x:10.4f}" for x in dens))

# simulate by inverse Rosenblatt: u2 = h^-1(w | u1) with independent uniforms u1, w
rng = np.random.default_rng(20261116)                # reproducible sample
u1, w = rng.random(n), rng.random(n)
u2 = truth.hinv1(np.column_stack([u1, w]))
u = np.column_stack([u1, u2])
print(f"empirical Kendall tau:{fv.kendall_tau(u1, u2):7.3f}")

# maximum likelihood fit of a given family
fitted = fv.Bicop("clayton").fit(u)
print(f"fitted theta:{fitted.parameters[0]:8.4f}   log-likelihood:{fitted.loglik():10.2f}")

# family selection by BIC over the default family set (with rotations)
chosen = fv.Bicop().select(u)
par = np.zeros(3); par[:chosen.npars] = chosen.parameters
print(f"selected: {chosen.family}  rotation {chosen.rotation}  par" + "".join(f"{p:8.3f}" for p in par))
# ex01_pair_copula -- one bivariate copula: evaluate, simulate, fit a known family, select a family.
# The R version of example/ex01_pair_copula.f90 (R CMD INSTALL r).
library(fortvine)
n <- 2000

# a Clayton copula with theta = 2 (Kendall's tau = theta / (theta + 2) = 0.5)
truth <- bicop_dist("clayton", 0, 2)

# density at three points
g <- c(0.1, 0.5, 0.9)
dens <- dbicop(cbind(g, g), truth)
cat("Clayton(2) density on the diagonal:", sprintf("%10.4f", dens), "\n", sep = "")

# simulate by inverse Rosenblatt: u2 = h^-1(w | u1) with independent uniforms u1, w
set.seed(20261116)                                   # reproducible sample
u1 <- runif(n); w <- runif(n)
u2 <- hbicop(cbind(u1, w), 1, truth, inverse = TRUE)
u <- cbind(u1, u2)
cat(sprintf("empirical Kendall tau:%7.3f\n", cor(u1, u2, method = "kendall")))

# maximum likelihood fit of a given family
fitted <- bicop(u, family_set = "clayton", rotation = 0)
cat(sprintf("fitted theta:%8.4f   log-likelihood:%10.2f\n", fitted$parameters, fitted$loglik))

# family selection by BIC over the default family set (with rotations)
chosen <- bicop(u, family_set = "all", selcrit = "bic")
par <- c(chosen$parameters, 0, 0, 0)[1:3]
cat("selected: ", chosen$family, "  rotation ", chosen$rotation, "  par",
    sprintf("%8.3f", par), "\n", sep = "")

Output

Clayton(2) density on the diagonal:    5.3702    1.4810    2.1578
empirical Kendall tau:  0.514
fitted theta:  2.0422   log-likelihood:    892.73
selected: clayton  rotation 0  par   2.042   0.000   0.000
Clayton(2) density on the diagonal:    5.3702    1.4810    2.1578
empirical Kendall tau:  0.517
fitted theta:  2.0239   log-likelihood:    915.66
selected: clayton  rotation 0  par   2.024   0.000   0.000
Clayton(2) density on the diagonal:    5.3702    1.4810    2.1578
empirical Kendall tau:  0.502
fitted theta:  1.9733   log-likelihood:    824.55
selected: clayton  rotation 0  par   1.973   0.000   0.000

The first line is deterministic and the same in all three languages. The other three depend on the random sample. Each program fixes its seed (random_seed(put=seed) in Fortran, np.random.default_rng(seed) in Python, set.seed() in R), so its output is reproducible, but the three generators produce different uniforms and so slightly different values around the same truth. Another Fortran compiler has another intrinsic generator as well. The numbers in the walk-through are those of the Fortran run.

Walk-through

Defining a copula

truth%family = FV_CLAYTON; truth%par = [2.0_dp, 0.0_dp, 0.0_dp]
truth = fv.Bicop("clayton", 0, [2.0])        # family, rotation, parameters
truth <- bicop_dist("clayton", 0, 2)         # family, rotation, parameters

An fv_bicop is a plain derived type: family is an integer code (named constants FV_GAUSSIAN, FV_CLAYTON, ... in fortvine_api), rotation is 0, 90, 180 or 270, and par(3) holds up to three parameters with unused slots left at 0. Nothing has to be initialised or allocated. In Python and R the family is given by its name (as fv_fam_name spells it, any case) or its integer code, and only as many parameters as the family has. The Clayton copula has the distribution function and density

\[ C(u, v) = \bigl(u^{-\theta} + v^{-\theta} - 1\bigr)^{-1/\theta}, \qquad c(u, v) = (1 + \theta)\,(uv)^{-1-\theta} \bigl(u^{-\theta} + v^{-\theta} - 1\bigr)^{-2 - 1/\theta}, \]

with Kendall's \(\tau = \theta / (\theta + 2)\), i.e. \(\tau = 0.5\) for \(\theta = 2\).

Evaluating the density

call truth%pdf([0.1_dp, 0.5_dp, 0.9_dp], [0.1_dp, 0.5_dp, 0.9_dp], dens)
dens = truth.pdf([[0.1, 0.1], [0.5, 0.5], [0.9, 0.9]])      # one point per row
dens <- dbicop(rbind(c(0.1, 0.1), c(0.5, 0.5), c(0.9, 0.9)), truth)   # one point per row

All evaluation routines take arrays of points (Fortran: two vectors; Python and R: an \(n \times 2\) array): here the three diagonal points \((0.1, 0.1)\), \((0.5, 0.5)\), \((0.9, 0.9)\). At the centre the formula gives \(3 \cdot 0.25^{-3} \cdot 7^{-5/2} = 1.4810\), as printed. The density is largest near the origin (5.37 at \((0.1, 0.1)\)) because Clayton has lower tail dependence, \(\lambda_L = 2^{-1/\theta} \approx 0.71\); at \((0.9, 0.9)\) it is smaller (2.16) since \(\lambda_U = 0\). The same calling pattern serves logpdf, cdf, hfunc1, hfunc2, hinv1 and hinv2 (in R: dbicop(u, b, log = TRUE), pbicop(u, b) and hbicop(u, 1 or 2, b, inverse =)). Long arrays (more than 4096 points) are evaluated in parallel.

Simulating

call random_number(u1); call random_number(w)
call truth%hinv1(u1, w, u2)
u1, w = rng.random(n), rng.random(n)
u2 = truth.hinv1(np.column_stack([u1, w]))
u = np.column_stack([u1, u2])                # the same in one call: truth.simulate(n, seed=...)
u1 <- runif(n); w <- runif(n)
u2 <- hbicop(cbind(u1, w), 1, truth, inverse = TRUE)
u <- cbind(u1, u2)                           # the same in one call: rbicop(n, truth)

hinv1(u1, w, u2) returns the \(v\) that solves \(h_1(v \mid u_1) = w\), where \(h_1(v \mid u) = \partial C(u, v)/\partial u\) is the conditional distribution of \(V\) given \(U = u\). If \(u_1\) and \(w\) are independent uniforms, \((u_1, u_2)\) is a draw from the copula: the inverse Rosenblatt transform in two dimensions. For Clayton, fortvine uses the closed form

\[ h_1^{-1}(w \mid u) = \Bigl(1 + u^{-\theta}\bigl(w^{-\theta/(1+\theta)} - 1\bigr)\Bigr)^{-1/\theta}, \]

written so that the factor \(w^{-\theta/(1+\theta)} - 1\) is computed with expm1 and nothing cancels for small \(u\).

h-function curves of the Clayton copula for three values of u, with inverse h-function points on them, and the corresponding conditional densities

The conditional distribution behind the simulation, for the Clayton copula with $\theta = 2$. (a) $h_1(v \mid u)$ for $u = 0.1, 0.5, 0.9$ (lines) and the points $\bigl(h_1^{-1}(w \mid u), w\bigr)$ returned by `hinv1` for $w = 0.05, 0.10, \ldots, 0.95$ (circles), which fall on the curves: over a 401-point grid $|h_1^{-1}(h_1(v \mid u) \mid u) - v| \le 3 \times 10^{-14}$. (b) The conditional density $c(u, v) = \partial h_1(v \mid u) / \partial v$: for small $u$ the mass of $V$ piles up near 0, the lower tail dependence of Clayton.

The empirical Kendall tau of the 2000 draws (0.514 with the fixed seed) scatters around the theoretical 0.5 with a standard error of roughly 0.01.

Fitting a known family

fitted%family = FV_CLAYTON
call fitted%fit(u1, u2)
fitted = fv.Bicop("clayton").fit(u)          # fitted.parameters, fitted.loglik()
fitted <- bicop(u, family_set = "clayton", rotation = 0)   # fitted$parameters, logLik(fitted)

fit keeps family and rotation and estimates par by maximum likelihood; the maximised log-likelihood goes to fitted%loglik. In R, bicop() fits a single family when family_set holds one family and rotation is given; without rotation it selects. Internally the start value comes from inverting Kendall's tau, and the likelihood is maximised by Fisher-scoring steps with exact parameter scores (see Exact parameter scores). Passing an fv_controls with method = FV_ITAU instead returns the tau-inversion estimate itself (method="itau" in Python, par_method = "itau" in R), and weights= gives observation weights (weights= in Python and R; example).

The 2000 simulated Clayton pairs in copula scale and in normal scores, with fitted and true density contours

The program's sample, $n = 2000$ draws from the Clayton copula with $\theta = 2$ ($\tau = 0.5$). (a) In copula scale the lower tail dependence shows as the dense cluster at $(0, 0)$. (b) In normal scores $z = \Phi^{-1}(u)$, with the contours of the fitted density ($\hat\theta = 2.042$, solid) and of the true density ($\theta = 2$, dashed): the two are indistinguishable at this sample size.

Selecting a family

call chosen%select(u1, u2)
chosen = fv.Bicop().select(u)                # chosen.family, chosen.rotation, chosen.parameters
chosen <- bicop(u)                           # family_set = "all", selcrit = "bic"

select with no controls tries the default candidate set: the 23 hand-coded parametric families 0..22 (every parametric family except the slow-to-fit t-EV). A rotatable family is tried in the two rotations that match the sign of the sample's Kendall tau (0 and 180 degrees for positive dependence, 90 and 270 for negative). With preselect on (the default), the correlations of the normal scores in the lower and the upper part of the sample are compared, as in vinecopulib, and a rotation whose tail sits on the weaker side is dropped before fitting. Every remaining candidate is fitted and the one with the smallest BIC, \(-2\ell + k \log n\), wins. The data came from Clayton, and Clayton without rotation is recovered with the same parameter as the dedicated fit.

Variations

Restrict or extend the candidates with a control object (keyword arguments in Python and R). See Controls for every field.

type(fv_controls) :: ctl
ctl%nfam = 5; ctl%family_set(1:5) = FV_ONE_PAR     ! gaussian, clayton, gumbel, frank, joe
ctl%selcrit = FV_AIC
call chosen%select(u1, u2, ctl)
chosen = fv.Bicop().select(u, family_set=["gaussian", "clayton", "gumbel", "frank", "joe"],
                           criterion="aic")
chosen <- bicop(u, family_set = c("gaussian", "clayton", "gumbel", "frank", "joe"),
                selcrit = "aic")

Evaluate a rotated copula: set the rotation to 90 before calling pdf/hinv1; a 90 or 270 degree rotation of Clayton models negative dependence with a tail in an off-diagonal corner (Rotations and asymmetry).

truth%rotation = 90
truth = fv.Bicop("clayton", 90, [2.0])
truth <- bicop_dist("clayton", 90, 2)

The distribution function \(C\) itself is in closed form for every family except the Gaussian and Student ones, which are integrated by Gauss-Legendre quadrature over their exact h-function.

call truth%cdf(u1, u2, out)
out = truth.cdf(u)
out <- pbicop(u, truth)