Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 85e2e47

Browse files
committed
SF bug 130030: Claim of bad betavariate algorithm.
1 parent 2786543 commit 85e2e47

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

Lib/random.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,27 @@ def gauss(self, mu, sigma):
461461
return mu + z*sigma
462462

463463
## -------------------- beta --------------------
464+
## See
465+
## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
466+
## for Ivan Frohne's insightful analysis of why the original implementation:
467+
##
468+
## def betavariate(self, alpha, beta):
469+
## # Discrete Event Simulation in C, pp 87-88.
470+
##
471+
## y = self.expovariate(alpha)
472+
## z = self.expovariate(1.0/beta)
473+
## return z/(y+z)
474+
##
475+
## was dead wrong, and how it probably got that way.
464476

465477
def betavariate(self, alpha, beta):
466-
467-
# Discrete Event Simulation in C, pp 87-88.
468-
469-
y = self.expovariate(alpha)
470-
z = self.expovariate(1.0/beta)
471-
return z/(y+z)
478+
# This version due to Janne Sinkkonen, and matches all the std
479+
# texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
480+
y = self.gammavariate(alpha, 1.)
481+
if y == 0:
482+
return 0.0
483+
else:
484+
return y / (y + self.gammavariate(beta, 1.))
472485

473486
## -------------------- Pareto --------------------
474487

Misc/ACKS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Sebastian Fernandez
118118
Nils Fischbeck
119119
Doug Fort
120120
Robin Friedrich
121-
Ivan Frohe
121+
Ivan Frohne
122122
Jim Fulton
123123
Geoff Furnish
124124
Tadayoshi Funaba
@@ -338,6 +338,7 @@ Joel Shprentz
338338
Eric Siegerman
339339
Paul Sijben
340340
Nathan Paul Simons
341+
Janne Sinkkonen
341342
George Sipe
342343
Kragen Sitaker
343344
Rafal Smotrzyk

0 commit comments

Comments
 (0)