from __future__ import division import math def nCr(n,r): f = math.factorial return f(n) / f(r) / f(n-r) # W = wins in population # L = losses in population # x = successes in sample # n = sample size def Hypergeometric(W, L, x, n): sample_successes = nCr(W,x) sample_losses = nCr(L,n-x) all_combinations = nCr(W+L, n) return sample_successes*sample_losses/all_combinations # chosse 2 hearts from 6 cards # 13 hearts and 39 non-hearts # 2 successes needed from sample of 6 print "chosse 2 hearts from 6 cards:", Hypergeometric(13, 39, 2, 6) h = Hypergeometric(13, 39, 2, 6) def HypergeometricLessThan(W, L, x, n): return sum([Hypergeometric(W,L,num, n) for num in range(0,x)]) # fewer than two diamonds 63.3% print "fewer than two diamonds", HypergeometricLessThan(13,39,2,5), def HypergeometricGreaterThanOrEqualTo(W,L,x,n): return 1- HypergeometricLessThan(W, L, x, n)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question