Skip to content

Instantly share code, notes, and snippets.

@cscheid
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscheid/90b025af6923133497d8 to your computer and use it in GitHub Desktop.
Save cscheid/90b025af6923133497d8 to your computer and use it in GitHub Desktop.
probabilities, how do they work
import math
from pylab import *
def fact(x):
return math.gamma(x+1)
def choose(n,k):
return fact(n) / (fact(k) * fact(n-k))
def hypergeom_mass_at_k(N, K, n, k):
return choose(K, k) * choose(N-K, n-k) / choose(N, n)
def deal(n, k):
values = [0] * n
for i in xrange(0, k):
values[i] = 1
random.shuffle(values)
return values
def draw_at_least_one(N, K, n):
result = 0
for i in xrange(1, K+1):
try:
result += hypergeom_mass_at_k(N, K, n, i)
except ValueError:
pass
return result
def draw_at_least_one_monte_carlo(N, K, n, rounds=10000):
count = 0
for i in xrange(rounds):
if sum(deal(N, K)[:n]) > 0: count += 1
return count / float(rounds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment