0

Hi,
I need some help getting the hypergeom distr. I've started with this code, but it doesn't seem work:

def logchoose(n, k):
    	lgn = special.gammaln(n+1)
   	lgk = special.gammaln(k+1)
   	lgnk = special.gammaln(n-k+1)
   	return lgn - (lgnk + lgk)

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

b = 2450
r = 346
N = r + b
n = 5000  
x = 1000
p = hypgeo(x,r,b,n)
print p
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Gribouillis
0

I think arguments to logchoose() should be swapped

from scipy import exp, special

def logchoose(n, k):
        lgn = special.gammaln(n+1)
        lgk = special.gammaln(k+1)
        lgnk = special.gammaln(n-k+1)
        return lgn - (lgnk + lgk)


def hypgeo(x, r, b, n):
    u = logchoose(x, r)
    v = logchoose(n-x, b)
    w = logchoose(n, r + b)
    return exp(u + v - w)

    
b = 2450
r = 346
N = r + b
n = 5000  
x = 1000
p = hypgeo(x,r,b,n)
print p

""" my output -->
2.79971621598e-52
"""

Edited by Gribouillis: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.