Reputation: 11
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:
Given: Two positive integers k (k≤7) and N (N≤2k). In this problem, we begin with Tom, who in the 0th generation has genotype Aa Bb. Tom has two children in the 1st generation, each of whom has two children, and so on. Each organism always mates with an organism having genotype Aa Bb.
Return: The probability that at least N Aa Bb organisms will belong to the k-th generation of Tom's family tree (don't count the Aa Bb mates at each level). Assume that Mendel's second law holds for the factors.
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
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