Challenge: Binomial Distribution #2

Subdomeniu: Probability (probability)

Scor cont: 5.0 / 5

Submission status: Accepted

Submission score: 1.0

Submission ID: 464715696

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/binomial-distribution-2/problem

Cerinta

The ratio of boys to girls born in Russia is 1.09:1.

What proportion of Russian families with exactly 6 children will have at least 3 boys? (Ignore the probability of multiple births.)

**Submission Modes and Output Format**

You may submit either an R or Python program to accomplish the above task, or solve the problem on pen-and-paper. Your output should be a floating point/decimal number, correct to 3 places of decimal.

1. In the text box below, enter a floating point/decimal number, correct to 3 places of decimal.

2. Alternatively, you may submit an R program, which uses the above parameters (hard-coded) and computes the answer.

Your answer should resemble something like:
<pre>
0.123
</pre>
(This is **NOT** the answer, just a demonstration of what the answering format should resemble).

Cod sursa

# Mathematics > Probability > Binomial Distribution #2
# Problems based on basic statistical distributions.
#
# https://www.hackerrank.com/challenges/binomial-distribution-2/problem
# challenge id: 12840
#

from __future__ import print_function
from math import factorial

# b(x,n,p) = C(n,p) * p^x * (1-p)^(n-x)

# x: number of successes
# n: total number of trials
# p: probability of success of 1 trial
def b(n, x, p):
    return factorial(n) // factorial(n - x) // factorial(x) * (p ** x) * (1 - p) ** (n - x)


boys, girls = 1.09, 1.00

p = boys / (boys + girls)

r = b(6, 3, p) + b(6, 4, p) + b(6, 5, p) + b(6, 6, p)

print("%.3f" % r)
HackerRank Probability – Binomial Distribution #2