https://github.com/pymc-devs/pymc-examples/blob/main/examples/howto/copula-estimation.ipynb
(copula-estimation)=
:::{post} December 2023 :tags: copula, parameter estimation :category: intermediate :author: Eric Ma, Benjamin T. Vincent :::
When we deal with multiple variables (e.g. $a$ and $b$) we often want to describe the joint distribution $P(a, b)$ parametrically. If we are lucky, then this joint distribution might be 'simple' in some way. For example, it could be that $a$ and $b$ are statistically independent, in which case we can break down the joint distribution into $P(a, b) = P(a) P(b)$ and so we just need to find appropriate parametric descriptions for $P(a)$ and $P(b)$. Even if this is not appropriate, it may be that $P(a, b)$ could be described well by a simple multivariate distribution, such as a multivariate normal distribution for example.
However, very often when we deal with real datasets, there is complex correlational structure in $P(a, b)$ meaning that these two previous approaches are not available to us. So alternative methods are required.
This is where copulas come in. These allow you do describe a complex distribution $P(a, b)$ with correlational structure by a simple multivariate distribution (such as a Multivariate Gaussian), two marginal distributions, and some transformations. For a very accessible introduction to copulas, we recommend reading through this blog post by Thomas Wiecki._
This notebook covers how we can describe a distribution $P(a, b)$ with correlational structure using Bayesian methods to infer the parameters of the copula. The general approach we will take is shown in the schematic below.
:::{figure-md} copula-fig-target

Schematic of a 2D Gaussian copula. Our complex distribution P(a, b) in observation space (bottom) is modelled as being generated by a 2D Gaussian copula in multivariate normal space (top). Mapping from multivariate normal space to observation space (downwards) is done by the normal cumulative density function and then the inverse cumulative density function of the marginal distributions. The reverse, inference, process (upwards) can be done through the cumulative density function of the marginal distributions followed by an inverse cumulative density function of the normal distribution. :::
This notebook will describe how to make inferences about copulas based on bivariate data with rich correlational structure. We at PyMC Labs completed this work as part of a larger project with the Gates Foundation, some of which has also been outlined {ref}here <binning>.
[Image blocked: No description]
Before diving in to inference, we will spend some time fleshing out the steps in the schematic figure above. First, we will demonstrate the generative model by describing a multivariate normal copula and transform that into observation space. Second, we show how the inverse transformations can allow use to move back from observation space to multivariate normal space. Once we have these details pinned down, we proceed to the inference process in a later section.
Now we will define the properties of our Gaussian copula with a nested dictionary. At the top level we have keys a and b and rho.
rho describes the correlation coefficient of the multivariate normal copula.a and b are also dictionaries, each of which contains the marginal distribution (as a scipy distribution object) and their parameters.Note that we implicitly define the multivariate normal to have zero mean and unit variance. This is because these moments do not survive the transformation through 'uniform space', the second step in our copula schematic above.
First, we define the true multivariate normal and draw some samples from it.
Our first transformation (normal cdf) transforms data from multivariate normal space into uniform space. Note how the marginal distributions are uniform, but the correlational structure from the multivariate normal space remains in the interesting joint density below.
Our final transformation (the inverse CDF of the marginal distributions) gives rise to $a$ and $b$ in observation space.
To understand the approach taken, we will walk through the inverse process, going from observation space to multivariate normal space.
So now we have worked through what we outlined in Figure 1. We have stepped through in detail the data generating process going from multivariate normal to observation space. We then saw how to do the inverse (inference) process going from observation space to multivariate normal space. This is the approach we use in the PyMC model.
We will conduct inferences about parameters in multivariate normal space, constraining plausible parameter values by the data in observation space. However, we also use our observations of $a$ and $b$ to constrain estimates of the parameters of the marginal distributions.
In our experimentation, we explored models which conducted simultaneous estimation of the parameters of the marginals, and the covariance parameter of the copula, but we found this unstable. The solution we used below was found to be more robust, and involves a 2-step process.
In the copula model below you can see that we set up a prior over the covariance parameter. The posterior distribution over this parameter is constrained by the data in multivariate normal space. But in order to do that we need to transform the observations [a, b] in observation space, to multivariate normal space, which we store in data.
On using point estimates: as you'll see in the code below we have opted to use point estimates from Step 1 rather than the full posterior from Step 1. This is a simplification that we opted for due to complexities in tensor shape handling when passing in posterior distributions as parameters to a distribution.
During notebook review, however, @OriolAbril (one of the maintainers of the PyMC Examples repository) correctly pointed out that exponentiating the logcdf of a data point evaluated under a distribution using point estimates will not necessarily return an value equal to the expectation of exponentiating the logcdf of a data point evaluated under many possible distributions (constructed from a full posterior). To ensure timely progress on the notebook, we have opted to show the code as-is, but also leave this note for both our future selves to update the notebook later while also providing an opportunity for future readers to contribute through modifying the example to address this point.
You can see that we have successfully recovered the covariance matrix of the multivariate normal copula which was used to generate the sample data.
In the section below, we will use this information in order to sample from our parametric description of $P(a, b)$.
Finally, we can do a visual check to see whether our inferences (red) match up with our original observed data (black).
We would like to acknowledge Jonathan Sedar, Junpeng Lao, and Oriol Abril for useful advice during the development of this notebook.
:::{include} ../page_footer.md :::