何时以及为什么不对某些函数及其输入采样随机数生成器? [英] When and why are random number generators not sampled for certain functions and their inputs?

查看:60
本文介绍了何时以及为什么不对某些函数及其输入采样随机数生成器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在之前的我的问题 我了解到当 p = 1p = 0 中时,随机数生成器不会被采样rbinom() 函数.在给定某些输入的情况下,是否有任何其他函数应该注意随机数生成器未采样的位置?我问的原因是,尽管使用了固定种子,但在与这些输出变量无关的参数发生变化后,某些变量的模拟输出在两次模拟之间并不是恒定的.

In a previous question of mine I learned that a random number generator is not sampled when p = 1 or p = 0 in the rbinom() function. Are there any other functions one should be aware of where a random number generator is not sampled given certain inputs? The reason I ask is that the simulated output of certain variables is not constant between simulations after changes in parameters unrelated to these output variables despite the use of a fixed seed.

我正在使用的 R 版本:

The version of R I am working with:

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes    

推荐答案

您可能会关注 R 的随机数生成方法的一个问题是 R 使用的伪随机生成器 (PRNG) 是全局的,但可以重新播种(使用 set.seed)用于可重复的随机性".

One issue with R's approach to random number generation that you may be concerned with is that the pseudorandom generator (PRNG) used by R is global, yet can be reseeded (with set.seed) for repeatable "randomness".

据我所知,rnormrunifrpois 等的算法没有在允许你知道每种方法在什么情况下推进 R 的全局 PRNG 多远.要了解有关每种方法的确切算法的更多信息,请查看 R 的源代码.然而,可以为可重复的随机性"重新播种全局 PRNG 的事实并非如此.从某种意义上说,意味着在不影响向后兼容性的情况下无法轻易更改 PRNG 的算法.另请参阅与您的问题相关的答案:为 <random> 创建 PRNG 引擎在 C++11 中匹配 PRNG 结果在 R

As far as I know, the algorithms for rnorm, runif, rpois, etc., are not documented at a level that allows you to know how far each method advances R's global PRNG and under what circumstances. To learn more about the exact algorithm of each method, look at R's source code. However, the fact the global PRNG can be reseeded for repeatable "randomness" means, in a sense, that the PRNG's algorithms can't easily be changed without affecting backward compatibility. See also an answer that relates to your concern: Creating a PRNG engine for <random> in C++11 that matches PRNG results in R

即便如此,将全局状态用于 PRNG 还是有问题的,尤其是当全局种子可以在 PRNG 用户的背后更改时.(另请参阅 NumPy 的 RNG 政策.) 对于 R 来说,更好的方法是使用轻量级 PRNG 对象,这些对象是单独播种的,然后将它们传递出去(这样我们就可以拥有,例如,rnorm(rng, mean, stdev).) 不同类型的 PRNG 对象可以实现不同的 PRNG 算法和/或不同的随机变量生成算法(即不同版本的 rnormrunif 等).

Even so, the use of global state for a PRNG is problematic, especially when the global seed can change behind the backs of the PRNG's users. (See also NumPy's RNG policy.) A better approach would have been for R to use lightweight PRNG objects, which are seeded individually, and pass those around (so that we would have, for example, rnorm(rng, mean, stdev).) Different kinds of PRNG objects could implement different PRNG algorithms and/or different random variate generation algorithms (that is, different versions of rnorm, runif, etc.).

这里有一些方法可以解决您在上一个问题中的潜在问题:

Here are some ways to solve your underlying concern in your previous question:

  • 考虑常见随机数"的技术,您可以搜索该技术在本网站上.
  • 使用自定义方法替换对 rnormrpois 等的调用,该方法调用 runif 以生成统一的随机数,然后将其转换为通过反演到所需分布的数字(参见逆变换采样").立>
  • 使用自定义方法替换对 rnormrpois 等的调用,该方法调用 runif 以生成统一随机数,然后使用该方法number 作为本地(非全局)伪随机生成器的种子,该生成器反过来用于从所需分布生成变量.JAX 中采用了类似的方法,尤其是它的 PRNGKey 类.
  • Consider the technique of "common random numbers", which you can search for on this site.
  • Replace calls to rnorm, rpois, etc. with a custom method that calls runif to generate a uniform random number, then transforms that number via inversion to the desired distribution (see "inverse transform sampling").
  • Replace calls to rnorm, rpois, etc. with a custom method that calls runif to generate a uniform random number, then uses that number as the seed to a local (non-global) pseudorandom generator, which in turn is used to produce a variate from the desired distribution. Something similar to this is employed in JAX, especially its PRNGKey class.

请注意,假设 runif 始终绘制固定数量的随机位,无论情况如何,最后两个建议都适用于您的目的.

Note that the last two suggestions will work for your purposes, assuming that runif always draws a fixed number of random bits regardless of the circumstances.

这篇关于何时以及为什么不对某些函数及其输入采样随机数生成器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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