# Python code for computing p-values against binomially
# distributed random variables, optimized for obviousness
# of conformance to textbook definitions.
def factorial(n):
""" Factorial function of non-negative integers.
"""
fact = 1
for i in range(1, n + 1):
fact = fact * i
return fact
def choose(n, m):
""" Return 'n-choose-m'.
"""
return factorial(n) / (factorial(m) * factorial(n - m))
def binomial(n, m, p):
""" Return the probability of exactly m hits in n independent
trials, where each trial hits with probability p.
"""
return float(choose(n, m)) * p**m * (1 - p)**(n - m)
def binomial_p(n, m, p):
""" Return the probability of m or more hits in n independent
trials, where each trial hits with probability p.
"""
sum = 0
for i in range(m, n + 1):
sum += binomial(n, i, p)
return sum