Skip to content

Commit 2fd2e74

Browse files
authored
Simplify binomial approximation example with random.binomialvariate() (gh-113871)
1 parent 65f8eb7 commit 2fd2e74

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

Doc/library/statistics.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,19 +1026,16 @@ probability that the Python room will stay within its capacity limits?
10261026
>>> round(NormalDist(mu=n*p, sigma=sqrt(n*p*q)).cdf(k + 0.5), 4)
10271027
0.8402
10281028

1029-
>>> # Solution using the cumulative binomial distribution
1029+
>>> # Exact solution using the cumulative binomial distribution
10301030
>>> from math import comb, fsum
10311031
>>> round(fsum(comb(n, r) * p**r * q**(n-r) for r in range(k+1)), 4)
10321032
0.8402
10331033

10341034
>>> # Approximation using a simulation
1035-
>>> from random import seed, choices
1035+
>>> from random import seed, binomialvariate
10361036
>>> seed(8675309)
1037-
>>> def trial():
1038-
... return choices(('Python', 'Ruby'), (p, q), k=n).count('Python')
1039-
...
1040-
>>> mean(trial() <= k for i in range(10_000))
1041-
0.8398
1037+
>>> mean(binomialvariate(n, p) <= k for i in range(10_000))
1038+
0.8406
10421039

10431040

10441041
Naive bayesian classifier

0 commit comments

Comments
 (0)