# Introduction

In this post, I’d like to discuss empirical copula density estimation from data. We will introduce the concept of copulas, the copula density estimation problem, and how to solve it. We will also detail an implementation of the proposed solution in Matlab. All code used in this post is taken from the Matlab copula toolbox I am currently developing.

# Copula Background

Copulas are functions which capture the dependence structure of joint distribution functions. They can also be seen as multivariate probability distribution functions with marginal distributions that are uniform. These equivalent descriptions of copulas can be expressed mathematically through Sklar’s theorem:

If we define , then Sklar’s theorem can be restated as

Equation [1] and [2] are equivalent and show that any joint distribution can be expressed with the joint distribution’s copula function and the marginal distributions . The perceptive reader may notice that there was an implication of the uniqueness between a joint distribution and its copula in the previous statement. To be precise, Sklar’s theorem guarantees that if the marginal distributions for all are continuous random variables, then is unique. In this post, we will assume that for all are continuous random variables.

# Problem Description

Although Sklar’s theorem, shown above in Equations [1] and [2], guarantees a unique copula for any joint distribution (provided the marginal distributions are continuous random variables), it does not describe how to find this unique copula. In fact, copula model selection (i.e. which copula to use) is a significant area of research. In general, there are two solutions to copula model selection:

- Choose a copula among existing copula families such as
**Gaussian**,**t**, or**Archimedean**copulas which fit the data well, using some metric to decide between the copula families. - Calculating an empirical copula.

The advantages and disadvantages of each approach is out of the scope of this post. Rather, we will focus on the second approach, which is calculating an empirical copula, and more specifically, an empirical copula density. This can be considered as a nonparametric approach.

The empirical copula can be thought of as an estimate of the underlying dependency structure of the data, unconstrained by any a-priori models of how the data interacts with each other. In this light, it is similar to a kernel density estimate because the copula function (and copula density function) can be thought of as a distribution function (and density function) itself.

# The Empirical Copula Density Estimation Problem

Before describing the copula density estimation problem, it is useful to summarize the problem statement mathematically. Suppose we have a multivariate dataset in an matrix, where each column is a “feature,” and there are features and sample points per feature. We would like to estimate the copula density which captures the dependency between the columns in the dataset . How would we go about approaching this problem?

# The Solution

There are many solutions to the problem posed above. We discuss two approaches here, and outline why the naive approach may not be a good idea for estimating copula densities.

## The Naive Approach

The naive approach entails calculating the empirical copula function and then performing differentiation:

where Equation [4] is an expression for the pseudo copula observations. While this is tempting, discontinuities in data can lead to differentiability issues. Additionally, even if discontinuities are not present in the data, the copula density derived by numerical differentiation of the empirical copula function will be spiky. The spiky nature of the numerical derivative is simply a consequence of the algorithm that is used for differentiation. We will see an example of the effect of numerical differentiation on an empirical copula below.

Of course, more sophisticated techniques such as second order approximations can be implemented and they may yield acceptable solutions. However, there exists a more natural approach to estimating the copula density from data directly.

## Direct Density Estimation

An alternative to the approach described above is to estimate the density directly from data using kernel methods. For copula density estimation, it is preferred to use the Beta kernel, because both the Beta kernel and the copula have a bounded support on the interval , which eliminates boundary bias. Additionally, the Beta kernel can be viewed as an adaptive kernel density estimation approach, because it changes shape with respect to the input, even if the kernel density estimation bandwidth, , remains the same. For more details, please see References 1 and 2 below.

The formula then for multivariate copula density estimation is:

where is the kernel bandwidth parameter, is the probability density function of the Beta distribution, and is the pseudo observation in dimension . Equation [5] is nothing more than a multivariate kernel density estimate of the data, using beta kernels. However, it has the advantages stated above: 1.) eliminates boundary bias, and 2.) adaptive approach to density estimation, and 3.) it is shown in the references below that the variance of the estimator decreases with increased sample size.

# Implementation

Any copula operations depend upon rather than . The ‘s are termed pseudo observations, and there are two ways of estimating them. We discuss the pros and cons of different ways to estimate the pseudo observations before diving into the details of estimating the copula function and the copula density directly.

## Generating Pseudo Observations

There are atleast two ways to generate pseudo observations:

The Matlab function pseudoobs has the ability to generate pseudo observations using both methods described above. Briefly, the rank based method (option 1 above) can be implemented as:

U = tiedrank(X)/(M+1); % scale by M+1 to mitigate boundary errors

In the code above, each column is ranked and scaled to achieve a uniform distribution from the observed data. To implement the empirical CDF approach (option 2 above), we do the following

U = zeros(size(X)); for nn=1:N domain = linspace(min(X(:,nn)),max(X(:,nn)),numEcdfPts); FX = ksdensity(X(:,nn), domain, 'function', 'cdf')'; empInfoObj = rvEmpiricalInfo(domain, [], FX); for mm=1:M U(mm,nn) = empInfoObj.queryDistribution(X(mm,nn)); end end

It can be seen that the ECDF method requires estimation of each marginal empirical density function, and then querying that distribution for each of the samples. From a computational perspective, the rank approach is faster due to vectorization and modern sorting algorithms being lightning quick!

Both are valid approaches to generating pseudo observations, but if we do not know the marginal distributions a-priori (which is usually the case), the rank based approach actually produces a that is more uniformly distributed than the empirical CDF approach! The rank based approach is able to do this because it evenly distributes the original observations into the unit cube (by virtue of the rank function). To show this in Matlab, we can generate samples from a joint distribution and compute the pseudo observations using both methods, and compare the distributions of the computed pseudo observations. To generate samples from a joint distribution, we can utilize the copularnd function as follows:

```
M = 1000;
alpha = 5;
u = copularnd('Frank', alpha, M);
mu = 2; a= 3; b = 2;
x = [expinv(u(:,1),mu), gaminv(u(:,2),a,b];
```

After generating the dependent random variables, we compute the pseudo observations using the pseudoobs function with both the rank algorithm and the ECDF algorithm as follows:

numECDFPts = 100; U_ecdf = pseudoobs(x, 'ecdf', numECDFPts); U_rank = pseudoobs(x); % default option for pseudoobs is rank

Finally, we compute an empirical density via a histogram for the two different pseudo observations. The results are shown below.

A consequence of “more” uniformity is that the rank based calculation of the pseudo observations actually produces lower variance estimates of an empirical copula estimated with rank based pseudo observations. For intuition on this, please refer to Reference [2]. We empirically show this with a simulation where we generate samples from a joint distribution, and perform a Monte-Carlo simulation where we compare the variance of the estimated value of the original copula density from pseudo observations computed with the ECDF and the rank method. We only demonstrate this for a sample point in the unit cube, but the results apply to the entire space of . The results are show below:

It is clear from the figure that the variance is lower when using pseudo observations from the rank based estimator. The proof and intuition of using the rank based algorithm for generating pseudo observations is given in Reference [2], and the full simulation to generate the figures above is located here.

## Estimation via Empirical Copula Function and Gradients

Having discussed how to generate pseudo observations, let us tackle the problem at hand of estimating the empirical copula density. We first calculate the empirical copula by

where are the the pseudo observations. The function empcopulacdf implements Equation [6], and is included in the copula Matlab toolbox. Using this, the pseudoobs function, and gradient, we can easily estimate a copula density from samples as follows:

K = 25; U = copularnd('Frank', 5, 1000); C_est = empcopulacdf(U, K, 'deheuvels'); [cc] = gradient(C_est); [~,c_est_grad] = gradient(cc);

## Direct Empirical Copula Density Estimation

To estimate the density directly, we use the empcopulapdf function which implements Equation [5] above. This function has the same call signature as empcopulacdf, and using the same data source as above, we can generate a copula density estimate as follows:

```
h = 0.05;
c_direct = empcopulapdf(U, h, K, 'betak');
```

## Comparison

Let us now compare these two methods of density estimation by plotting the results, against the true copula density for the Frank copula with a dependency parameter of 5. The figure below shows three plots of the Frank copula. The plot on the left represents the true copula density, the plot in the middle shows the density derived from estimating the empirical copula first and performing directional derivatives, and finally the plot on the right shows the empirical copula density generated from the outlined Beta kernel approach.

The source for generating these diagrams is located in test_empcopulacdf.m.

# Conclusion

In this post, we have discussed two different methods for estimating a copula density directly from data. From the limited experiments conducted above, two recommendations can be given:

- Use a rank based approach to generating pseudo observations.
- If the empirical copula density is what is required, estimate it directly from data using the Beta kernel approach outlined above, as opposed to computing the empirical copula function and performing directional derivatives.

In the next post, we’ll discuss the mathematics behind generating random samples from an empirical copula, using the empcopularnd function.