MCPcopy Index your code
hub / github.com/python/cpython / betavariate

Method betavariate

Lib/random.py:736–766  ·  view source on GitHub ↗

Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. The mean (expected value) and variance of the random variable are: E[X] = alpha / (alpha + beta) Var[X] = alpha * beta / ((alpha + bet

(self, alpha, beta)

Source from the content-addressed store, hash-verified

734 return x * beta
735
736 def betavariate(self, alpha, beta):
737 """Beta distribution.
738
739 Conditions on the parameters are alpha > 0 and beta > 0.
740 Returned values range between 0 and 1.
741
742 The mean (expected value) and variance of the random variable are:
743
744 E[X] = alpha / (alpha + beta)
745 Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1))
746
747 """
748 ## See
749 ## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
750 ## for Ivan Frohne's insightful analysis of why the original implementation:
751 ##
752 ## def betavariate(self, alpha, beta):
753 ## # Discrete Event Simulation in C, pp 87-88.
754 ##
755 ## y = self.expovariate(alpha)
756 ## z = self.expovariate(1.0/beta)
757 ## return z/(y+z)
758 ##
759 ## was dead wrong, and how it probably got that way.
760
761 # This version due to Janne Sinkkonen, and matches all the std
762 # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
763 y = self.gammavariate(alpha, 1.0)
764 if y:
765 return y / (y + self.gammavariate(beta, 1.0))
766 return 0.0
767
768 def paretovariate(self, alpha):
769 """Pareto distribution. alpha is the shape parameter."""

Callers 2

test_zeroinputsMethod · 0.95

Calls 1

gammavariateMethod · 0.95

Tested by 2

test_zeroinputsMethod · 0.76