Page MenuHomeDevCentral

hypergeometric_distribution
No OneTemporary

hypergeometric_distribution

#!/usr/bin/env python3
import math
import sys
def binomial_coefficient(n, k):
f = math.factorial
return f(n) / f(k) / f(n - k)
def compute(population_size, success_states_count, draws_count, observed_successes):
N = population_size
K = success_states_count
n = draws_count
k = observed_successes
bc = binomial_coefficient
return bc(K, k) * bc(N - K, n - k) / bc(N, n)
def usage():
print(f"Usage: {sys.argv[0]} <population size> <number of success states> <number of draws> <number of observed successes>", file=sys.stderr)
def help():
usage()
print()
print("Example:")
print()
print("In a Magic the Gathering deck of 60 crads, you've put 23 lands.")
print("To compute the probability to get 3 lands in your opening hand (7 cards):")
print(f" {sys.argv[0]} 60 23 7 3")
def has_help_arg():
help_args = ["-h", "--help", "/?", "/help"]
return any([help_arg in sys.argv[1:] for help_arg in help_args])
if __name__ == "__main__":
if has_help_arg():
help()
sys.exit(0)
if len(sys.argv) != 5:
usage()
sys.exit(1)
try:
numbers = [float(arg) for arg in sys.argv[1:]]
probability = compute(*numbers)
print(probability)
except ValueError as e:
print(e)
usage()
sys.exit(2)

File Metadata

Mime Type
text/x-python
Expires
Tue, Oct 6, 02:27 (56 m, 20 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4145668
Default Alt Text
hypergeometric_distribution (1 KB)

Event Timeline