超几何分布 [英] Hypergeometric distribution

查看:99
本文介绍了超几何分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我需要计算hpergeometric分布:

选择(r,x)*选择(b,nx)

p(x; r,b ,n)= -----------------------------

选择(r + b,n)


选择(r,x)是二项式系数

我用factorial来计算上面的公式但是因为我用的是

大数,选择(a,b)的结果(即:二项式系数)

即使对于大型int也是如此。我已经尝试了scipy库,但是这个

库也计算了使用阶乘的超几何的
,所以问题依然存在。

有没有任何其他libray或算法来计算超几何分布?
超几何分布?统计软件包R可以处理

这样的计算,但我不想使用python R绑定,因为我想要

a独立应用程序。

非常感谢

Ale

解决方案

Raven写道:

嗨到所有,我需要计算hpergeometric分布:

选择(r,x)*选择(b,nx)
p(x; r,b,n)= ---- -------------------------
选择(r + b,n)

选择(r,x )是二项式系数
我使用阶乘来计算上面的公式,但由于我使用的是大数,所以选择(a,b)的结果(即:二项式系数)
对于大型int来说太大了。我已经尝试了scipy库,但是这个库也使用阶乘计算了超几何,所以问题依然存在。是否有任何其他图书馆或算法来计算超几何分布?




使用对数。


具体来说,


来自scipy import special


def logchoose(n,k):

lgn1 = special.gammaln(n + 1)

lgk1 = special.gammaln(k + 1)

lgnk1 = special.gammaln(n-k + 1)

返回lgn1 - (lgnk1 + lgk1)

def gauss_hypergeom(x,r,b,n):

返回exp(logchoose( r,x)+

logchoose(b,nx) -

logchoose(r + b,n))


或你可以使用gmpy,如果你需要精确的理性算术而不是浮动

点。


-

Robert Kern ro*********@gmail.com


"在草地长得高的地狱里

梦想的坟墓是否已经死亡。

- Richard Harter


Raven写道:

大家好,我需要计算hpergeometric分布:

选择(r,x)*选择(b,nx)
p(x; r,b,n)= -----------------------------
选择(r + b,n)我用阶乘来计算上面的公式,但由于我使用的是大数,所以选择(a,b)的结果(即:二项式系数)
即使对于大型int也是如此。我已经尝试了scipy库,但是这个库也使用阶乘计算了超几何,所以问题依然存在。是否有任何其他图书馆或算法来计算超几何分布?统计软件包R可以处理这样的计算,但我不想使用python R绑定,因为我想要一个独立的应用程序。
非常感谢
Ale



Ale


我有这个代码,如果它有帮助的话。我不知道它是否正确,但是它是非正常的!


def二项式(n,k):

ret = 0

如果k == 0:

ret = 1

elif k> 0:

a =范围(n + 1)

a [0] = 1

for i in a [1:]:

a [i] = 1

范围内的j(i-1,0,-1):

a [j] = a [j] + a [j-1]

ret = a [k]

返回


Gerard


2005年12月26日星期一12:18:55 -0800,Raven写道:

大家好,我需要计算一下hpergeometric distribution:

选择(r,x)*选择(b,nx)
p(x; r,b,n)= ----------- ------------------
选择(r + b,n)

选择(r,x)是二项式系数即使对于大的int也是如此。


你确定吗? Python的长整数可以和你足够的一样大。

内存。我的Python可以打印10L ** 10000到控制台,只有几乎没有b $ b可检测到的暂停,而10L ** 100000则有大约十秒钟的延迟。 (大部分

延迟是打印它,而不是计算它。)


25206是第一个整数,其阶乘超过10L ** 100000,所以即使

你用最天真的算法计算二项式系数,计算阶乘和除法,你应该很容易得到它能够生成它对于a,b高达20,000,除非你有严重的内存短缺。

我已经尝试了scipy库,但这个
库计算<使用阶乘也是超几何的,所以问题存在。




究竟是什么问题?超几何(x; r,b,n)的值是什么值?
给你?


-

史蒂文。


Hi to all, I need to calculate the hpergeometric distribution:
choose(r, x) * choose(b, n-x)
p(x; r,b,n) = -----------------------------
choose(r+b, n)

choose(r,x) is the binomial coefficient
I use the factorial to calculate the above formula but since I am using
large numbers, the result of choose(a,b) (ie: the binomial coefficient)
is too big even for large int. I''ve tried the scipy library, but this
library calculates
the hypergeometric using the factorials too, so the problem subsist. Is
there any other libray or an algorithm to calculate
the hypergeometric distribution? The statistical package R can handle
such calculations but I don''t want to use python R binding since I want
a standalone app.
Thanks a lot
Ale

解决方案

Raven wrote:

Hi to all, I need to calculate the hpergeometric distribution:
choose(r, x) * choose(b, n-x)
p(x; r,b,n) = -----------------------------
choose(r+b, n)

choose(r,x) is the binomial coefficient
I use the factorial to calculate the above formula but since I am using
large numbers, the result of choose(a,b) (ie: the binomial coefficient)
is too big even for large int. I''ve tried the scipy library, but this
library calculates
the hypergeometric using the factorials too, so the problem subsist. Is
there any other libray or an algorithm to calculate
the hypergeometric distribution?



Use logarithms.

Specifically,

from scipy import special

def logchoose(n, k):
lgn1 = special.gammaln(n+1)
lgk1 = special.gammaln(k+1)
lgnk1 = special.gammaln(n-k+1)
return lgn1 - (lgnk1 + lgk1)

def gauss_hypergeom(x, r, b, n):
return exp(logchoose(r, x) +
logchoose(b, n-x) -
logchoose(r+b, n))

Or you could use gmpy if you need exact rational arithmetic rather than floating
point.

--
Robert Kern
ro*********@gmail.com

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter


Raven wrote:

Hi to all, I need to calculate the hpergeometric distribution:
choose(r, x) * choose(b, n-x)
p(x; r,b,n) = -----------------------------
choose(r+b, n)

choose(r,x) is the binomial coefficient
I use the factorial to calculate the above formula but since I am using
large numbers, the result of choose(a,b) (ie: the binomial coefficient)
is too big even for large int. I''ve tried the scipy library, but this
library calculates
the hypergeometric using the factorials too, so the problem subsist. Is
there any other libray or an algorithm to calculate
the hypergeometric distribution? The statistical package R can handle
such calculations but I don''t want to use python R binding since I want
a standalone app.
Thanks a lot
Ale



Ale

I had this code lying about if it helps. I don''t know if it''s even
correct but it''s non-recursive!

def Binomial( n, k ):
ret = 0
if k == 0:
ret = 1
elif k > 0:
a = range( n+1 )
a[0] = 1
for i in a[1:]:
a[i] = 1
for j in range(i-1,0,-1):
a[j] = a[j] + a[j-1]
ret = a[k]
return ret

Gerard


On Mon, 26 Dec 2005 12:18:55 -0800, Raven wrote:

Hi to all, I need to calculate the hpergeometric distribution:
choose(r, x) * choose(b, n-x)
p(x; r,b,n) = -----------------------------
choose(r+b, n)

choose(r,x) is the binomial coefficient
I use the factorial to calculate the above formula but since I am using
large numbers, the result of choose(a,b) (ie: the binomial coefficient)
is too big even for large int.
Are you sure about that? Python long ints can be as big as you have enough
memory for. My Python can print 10L**10000 to the console with a barely
detectable pause, and 10L**100000 with about a ten second delay. (Most of
that delay is printing it, not calculating it.)

25206 is the first integer whose factorial exceeds 10L**100000, so even if
you are calculating the binomial coefficient using the most naive
algorithm, calculating the factorials and dividing, you should easily be
able to generate it for a,b up to 20,000 unless you have a severe
shortage of memory.
I''ve tried the scipy library, but this
library calculates
the hypergeometric using the factorials too, so the problem subsist.



What exactly is your problem? What values of hypergeometric(x; r,b,n) fail
for you?

--
Steven.


这篇关于超几何分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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