简体   繁体   English

如何在numpy中指定随机数生成器

[英]How to specify a random number generator in numpy

I'm building a monte carlo simulation using python, and have thus far been using numpy to generate my random variates. 我正在使用python构建蒙特卡罗模拟,并且到目前为止已经使用numpy来生成我的随机变量。 However, I've just learned that numpy uses the Mersenne Twister algorithm to produce its random numbers, which based on my limited understanding is not desireable in monte carlo simulations. 然而,我刚刚了解到numpy使用Mersenne Twister算法生成随机数,基于我有限的理解,这在蒙特卡罗模拟中是不可取的。 I'd much prefer to use MRG32k3a, but I'd also like to take advantage of numpy's distribution functions. 我更喜欢使用MRG32k3a,但我也想利用numpy的分配功能。 Is there any way to make numpy use a generator of my choice, or is there another library that will give me the same functionality with the option of using my preferred generator? 有没有办法让numpy使用我选择的生成器,或者是否有另一个库可以使用我喜欢的生成器选项给我相同的功能?

Why don't you code it up yourself in python? 为什么不在python中自己编写代码呢?

I found an example implementation of the algorithm at http://simul.iro.umontreal.ca/rng/MRG32k3a.c (with many others at http://www-labs.iro.umontreal.ca/~simul/rng ) 我在http://simul.iro.umontreal.ca/rng/MRG32k3a.c找到了算法的示例实现(许多其他人在http://www-labs.iro.umontreal.ca/~simul/rng

If you are worried about speed (but profile it first!) you could wrap the C or F code and call it directly from python using eg CFFI, f2py etc - see eg 如果你担心速度(但首先对它进行配置!)你可以包装C或F代码并使用例如CFFI,f2py等直接从python调用它 - 参见eg

https://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html https://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html

But given it's only a few lines of C coding it in python shouldn't be too challenging, right? 但鉴于它只有几行C编码,它在python中不应该太具有挑战性,对吧?

You could also submit a pull request at https://github.com/bashtage/randomgen (or ask the author nicely to include your chosen generator). 您还可以通过https://github.com/bashtage/randomgen提交拉取请求(或者请作者很好地包含您选择的生成器)。

I'm sorry this doesn't address your need for distributions, but perhaps it's a start and a better answer will come along. 对不起,这并不能解决您对发行版的需求,但也许这是一个开始,一个更好的答案将会出现。

Also perhaps work out why Mersenne is no good for your own purposes - have you tested it and what figures of merit are you using to decide? 也许可以找出为什么Mersenne对你自己的目的不利 - 你测试过它以及你用什么数字来决定?

https://scicomp.stackexchange.com/questions/23547/parallel-mersenne-twister-for-monte-carlo might be a useful reference for you. https://scicomp.stackexchange.com/questions/23547/parallel-mersenne-twister-for-monte-carlo可能是一个有用的参考。

Can you do any precomputation? 你能做任何预先计算吗?

Hope that helps in some small way. 希望在某种程度上有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM