Skip to content

Instantly share code, notes, and snippets.

@augustgl
Created February 5, 2021 02:59
Show Gist options
  • Save augustgl/b9d6006c26374ba2771226169ae1bfc9 to your computer and use it in GitHub Desktop.
Save augustgl/b9d6006c26374ba2771226169ae1bfc9 to your computer and use it in GitHub Desktop.
binomial distribution function in python
# 2/2/2021
# stats class
import math
def factorial(n):
fact = 1
for i in range(1, n+1):
fact = fact * i
return fact
def findq(p):
return 1 - p
def binomdist(n, p):
print
for x in range(1, n + 1):
numerator = factorial(int(n))
denominator = factorial(int(x)) * factorial(int(n) - int(x))
fraction = numerator / denominator
q = findq(p)
secondhalf = pow(p, x) * pow(q, (n - x))
final = fraction * secondhalf
print str(final)
print
print "[+] MEAN " + str(n * p)
print
print "[+] VARIANCE " + str(n * p * q)
print
print "[+] STANDARD DEVIATION " + str(math.sqrt((n * p * q)))
print
def main():
binomdist(6, .85)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment