From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Tue, 9 Jan 2024 19:24:55 +0000 (+0100) Subject: [3.12] Simplify binomial approximation example with random.binomialvariate() (gh... X-Git-Tag: v3.12.2~206 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9a6b99ee8b750c8891b488a6bd60696bc164c6fa;p=thirdparty%2FPython%2Fcpython.git [3.12] Simplify binomial approximation example with random.binomialvariate() (gh-113871) (gh-113872) --- diff --git a/Doc/library/statistics.rst b/Doc/library/statistics.rst index 318e5d746114..36f47b92ee3d 100644 --- a/Doc/library/statistics.rst +++ b/Doc/library/statistics.rst @@ -1016,19 +1016,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