mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Simplify binomial approximation example with random.binomialvariate() (gh-113871)
This commit is contained in:
parent
65f8eb7119
commit
2fd2e74793
1 changed files with 4 additions and 7 deletions
|
@ -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)
|
>>> round(NormalDist(mu=n*p, sigma=sqrt(n*p*q)).cdf(k + 0.5), 4)
|
||||||
0.8402
|
0.8402
|
||||||
|
|
||||||
>>> # Solution using the cumulative binomial distribution
|
>>> # Exact solution using the cumulative binomial distribution
|
||||||
>>> from math import comb, fsum
|
>>> from math import comb, fsum
|
||||||
>>> round(fsum(comb(n, r) * p**r * q**(n-r) for r in range(k+1)), 4)
|
>>> round(fsum(comb(n, r) * p**r * q**(n-r) for r in range(k+1)), 4)
|
||||||
0.8402
|
0.8402
|
||||||
|
|
||||||
>>> # Approximation using a simulation
|
>>> # Approximation using a simulation
|
||||||
>>> from random import seed, choices
|
>>> from random import seed, binomialvariate
|
||||||
>>> seed(8675309)
|
>>> seed(8675309)
|
||||||
>>> def trial():
|
>>> mean(binomialvariate(n, p) <= k for i in range(10_000))
|
||||||
... return choices(('Python', 'Ruby'), (p, q), k=n).count('Python')
|
0.8406
|
||||||
...
|
|
||||||
>>> mean(trial() <= k for i in range(10_000))
|
|
||||||
0.8398
|
|
||||||
|
|
||||||
|
|
||||||
Naive bayesian classifier
|
Naive bayesian classifier
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue