4:Binomial distribution in python
Binomial distribution in python
The ratio of boys to girls for babies born in Russia is . If there is child born per birth, what proportion of Russian families with exactly children will have at least boys?
Write a program to compute the answer using the above parameters. Then print your result, rounded to a scale of decimal places (i.e., format).
Sloution:
pc=str(input())
p1=float(pc[:pc.find(' ')])
p2=float(pc[pc.find(' ')+1:])
pf=0.0
#This is the probability of boy
p = p1/(p1+p2)
from math import factorial as f
def comb(n,r):
return f(n) / f(r) / f(n-r)
#You have to sum the probabilities of 3,4,5 or 6 boys
for i in range(3,7):
#You were not using the right formula (it's a multiplication, not a sum)
prob_i = pow(p,i)*pow(1-p,6-i)*comb(6,i)
pf += prob_i
print("%.3f"%(pf))
Comments
Post a Comment