Created
February 23, 2010 14:23
-
-
Save showyou/312197 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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