Skip to content

Commit 5fc84b4

Browse files
committed
Fix binomial_distribution overflow at 171 trials (#15656)
1 parent 6e03a48 commit 5fc84b4

1 file changed

Lines changed: 43 additions & 41 deletions

File tree

‎maths/binomial_distribution.py‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
"""For more information about the Binomial Distribution -
2-
https://en.wikipedia.org/wiki/Binomial_distribution"""
3-
4-
from math import factorial
5-
6-
7-
def binomial_distribution(successes: int, trials: int, prob: float) -> float:
8-
"""
9-
Return probability of k successes out of n tries, with p probability for one
10-
success
11-
12-
The function uses the factorial function in order to calculate the binomial
13-
coefficient
14-
15-
>>> binomial_distribution(3, 5, 0.7)
16-
0.30870000000000003
17-
>>> binomial_distribution (2, 4, 0.5)
18-
0.375
19-
"""
20-
if successes > trials:
21-
raise ValueError("""successes must be lower or equal to trials""")
22-
if trials < 0 or successes < 0:
23-
raise ValueError("the function is defined for non-negative integers")
24-
if not isinstance(successes, int) or not isinstance(trials, int):
25-
raise ValueError("the function is defined for non-negative integers")
26-
if not 0 < prob < 1:
27-
raise ValueError("prob has to be in range of 1 - 0")
28-
probability = (prob**successes) * ((1 - prob) ** (trials - successes))
29-
# Calculate the binomial coefficient: n! / k!(n-k)!
30-
coefficient = float(factorial(trials))
31-
coefficient /= factorial(successes) * factorial(trials - successes)
32-
return probability * coefficient
33-
34-
35-
if __name__ == "__main__":
36-
from doctest import testmod
37-
38-
testmod()
39-
print("Probability of 2 successes out of 4 trials")
40-
print("with probability of 0.75 is:", end=" ")
41-
print(binomial_distribution(2, 4, 0.75))
1+
"""For more information about the Binomial Distribution -
2+
https://en.wikipedia.org/wiki/Binomial_distribution"""
3+
4+
from math import comb
5+
6+
7+
def binomial_distribution(successes: int, trials: int, prob: float) -> float:
8+
"""
9+
Return probability of k successes out of n tries, with p probability for one
10+
success
11+
12+
The function uses the factorial function in order to calculate the binomial
13+
coefficient
14+
15+
>>> binomial_distribution(3, 5, 0.7)
16+
0.30870000000000003
17+
>>> binomial_distribution (2, 4, 0.5)
18+
0.375
19+
>>> binomial_distribution(85, 171, 0.5)
20+
0.060749764626165485
21+
"""
22+
if successes > trials:
23+
raise ValueError("""successes must be lower or equal to trials""")
24+
if trials < 0 or successes < 0:
25+
raise ValueError("the function is defined for non-negative integers")
26+
if not isinstance(successes, int) or not isinstance(trials, int):
27+
raise ValueError("the function is defined for non-negative integers")
28+
if not 0 < prob < 1:
29+
raise ValueError("prob has to be in range of 1 - 0")
30+
probability = (prob**successes) * ((1 - prob) ** (trials - successes))
31+
# Calculate the binomial coefficient: n! / k!(n-k)!
32+
# math.comb is exact and avoids converting huge factorials to float.
33+
coefficient = comb(trials, successes)
34+
return probability * coefficient
35+
36+
37+
if __name__ == "__main__":
38+
from doctest import testmod
39+
40+
testmod()
41+
print("Probability of 2 successes out of 4 trials")
42+
print("with probability of 0.75 is:", end=" ")
43+
print(binomial_distribution(2, 4, 0.75))

0 commit comments

Comments
 (0)