# How to sample from a copula in matlab

I have two random variables (say, X and Y). Each of these rv's are defined by their CDFs (CDF_X and CDF_Y). These CDFs were obtained empirically, so they are a "stair" graph. I also have a copula C representing the relation between X and Y. This copula was obtained through a kernel estimator.

I want to sample (say 10 points (X,Y)) from the bivariate distribution of X and Y (that is, respecting the dependence relation imposed by C).

How can I do such implementation in Matlab or in R? I prefer Matlab.

• You need to derive a conditional copula, which you can find by taking the derivative of your copula to one of the variables. This will allow you to first sample a uniform random value, and then conditional on that one, a second uniform one from the conditional copula. Then you transform them with the inverse CDF's so that they have the appropriate marginal distributions. – Raskolnikov Feb 9 '18 at 19:30

Suppose you have the copula $C(u_1,u_2)$, then you could compute the conditional copula

$$c_{u_1}(u_2)=\frac{\partial C(u_1,u_2)}{\partial u_1} \; .$$

Now, you can generate a pair of independent uniformly distributed random values $(U,V)$. Let's say a particular realistation is $(u,v)$. Then the pair

$$(u,c_u^{-1}(v))$$

will be distributed according to the copula. You only need to apply the inverse CDF's to get them distributed like $(X,Y)$. Say $X\sim F_X$ and $Y\sim F_Y$, then

$$(F_X^{-1}(u),F_Y^{-1}(c_u^{-1}(v)))$$

is what you need to do in the end.

An example in Matlab for a Clayton copula

%% Simulations of Clayton copulas using conditional cdf

%Example for theta=4
n=3000;
theta=5;
u=rand(1,n);
y=rand(1,n);
v=((y.^(1/(1+theta)).*u).^(-theta)+1-u.^(-theta)).^(-1/theta);

x1=norminv(u);
x2=norminv(v);

plot(x1,x2,'.')

• thank you very much. This will surely help. But ideally I need a matlab implementation... – Pierre Feb 10 '18 at 16:56
• Maybe you should give the format of your data then. Can you update your question and add the Matlab code you already have, as well as the structure of your data? I added an example for a Clayton copula and standard normal marginal cdfs. – Raskolnikov Feb 10 '18 at 17:14
• If your copula results from a kernel estimator, then presumably the kernel is some known smooth function and computing the partial derivatives should not be hard. Then the conditional copula will just be a linear combination involving the partial derivatives of the kernel. – Raskolnikov Feb 10 '18 at 17:28
• thank you. But Matlab does not give the analytical formula when I use the ksdensity function (it gives the values of the kernel function at a finite number of points). Is there a way to get the analytical formula of the kernel in matlab, so that I can compute its derivative? – Pierre Feb 15 '18 at 17:22
• Have you looked in the documentation of the function? – Raskolnikov Feb 15 '18 at 17:45