# Hypergeometric distribution

Gerard Flanagan grflanagan at yahoo.co.uk
Mon Dec 26 22:45:36 CET 2005

```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 = 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

```