Tuesday, September 26, 2017

programming - 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.



Answer



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,'.')

No comments:

Post a Comment

technique - How credible is wikipedia?

I understand that this question relates more to wikipedia than it does writing but... If I was going to use wikipedia for a source for a res...