From 24f33b5bb5dc51831522f7573b16fa66869f7b3b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Thu, 20 Oct 2016 01:36:52 -0700 Subject: Add cum_weights example (simulation of a cumulative binomial distribution). --- Doc/library/random.rst | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Doc/library/random.rst b/Doc/library/random.rst index a47ed9c..edf76d7 100644 --- a/Doc/library/random.rst +++ b/Doc/library/random.rst @@ -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 `_ using resampling with replacement to estimate a confidence interval for the mean of a small -- cgit v0.12