基于相同的种子在R中复制SAS(rancor)中生成的随机法线? [英] Replicating random normal generated in SAS (rancor) in R, based on the same seed?

查看:10
本文介绍了基于相同的种子在R中复制SAS(rancor)中生成的随机法线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定相同的种子,有没有办法在 R 中使用 rannor 函数在 SAS 中生成完全相同的随机正态数?

解决方案

要使这些匹配,您需要两件事:

  • 用于生成随机数的种子
  • 用于生成随机数的公式

SAS 用于 rannor(我认为也用于 rand,但我还没有看到对此的确认),以下算法(在 伪随机数:不统一,作者:Robert Johnson 和 Hui Liu):

rannor_result = (−2* log(U1))**.5*cos(2*constant('pi')*U2)

其中 U1 和 U2 是统一数字流中的两个数字.(也可以获得第二个数字,但据我所知,该数字已被丢弃.)

查看以下 SAS 数据步:

数据测试1;U1 = 兰尼(7);U2 = 兰尼(7);X1 = (−2* log(U1))**.5*cos(2*constant('pi')*U2);U1 = 兰尼(7);U2 = 兰尼(7);X2 = (−2* log(U1))**.5*cos(2*constant('pi')*U2);跑;数据测试2;x1 = 兰诺(7);x2 = 兰诺(7);跑;

test1test2 的随机数具有相同的值.

我怀疑 R 和 SAS 共享共同的 PRNG 算法,特别是当 rannor 使用的那个不是很好(你应该使用 rand 好得多,使用 Mersenne Twister).但是,您可以轻松地要求 SAS 输出它使用的种子 - 只要您输出 RANUNI 结果.

您可以要求 SAS 这样做:

数据 rands_uni;种子=7;做_i_=1到10;种子1 =种子;调用 ranuni(seed,x1);种子2=种子;调用 ranuni(seed,x2);输出;结尾;跑;

然后,您可以在 R 中计算 rannor 结果(即,从 x1 和 x2).我在其中包含了种子以供参考——也许 R 确实有能力使用它们.上述论文确实引用了用于 ranuni 的算法 SAS,但它也指出,由于使用了一些更正(或者,可能是由于浮点精度问题?),您无法完美地复制它.p>

Given the same seed, is there a way to produce the exact same random normal numbers generated in SAS, using the rannor function, in R?

解决方案

To make these match, you need two things:

  • The seed used to generate the random number
  • The formula used to generate the random number

SAS uses for rannor (and I think also for rand, but I haven't seen confirmation of this), the following algorithm (found in Psuedo-Random Numbers: Out of Uniform, by Robert Johnson and Hui Liu):

rannor_result = (−2* log(U1))**.5*cos(2*constant('pi')*U2)

where U1 and U2 are two numbers from the uniform number stream. (A second number could also be obtained, but that number is discarded as far as I can tell.)

See the following SAS datastep:

data test1;
  U1 = ranuni(7);
  U2 = ranuni(7);
  X1 = (−2* log(U1))**.5*cos(2*constant('pi')*U2);
  U1 = ranuni(7);
  U2 = ranuni(7);
  X2 = (−2* log(U1))**.5*cos(2*constant('pi')*U2);
run;
data test2;
  x1 = rannor(7);
  x2 = rannor(7);
run;

test1 and test2 have identical values for the rannorm numbers.

I doubt that R and SAS share common PRNG algorithms, particularly as the one rannor uses is not very good (you should use rand which is much, much better, using a Mersenne Twister). However, you can easily ask SAS to output the seeds it used - as long as you output the RANUNI results.

You can ask SAS to do that in this manner:

data rands_uni;
  seed=7;
  do _i_=1 to 10;
    seed1 = seed;
    call ranuni(seed,x1);
    seed2=seed;
    call ranuni(seed,x2);
    output;
  end;
run;

From that you could then in R calculate the rannor result (ie, from x1 and x2). I include the seeds there for reference - perhaps R does have the ability to use them. The above paper does reference the algorithm SAS used for ranuni, but it also states that you cannot replicate it perfectly due to some corrections used (or, perhaps, due to floating point precision issues?).

这篇关于基于相同的种子在R中复制SAS(rancor)中生成的随机法线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆