From b66783bb031a66c2ba6ed1ba9f828a015ede616d Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 22 Sep 2023 10:38:46 -0500 Subject: [PATCH 1/5] Add expected mean and variance to the docstrings --- Lib/random.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Lib/random.py b/Lib/random.py index 84bbfc5df1bf23..c5cfd363273e57 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -492,7 +492,11 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1): ## -------------------- real-valued distributions ------------------- def uniform(self, a, b): - "Get a random number in the range [a, b) or [a, b] depending on rounding." + """Get a random number in the range [a, b) or [a, b] depending on rounding. + + E[X] = (a + b) / 2 + Var[X] = (b - a) ** 2 / 12 + """ return a + (b - a) * self.random() def triangular(self, low=0.0, high=1.0, mode=None): @@ -503,6 +507,9 @@ def triangular(self, low=0.0, high=1.0, mode=None): http://en.wikipedia.org/wiki/Triangular_distribution + E[X] = (low + high + mode) / 3 + Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18 + """ u = self.random() try: @@ -593,6 +600,9 @@ def expovariate(self, lambd=1.0): positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative. + E[X] = 1 / lambd + Var[X] = 1 / lambd ** 2 + """ # lambd: rate lambd = 1/mean # ('lambda' is a Python reserved word) @@ -654,6 +664,9 @@ def gammavariate(self, alpha, beta): pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha + E[X] = alpha * beta + Var[X] = alpha * beta ** 2 + """ # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 @@ -714,6 +727,9 @@ def betavariate(self, alpha, beta): Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. + E[X] = alpha / (alpha + beta) + Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1)) + """ ## See ## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html @@ -766,6 +782,9 @@ def binomialvariate(self, n=1, p=0.5): Returns an integer in the range: 0 <= X <= n + E[X] = n * p + Var[x] = n * p * (1 - p) + """ # Error check inputs and handle edge cases if n < 0: From d30c2153c62adc2459d7610d894691445b341c7b Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 22 Sep 2023 12:06:11 -0500 Subject: [PATCH 2/5] Remove redundant comment --- Lib/random.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/random.py b/Lib/random.py index c5cfd363273e57..ac133b1de16daf 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -496,6 +496,7 @@ def uniform(self, a, b): E[X] = (a + b) / 2 Var[X] = (b - a) ** 2 / 12 + """ return a + (b - a) * self.random() @@ -668,7 +669,6 @@ def gammavariate(self, alpha, beta): Var[X] = alpha * beta ** 2 """ - # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 # Warning: a few older sources define the gamma distribution in terms # of alpha > -1.0 From 0aecb973838ad5c037843508ccaf140a1de317c8 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 23 Sep 2023 12:57:21 -0500 Subject: [PATCH 3/5] Explain the E[X] and Var[X] notation --- Lib/random.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index ac133b1de16daf..82d27b28db6a56 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -494,8 +494,9 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1): def uniform(self, a, b): """Get a random number in the range [a, b) or [a, b] depending on rounding. - E[X] = (a + b) / 2 - Var[X] = (b - a) ** 2 / 12 + The expected value (mean) and variance of the random variable are: + E[X] = (a + b) / 2 + Var[X] = (b - a) ** 2 / 12 """ return a + (b - a) * self.random() @@ -508,8 +509,9 @@ def triangular(self, low=0.0, high=1.0, mode=None): http://en.wikipedia.org/wiki/Triangular_distribution - E[X] = (low + high + mode) / 3 - Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18 + The expected value (mean) and variance of the random variable are: + E[X] = (low + high + mode) / 3 + Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18 """ u = self.random() @@ -601,8 +603,9 @@ def expovariate(self, lambd=1.0): positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative. - E[X] = 1 / lambd - Var[X] = 1 / lambd ** 2 + The expected value (mean) and variance of the random variable are: + E[X] = 1 / lambd + Var[X] = 1 / lambd ** 2 """ # lambd: rate lambd = 1/mean @@ -665,8 +668,9 @@ def gammavariate(self, alpha, beta): pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha - E[X] = alpha * beta - Var[X] = alpha * beta ** 2 + The expected value (mean) and variance of the random variable are: + E[X] = alpha * beta + Var[X] = alpha * beta ** 2 """ @@ -727,8 +731,9 @@ def betavariate(self, alpha, beta): Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. - E[X] = alpha / (alpha + beta) - Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1)) + The expected value (mean) and variance of the random variable are: + E[X] = alpha / (alpha + beta) + Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1)) """ ## See @@ -782,8 +787,9 @@ def binomialvariate(self, n=1, p=0.5): Returns an integer in the range: 0 <= X <= n - E[X] = n * p - Var[x] = n * p * (1 - p) + The expected value (mean) and variance of the random variable are: + E[X] = n * p + Var[x] = n * p * (1 - p) """ # Error check inputs and handle edge cases From c17d2eb466680405018e4cd1d8420ca53bfdd4e5 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 24 Sep 2023 17:01:24 -0500 Subject: [PATCH 4/5] Remove redundant comment --- Lib/random.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 82d27b28db6a56..2cbd8ee383d847 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -608,11 +608,9 @@ def expovariate(self, lambd=1.0): Var[X] = 1 / lambd ** 2 """ - # lambd: rate lambd = 1/mean - # ('lambda' is a Python reserved word) - # we use 1-random() instead of random() to preclude the # possibility of taking the log of zero. + return -_log(1.0 - self.random()) / lambd def vonmisesvariate(self, mu, kappa): From 6703fad3b66f1e6e997c021566ce90471333bdb4 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sun, 24 Sep 2023 17:07:29 -0500 Subject: [PATCH 5/5] Minor beautification and wordsmithing --- Lib/random.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Lib/random.py b/Lib/random.py index 2cbd8ee383d847..1d789b107904fb 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -494,7 +494,8 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1): def uniform(self, a, b): """Get a random number in the range [a, b) or [a, b] depending on rounding. - The expected value (mean) and variance of the random variable are: + The mean (expected value) and variance of the random variable are: + E[X] = (a + b) / 2 Var[X] = (b - a) ** 2 / 12 @@ -509,7 +510,8 @@ def triangular(self, low=0.0, high=1.0, mode=None): http://en.wikipedia.org/wiki/Triangular_distribution - The expected value (mean) and variance of the random variable are: + The mean (expected value) and variance of the random variable are: + E[X] = (low + high + mode) / 3 Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18 @@ -603,7 +605,8 @@ def expovariate(self, lambd=1.0): positive infinity if lambd is positive, and from negative infinity to 0 if lambd is negative. - The expected value (mean) and variance of the random variable are: + The mean (expected value) and variance of the random variable are: + E[X] = 1 / lambd Var[X] = 1 / lambd ** 2 @@ -666,7 +669,8 @@ def gammavariate(self, alpha, beta): pdf(x) = -------------------------------------- math.gamma(alpha) * beta ** alpha - The expected value (mean) and variance of the random variable are: + The mean (expected value) and variance of the random variable are: + E[X] = alpha * beta Var[X] = alpha * beta ** 2 @@ -729,7 +733,8 @@ def betavariate(self, alpha, beta): Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. - The expected value (mean) and variance of the random variable are: + The mean (expected value) and variance of the random variable are: + E[X] = alpha / (alpha + beta) Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1)) @@ -785,7 +790,8 @@ def binomialvariate(self, n=1, p=0.5): Returns an integer in the range: 0 <= X <= n - The expected value (mean) and variance of the random variable are: + The mean (expected value) and variance of the random variable are: + E[X] = n * p Var[x] = n * p * (1 - p)