Drawing from correlated marginal uniform in Matlab: Alternatives to copularnd?

匆匆过客 提交于 2019-12-11 13:36:03

问题


Consider two random variables X and Y both uniformly distributed in [0,1] and correlated with correlation rho.

I want to draw P realisations from their joint distribution in Matlab.

Is

A = copularnd('gaussian',rho,P);

the only way to do it?


回答1:


Copulas provide a very convenient way modelling the joint distribution. Just saying that you want X~U[0,1], Y~U[0,1] and corr(X,Y)=rho is not enough to define the relationship between these two random variables. Simply by substituting different copulas you can build different models (eventually choosing the one suiting your use-case best) that all satisfy this condition.

Apart from understanding the basics of what copulas are, you need to understand there are different types of correlations, such as linear (Pearson) and rank (Spearman / Kendall), and how they relate to each other. In particular, rank correlations preserve when a monotonic transformation is applied, unlike linear correlation. This is the key property allowing us to easily translate the desired linear correlation of uniform marginal distributions to the linear correlation of bivariate normal (or other type of distribution you use in the copula), which would be the input correlation to copularnd.

There is a very good answer on Cross Validated, describing exactly how to convert the correlation in your case. You should also read this Matlab guide on Using Rank Correlation Coefficients and copulas.

In a nutshell, to model desired linear correlation of marginals, you need to translate it into some rank correlation (using Spearman is convenient, since for uniform distribution it equals Pearason correlation), which would be the same for your normals (because it's a monotonic transformation). All you need is to convert that Spearman correlation for your normal to a linear Person correlation, which would be the input to copularnd.

Hopefully the code would make it easy to understand. Obviously you don't need those temporary variables, but it should make the logic clear:

rho_uni_Pearson = 0.7;
rho_uni_Spearman = rho_uni_Pearson;
rho_normal_Spearman = rho_uni_Spearman;
rho_normal_Pearson = 2*sin(rho_normal_Spearman*pi/6);

X = copularnd('gaussian', rho_normal_Pearson, 1e7);

And the resulting linear correlation is exactly what we wanted (since we've generated a very large sample):

corr(X(:,1), X(:,2));
ans =
    0.7000

Note that for a bivariate normal copula, the relationship between linear and rank correlations is easy enough, but it could be more complex if you use a different copula. Matlab has a function copulaparam that allows you to translate from rank to linear correlation. So instead of writing out explicit formula as above, we could just use:

rho_normal_Pearson = copulaparam('Gaussian', rho_normal_Spearman, 'type', 'spearman')

Now that we have learned the basics, let's go ahead and use a t copula with 5 degress of freedom instead of gaussian copula:

nu = 5; % number of degrees of freedom of t distribution
rho_t_Pearson = copulaparam('t', 0.7, nu, 'type', 'spearman');

X = copularnd('t', rho_t_Pearson, nu, 1e7);

Ensuring resulting linear correlation is what we wanted:

corr(X(:,1), X(:,2));
ans =
    0.6996

It's easy to observe that resulting distributions may be strikingly different, depending on which copula you choose, even through they all give you the same linear correlation. It is up to you, the researcher, to determine which model is best suited to your particular data and problem. For example:

N = 500;
rho_Pearson = copulaparam('gaussian', 0.1, 'type', 'spearman');
X1 = copularnd('gaussian', rho_Pearson, N);
figure(); scatterhist(X1(:,1),X1(:,2)); title('gaussian');

nu = 1; % number of degrees of freedom of t distribution
rho_Pearson = copulaparam('t', 0.1, nu, 'type', 'spearman');
X2 = copularnd('t', rho_Pearson, nu, N);
figure(); scatterhist(X2(:,1),X2(:,2)); title('t, nu=1');



来源:https://stackoverflow.com/questions/37511147/drawing-from-correlated-marginal-uniform-in-matlab-alternatives-to-copularnd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!