]> granicus.if.org Git - python/commitdiff
Add cum_weights example (simulation of a cumulative binomial distribution).
authorRaymond Hettinger <python@rcn.com>
Thu, 20 Oct 2016 08:36:52 +0000 (01:36 -0700)
committerRaymond Hettinger <python@rcn.com>
Thu, 20 Oct 2016 08:36:52 +0000 (01:36 -0700)
Doc/library/random.rst

index a47ed9ce3dd05e04925b4924f9c89c98b5a05660..edf76d7f91312be04b3b602240f4623261bcce37 100644 (file)
@@ -351,6 +351,13 @@ Basic usage::
    >>> choices(['red', 'black', 'green'], [18, 18, 2], k=6)
    ['red', 'green', 'black', 'black', 'red', 'black']
 
+   # Probability of getting 5 or more heads from 7 spins of a biased coin
+   # that settles on heads 60% of the time.
+   >>> n = 10000
+   >>> cw = [0.60, 1.00]
+   >>> sum(choices('HT', cum_weights=cw, k=7).count('H') >= 5 for i in range(n)) / n
+   0.4169
+
 Example of `statistical bootstrapping
 <https://en.wikipedia.org/wiki/Bootstrapping_(statistics)>`_ using resampling
 with replacement to estimate a confidence interval for the mean of a small