Skip to content

Instantly share code, notes, and snippets.

@showyou
Created February 23, 2010 14:23
Show Gist options
  • Select an option

  • Save showyou/312197 to your computer and use it in GitHub Desktop.

Select an option

Save showyou/312197 to your computer and use it in GitHub Desktop.
def combination( a, b ):
"""
>>> combination( 3, 1 )
3
>>> combination( 3, 2 )
3
>>> combination( 4, 2 )
6
"""
# a! / b!(a-b)!
return float(factorial(a)) / float(factorial(b)) / float(factorial(a-b))
def factorial( n ):
"""
>>> factorial( 1 )
1
>>> factorial( 3 )
6
>>> factorial( 5 )
120
"""
if n > 1: return n * factorial(n-1)
else: return 1
def negative_hypergeometric_distribution( N, p, r, x):
fx = combination( x-1, r-1 ) * combination( N-x, N*p - r ) / combination( N,
N * p)
return fx
if __name__ == "__main__":
import doctest
doctest.testmod(verbose = True)
N = 8.0
p = 0.125
r = 1
E = 0
for x in range( 1, N+1 ):
fx = negative_hypergeometric_distribution( N, p, r, x )
print "f(x) %s" % fx
E += fx * x
print E
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment