summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2001-01-26 06:49:56 (GMT)
committerTim Peters <tim.peters@gmail.com>2001-01-26 06:49:56 (GMT)
commit85e2e4742d0a1accecd02058a7907df36308297e (patch)
tree3ef4623379dca2e3ba68fbcb368f01c1073957f2
parent2786543b705af6d7f091edcb66f1855488a6cb42 (diff)
downloadcpython-85e2e4742d0a1accecd02058a7907df36308297e.zip
cpython-85e2e4742d0a1accecd02058a7907df36308297e.tar.gz
cpython-85e2e4742d0a1accecd02058a7907df36308297e.tar.bz2
SF bug 130030: Claim of bad betavariate algorithm.
-rw-r--r--Lib/random.py25
-rw-r--r--Misc/ACKS3
2 files changed, 21 insertions, 7 deletions
diff --git a/Lib/random.py b/Lib/random.py
index d098d39..a22449c 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -461,14 +461,27 @@ class Random:
return mu + z*sigma
## -------------------- beta --------------------
+## See
+## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
+## for Ivan Frohne's insightful analysis of why the original implementation:
+##
+## def betavariate(self, alpha, beta):
+## # Discrete Event Simulation in C, pp 87-88.
+##
+## y = self.expovariate(alpha)
+## z = self.expovariate(1.0/beta)
+## return z/(y+z)
+##
+## was dead wrong, and how it probably got that way.
def betavariate(self, alpha, beta):
-
- # Discrete Event Simulation in C, pp 87-88.
-
- y = self.expovariate(alpha)
- z = self.expovariate(1.0/beta)
- return z/(y+z)
+ # This version due to Janne Sinkkonen, and matches all the std
+ # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
+ y = self.gammavariate(alpha, 1.)
+ if y == 0:
+ return 0.0
+ else:
+ return y / (y + self.gammavariate(beta, 1.))
## -------------------- Pareto --------------------
diff --git a/Misc/ACKS b/Misc/ACKS
index 753cc77..9568547 100644
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -118,7 +118,7 @@ Sebastian Fernandez
Nils Fischbeck
Doug Fort
Robin Friedrich
-Ivan Frohe
+Ivan Frohne
Jim Fulton
Geoff Furnish
Tadayoshi Funaba
@@ -338,6 +338,7 @@ Joel Shprentz
Eric Siegerman
Paul Sijben
Nathan Paul Simons
+Janne Sinkkonen
George Sipe
Kragen Sitaker
Rafal Smotrzyk