MarianaZ
MarianaZ

Reputation: 11

Cumulative Binomial Probability Python

I tried to calculate the Cumulative Binomial Probability to resolve the "Independent Alleles" problem from Rosalind, and I think I got the program right but the result gives me "wrong" every time.

The problem:

My resolution:

def factorial(n):
    if n == 0:
        return 1
    else:
        return float(n * factorial(n-1))

print('Nr of generations?')
K=float(input())
print('At least how many AaBb organisms?')
N=float(input())
a=K**2 #found the error!!!
b=factorial(a)
c=factorial(a-N)*factorial(N)
d=(b//c)*(0.25**N)*(0.75**(a-N))
e=[]
while N <= a:
    c=factorial(a-N)*factorial(N)
    val=(b//c)*(0.25**N)*(0.75**(a-N))
    e.append(val)
    N=N+1
print('The probability of at least X organisms AaBb is:')
print(round(sum(e),3)) #P(X ≥ x)

Any idea what I may be doing wrong?

Upvotes: 0

Views: 212

Answers (1)

MarianaZ
MarianaZ

Reputation: 11

It's 2^k and not K^2, that was the problem

def factorial(n):
    if n == 0:
        return 1
    else:
        return float(n * factorial(n-1))

print('Nr of generations?')
K=float(input())
print('At least how many AaBb organisms?')
N=float(input())
a=2**k #found the error!!!
b=factorial(a)
c=factorial(a-N)*factorial(N)
d=(b//c)*(0.25**N)*(0.75**(a-N))
e=[]
while N <= a:
    c=factorial(a-N)*factorial(N)
    val=(b//c)*(0.25**N)*(0.75**(a-N))
    e.append(val)
    N=N+1
print('The probability of at least X organisms AaBb is:')
print(round(sum(e),3)) #P(X ≥ x)

Upvotes: 0

Related Questions