Given n trials, with a probability p of winning each trial, what is the probability of winning r or more trials?
My thinking goes as follows: Each combination of wins and losses has probability p^w * (p-1)^(n-w) where w is number of wins.
Each number of wins can occur in nCr combinations, ex. winning 2 out 3 times means you might lose the first, the second or the third times, e.g. three combinations.
So the probability of winning 2 out of 3 times is 3C2 * p^2 * (1-p)^1. The probability of winning 2 or more times is just the sum of this calculation for 2 and 3 wins.
I have the following code:
import math
def nCr(n,r):
f = math.factorial
return f(n) / f(r) / f(n-r)
def prob_at_least(n, r, p):
probs = [nCr(n,k)*pow(p,k)*pow(1.0-p,n-k) for k in range(r, n+1)]
return sum(probs)
This code works, but is there a built-in function, or a shorter way to achieve the same?