我一直在环顾四周,但我不知道该怎么做。
我发现此页面在最后一段中说:
使用以下简单方法可以获得从泊松分布中获取的随机数的简单生成器:如果 x1, x2, ... 是随机序列0 到 1 之间均匀分布的数字,k 是第一个整数,其乘积 x1 · x2 · ... · xk+1< /子> < e-λ
我发现另一页描述如何生成二项式数,但我认为它使用的是泊松生成的近似值,这对我没有帮助。
例如,考虑二项式随机数。 二项式随机数是 N 次抛硬币中正面朝上的数量,其中任何一次抛掷正面朝上的概率为 p。 如果您在区间 (0,1) 上生成 N 个均匀随机数并计算小于 p 的数字,则该计数是一个具有参数 N 和 p 的二项式随机数。
我知道有库可以做到这一点,但我不能使用它们,只能使用语言(在本例中为 java)提供的标准统一生成器。
i've been looking around, but i'm not sure how to do it.
i've found this page which, in the last paragraph, says:
A simple generator for random numbers taken from a Poisson distribution is obtained using this simple recipe: if x1, x2, ... is a sequence of random numbers with uniform distribution between zero and one, k is the first integer for which the product x1 · x2 · ... · xk+1 < e-λ
i've found another page describing how to generate binomial numbers, but i think it is using an approximation of poisson generation, which doesn't help me.
For example, consider binomial random numbers. A binomial random number is the number of heads in N tosses of a coin with probability p of a heads on any single toss. If you generate N uniform random numbers on the interval (0,1) and count the number less than p, then the count is a binomial random number with parameters N and p.
i know there are libraries to do it, but i can't use them, only the standard uniform generators provided by the language (java, in this case).
发布评论
评论(5)
泊松分布
以下是 维基百科如何描述 Knuth 的说法:
在 Java 中,这将be:
二项式分布
按照 非均匀随机变量生成 (PDF) 第 10 章Luc Devroye 的 (我发现从维基百科文章链接)给出了这个:
请注意,
这些算法都不是最佳的。 第一个是 O(λ),第二个是 O(n)。 根据这些值通常有多大,以及您需要调用生成器的频率,您可能需要更好的算法。 我上面链接的论文具有在恒定时间内运行的更复杂的算法,但我将把这些实现留给读者作为练习。 :)
Poisson distribution
Here's how Wikipedia says Knuth says to do it:
In Java, that would be:
Binomial distribution
Going by chapter 10 of Non-Uniform Random Variate Generation (PDF) by Luc Devroye (which I found linked from the Wikipedia article) gives this:
Please note
Neither of these algorithms is optimal. The first is O(λ), the second is O(n). Depending on how large these values typically are, and how frequently you need to call the generators, you might need a better algorithm. The paper I link to above has more complicated algorithms that run in constant time, but I'll leave those implementations as an exercise for the reader. :)
对于这个问题和其他数字问题,圣经就是数字食谱书。
这里有一个免费的 C 版本:http://www.nrbook.com/a/bookcpdf。 php(需要插件)
或者您可以在谷歌图书上看到它:http://books.google.co.uk/books?id= 4t-sybVuoqoC&lpg=PP1&ots=5IhMINLhHo&dq=numerical%20recipes%20in%20c&pg=PP1#v=onepage&q=&f=false
C代码应该很容易传输到爪哇。
对于许多数值问题,这本书物有所值。 在上述网站上您还可以购买该书的最新版本。
For this and other numerical problems the bible is the numerical recipes book.
There's a free version for C here: http://www.nrbook.com/a/bookcpdf.php (plugin required)
Or you can see it on google books: http://books.google.co.uk/books?id=4t-sybVuoqoC&lpg=PP1&ots=5IhMINLhHo&dq=numerical%20recipes%20in%20c&pg=PP1#v=onepage&q=&f=false
The C code should be very easy to transfer to Java.
This book is worth it's weight in gold for lots of numerical problems. On the above site you can also buy the latest version of the book.
尽管 Kip 发布的答案对于生成具有小到达率 (lambda) 的泊松 RV 完全有效,但维基百科中发布的第二个算法 生成泊松随机变量更适合较大的到达率。
由于这个原因,我在实施一个需要生成具有非常高 lambda 的 Poisson RV 的项目时遇到了问题。 所以我建议采用另一种方式。
Although the answer posted by Kip is perfectly valid for generating Poisson RVs with small rate of arrivals (lambda), the second algorithm posted in Wikipedia Generating Poisson Random variables is better for larger rate of arrivals due to numerical stability.
I faced problems during implementation of one of the projects requiring generation of Poisson RV with very high lambda due to this. So I suggest the other way.
以下库(Java 代码)中有来自 CERN 的多个实现:
http://acs.lbl .gov/~hoschek/colt/
关于二项式随机数,它基于 1988 年的论文“Binomial Random Variate Generation”,我向您推荐该论文,因为他们使用了优化算法。
问候
There are several implementations from CERN in the following library (Java code):
http://acs.lbl.gov/~hoschek/colt/
Concerning binomial random numbers, it is based on the paper from 1988 "Binomial Random Variate Generation", that I recommend to you since they use an optimized algorithm.
Regards
您可以将其添加到 build.gradle
并使用类 PoissonDistribution
更多详细信息对于 PoissonDistribution 类
you can add this to build.gradle
and use class PoissonDistribution
more detail for class PoissonDistribution