Using the NAG Toolbox for MATLAB Part 2 - The G05 Chapter - Creating a Gaussian copula

The G05 Chapter - Creating a Gaussian copula

In this final section, we use the NAG Toolbox in an application that is somewhat more complicated. In statistics, a copula can be thought of as defining the correlation structure for a family of multivariate distributions. Each distribution in the family is constructed by "gluing" together two or more univariate distributions, with the copula supplying the "glue". Copulas can be applied to a wide range of simulation problems - for example, the so-called Gaussian copula is often used in financial modelling.

To illustrate, suppose we wish to simulate the joint distribution of several random variables. The kind of copula we use - Gaussian, Student's t, etc - describes the correlation structure between the variables, while a collection of univariate distributions (usually called the marginal distributions) define the distribution within each of the variables. Compared to more conventional multivariate distributions (such as, for example, the multivariate normal), the use of a copula allows each variable to have a different marginal distribution. Thus, for example, one variable may have a Gaussian (normal) marginal distribution, while another has a Beta distribution, a third is uniformly distributed, and so on.

In this example, we construct a bivariate distribution using a Gaussian copula with Beta marginal distributions. The input for the calculation of the multivariate distribution includes its covariance matrix, which contains details of the correlations between the different variables. The copula is created using NAG routine g05ra . The shape of the Beta distribution is defined by two parameters (usually called alpha and beta), and can range from the symmetric bell curve of the normal distribution (when alpha and beta are both 1) to being highly skewed or, at the other extreme, a uniform distribution. Here, we set alpha = 12.0 and beta = 5.0 for the first variable - which gives a skewed distribution - and alpha = beta = 5.0 for the second variable, which is distributed symmetrically. Here's the code:

   
% Prepare to call the NAG routine g05ra, which generates an array of pseudo-
% random variates from a Gaussian copula. First, initialize some parameters
% required by the routine.
mode = int32(0);
igen = int32(1);
iseed = [int32(1); int32(2); int32(3); int32(4)];
r = zeros(7, 1);

% n is the number of values (actually value pairs) that will be generated.
n = int32(10000);

% Next, set the correlation coefficient for the two variables, and use it
% to construct the (upper triangle of the) covariance matrix of the
% distribution.
corr = 0.8;
c = [1.0, corrl;
0.0, 1.0;];

% Call g05ra to generate the array of variates.
[x, iseedOut, rOut, ifail] = g05ra(mode, c, n, igen, iseed, r);

% Now set the parameters for the two marginal distributions...
alpha1 = 12.0;
beta1 = 5.0;
alpha2 = 5.0;
beta2 = 5.0;

% ...and call g01fe to compute the deviates from the Beta distribution.
tol = 1.0;
for ii = 1 : n
[x(ii,1)] = g01fe(x(ii,1), alpha1, beta1, tol);
[x(ii,2)] = g01fe(x(ii,2), alpha2, beta2, tol);
end

The resultant distribution is then converted into a two-dimensional array of frequencies and displayed in MATLAB as a surface (though other representations - such as a contour plot - are also possible).


Figure 5: Creating a Gaussian copula.

It can be instructive to look at the shape of the copula for different marginal distributions, or for other correlations - for example, increasing the correlation coefficient causes the peak to become narrower. Furthermore, it's reasonably straightforward in MATLAB to construct an animation, based upon a loop over parameter values, which tracks the progress of the peak across the two-dimensional domain.

The MATLAB script for this demo is available as the file NAGToolboxDemos/Random_number_generation/g05ra_demo.m, distributed in this archive.