Skip to content

Commit 9a6b99e

Browse files
[3.12] Simplify binomial approximation example with random.binomialvariate() (gh-113871) (gh-113872)
1 parent 6b6f91e commit 9a6b99e

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
@@ -1016,19 +1016,16 @@ probability that the Python room will stay within its capacity limits?
10161016
>>> round(NormalDist(mu=n*p, sigma=sqrt(n*p*q)).cdf(k + 0.5), 4)
10171017
0.8402
10181018

1019-
>>> # Solution using the cumulative binomial distribution
1019+
>>> # Exact solution using the cumulative binomial distribution
10201020
>>> from math import comb, fsum
10211021
>>> round(fsum(comb(n, r) * p**r * q**(n-r) for r in range(k+1)), 4)
10221022
0.8402
10231023

10241024
>>> # Approximation using a simulation
1025-
>>> from random import seed, choices
1025+
>>> from random import seed, binomialvariate
10261026
>>> seed(8675309)
1027-
>>> def trial():
1028-
... return choices(('Python', 'Ruby'), (p, q), k=n).count('Python')
1029-
...
1030-
>>> mean(trial() <= k for i in range(10_000))
1031-
0.8398
1027+
>>> mean(binomialvariate(n, p) <= k for i in range(10_000))
1028+
0.8406
10321029

10331030

10341031
Naive bayesian classifier

0 commit comments

Comments
 (0)