summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2024-01-09 19:02:07 (GMT)
committerGitHub <noreply@github.com>2024-01-09 19:02:07 (GMT)
commit2fd2e747930987eb8ed4929cf0132e85db759dab (patch)
treea8e62f4ffcc1425d68c2f984e480b532ddf9a874
parent65f8eb71190f870c66fb00da29a670ee232a3fd5 (diff)
downloadcpython-2fd2e747930987eb8ed4929cf0132e85db759dab.zip
cpython-2fd2e747930987eb8ed4929cf0132e85db759dab.tar.gz
cpython-2fd2e747930987eb8ed4929cf0132e85db759dab.tar.bz2
Simplify binomial approximation example with random.binomialvariate() (gh-113871)
-rw-r--r--Doc/library/statistics.rst11
1 files changed, 4 insertions, 7 deletions
diff --git a/Doc/library/statistics.rst b/Doc/library/statistics.rst
index 5c8ad3a..588c9c0 100644
--- a/Doc/library/statistics.rst
+++ b/Doc/library/statistics.rst
@@ -1026,19 +1026,16 @@ probability that the Python room will stay within its capacity limits?
>>> round(NormalDist(mu=n*p, sigma=sqrt(n*p*q)).cdf(k + 0.5), 4)
0.8402
- >>> # Solution using the cumulative binomial distribution
+ >>> # Exact solution using the cumulative binomial distribution
>>> from math import comb, fsum
>>> round(fsum(comb(n, r) * p**r * q**(n-r) for r in range(k+1)), 4)
0.8402
>>> # Approximation using a simulation
- >>> from random import seed, choices
+ >>> from random import seed, binomialvariate
>>> seed(8675309)
- >>> def trial():
- ... return choices(('Python', 'Ruby'), (p, q), k=n).count('Python')
- ...
- >>> mean(trial() <= k for i in range(10_000))
- 0.8398
+ >>> mean(binomialvariate(n, p) <= k for i in range(10_000))
+ 0.8406
Naive bayesian classifier