Last active
August 29, 2015 14:14
-
-
Save cscheid/90b025af6923133497d8 to your computer and use it in GitHub Desktop.
probabilities, how do they work
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
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