生成遵循β分布的相关随机变量

时间:2015-07-16 16:04:31

标签: sas correlation sas-iml

我需要为使用SAS的相关的两个beta-distributed变量生成随机值。感兴趣的两个变量的特征如下:

X1mean = 0.896variance = 0.001

X2mean = 0.206variance = 0.004

对于X1X2 p = 0.5,其中 p 是相关系数。

使用SAS,我了解如何使用函数X = RAND('BETA', a, b)生成指定beta分布的随机数,其中 a b 是两个形状参数对于可以根据均值和方差计算的变量 X 。但是,我想同时为X1X2生成值,同时指定它们在 p = 0.5时相关。

1 个答案:

答案 0 :(得分:1)

此解决方案基于Simulating Data with SAS Rick Wicklin第9章中使用的修改方法。

在这个特定的例子中,我首先必须定义与beta distribution相关联的变量均值,方差和形状参数(alpha,beta):

data beta_corr_vars;
    input x1 var1 x2 var2;  *mean1, variance1, mean2, variance2;
    *calculate shape parameters alpha and beta from means and variances;
    alpha1 = ((1 - x1) / var1 - 1/ x1) * x1**2;   
    alpha2 = ((1 - x2) / var2 - 1/ x2) * x2**2; 
    beta1 = alpha1 * (1 / x1 - 1);
    beta2 = alpha2 * (1 / x2 - 1);
    *here are the means and variances referred to in the original question;
    datalines; 
0.896 0.001 0.206 0.004
;
run;
proc print data = beta_corr_vars;
run;

定义这些变量后:

proc iml;
  use beta_corr_vars; read all; 
  call randseed(12345);
      N = 10000;                  *number of random variable sets to generate;
      *simulate bivariate normal data with a specified correlation (here, rho = 0.5);
      Z = RandNormal(N, {0, 0}, {1 0.5, 0.5 1});   *RandNormal(N, Mean, Cov);
      *transform the normal variates into uniform variates;
      U = cdf("Normal", Z);      

      *From here, we can obtain beta variates for each column of U by; 
      *applying the inverse beta CDF;
      x1_beta = quantile("Beta", U[,1], alpha1, beta1);        
      x2_beta = quantile("Beta", U[,2], alpha2, beta2); 
      X = x1_beta || x2_beta; 

  *check adequacy of rho values--they approach the desired values with more sims (N);
  rhoZ = corr(Z)[1,2];                
  rhoX = corr(X)[1,2];

print X;
print rhoZ rhoX;

感谢所有为此答案做出贡献的用户。