问题描述
我需要从二项式(N,P)分布生成随机数。
I need to generate random numbers from Binomial(n,p) distribution.
一个二项式(N,P)随机变量是取n统一变量的总和1以概率p。在伪代码, X = 0;对于(I = 0; I&下; N ++ⅰ)X + =(RAND()&下,P?1:0);将生成一个二项式(N,P)
A Binomial(n,p) random variable is sum of n uniform variables which take 1 with probability p. In pseudo code, x=0; for(i=0; i<n; ++i) x+=(rand()<p?1:0); will generate a Binomial(n,p).
我需要生成这对小以至真大N,例如N = 10 ^ 6和p = 0.02 。有没有快速的数值算法来生成它。
I need to generate this for small as well as really large n, for example n = 10^6 and p=0.02. Is there any fast numerical algorithm to generate it?
编辑 - ?
现在这是我的近似(以及用于精确泊松分布和正态分布函数) -
Right now this is what I have as approximation (along with functions for exact Poisson and Normal distribution)-
public long Binomial(long n, double p) {
// As of now it is an approximation
if (n < 1000) {
long result = 0;
for (int i=0; i<n; ++i)
if (random.NextDouble() < p) result++;
return result;
}
if (n * p < 10) return Poisson(n * p);
else if (n * (1 - p) < 10) return n - Poisson(n * p);
else {
long v = (long)(0.5 + nextNormal(n * p, Math.Sqrt(n * p * (1 - p))));
if (v < 0) v = 0;
else if (v > n) v = n;
return v;
}
}
推荐答案
如果你愿意支付,然后看看的通过Centerspace。
If you are willing to pay, then take a look at NMath by Centerspace.
否则由统计程序R用于C代码是。
另一个编辑:,你想要做什么
ANOTHER A free C# library that does what you want.
这篇关于C#:数值算法生成的二项分布数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!