Last active
January 1, 2018 00:34
-
-
Save SwiftsNamesake/03a978eeb8f9cb9cfa83aab9a802d94a to your computer and use it in GitHub Desktop.
This file contains 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
# I have a biased coin that lands heads up with | |
# probability p and tails up with probability (1-p). I | |
# toss the coin n times. What is the probability that I | |
# will see at least k heads? | |
# | |
# https://www.quora.com/I-have-a-biased-coin-that-lands-heads-up-with-probability-p-and-tails-up-with-probability-1-p-I-toss-the-coin-n-times-What-is-the-probability-that-I-will-see-at-least-k-heads | |
def product(xs): | |
p = 1 | |
for i in xs: | |
p *= i | |
return p | |
def factorial(n): | |
return product(range(1, n+1)) | |
def choose(n, i): | |
return factorial(n)/(factorial(i)*factorial(n-i)) | |
def atleast_k_heads(n, k): | |
return sum(choose(n,i) * p**i * (1-p)**(n-i) for i in range(k, n+1)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment