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

查看:111
本文介绍了基于相同的种子,在 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 = rannor(7);x2 = rannor(7);跑步;

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

我怀疑 R 和 SAS 共享公共 PRNG 算法,特别是因为 rannor 使用的一种不是很好(您应该使用 rand,它要好得多,使用梅森捻线机).但是,您可以轻松地要求 SAS 输出它使用的种子——只要您输出 RANUNI 结果.

您可以要求 SAS 以这种方式执行此操作:

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

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

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天全站免登陆