Commit 81fd6ca9 authored by Guido van Rossum's avatar Guido van Rossum

Added gauss() (same as normal but twice as fast) and betavariate();

print more statistics in test_generator()
parent f4784f5a
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
# lognormal # lognormal
# negative exponential # negative exponential
# gamma # gamma
# beta
# #
# distributions on the circle (angles 0 to 2pi) # distributions on the circle (angles 0 to 2pi)
# --------------------------------------------- # ---------------------------------------------
...@@ -15,7 +16,7 @@ ...@@ -15,7 +16,7 @@
# Translated from anonymously contributed C/C++ source. # Translated from anonymously contributed C/C++ source.
from whrandom import random, uniform, randint, choice # Also for export! from whrandom import random, uniform, randint, choice # Also for export!
from math import log, exp, pi, e, sqrt, acos, cos from math import log, exp, pi, e, sqrt, acos, cos, sin
# Housekeeping function to verify that magic constants have been # Housekeeping function to verify that magic constants have been
# computed correctly # computed correctly
...@@ -172,6 +173,37 @@ def stdgamma(alpha, ainv, bbb, ccc): ...@@ -172,6 +173,37 @@ def stdgamma(alpha, ainv, bbb, ccc):
break break
return x return x
# -------------------- Gauss (faster alternative) --------------------
# When x and y are two variables from [0, 1), uniformly distributed, then
#
# cos(2*pi*x)*log(1-y)
# sin(2*pi*x)*log(1-y)
#
# are two *independent* variables with normal distribution (mu = 0, sigma = 1).
# (Lambert Meertens)
gauss_next = None
def gauss(mu, sigma):
global gauss_next
if gauss_next != None:
z = gauss_next
gauss_next = None
else:
x2pi = random() * TWOPI
log1_y = log(1.0 - random())
z = cos(x2pi) * log1_y
gauss_next = sin(x2pi) * log1_y
return mu + z*sigma
# -------------------- beta --------------------
def betavariate(alpha, beta):
y = expovariate(alpha)
z = expovariate(1.0/beta)
return z/(y+z)
# -------------------- test program -------------------- # -------------------- test program --------------------
def test(): def test():
...@@ -179,7 +211,7 @@ def test(): ...@@ -179,7 +211,7 @@ def test():
print 'LOG4 =', LOG4 print 'LOG4 =', LOG4
print 'NV_MAGICCONST =', NV_MAGICCONST print 'NV_MAGICCONST =', NV_MAGICCONST
print 'SG_MAGICCONST =', SG_MAGICCONST print 'SG_MAGICCONST =', SG_MAGICCONST
N = 100 N = 200
test_generator(N, 'random()') test_generator(N, 'random()')
test_generator(N, 'normalvariate(0.0, 1.0)') test_generator(N, 'normalvariate(0.0, 1.0)')
test_generator(N, 'lognormvariate(0.0, 1.0)') test_generator(N, 'lognormvariate(0.0, 1.0)')
...@@ -192,21 +224,30 @@ def test(): ...@@ -192,21 +224,30 @@ def test():
test_generator(N, 'gammavariate(2.0, 1.0)') test_generator(N, 'gammavariate(2.0, 1.0)')
test_generator(N, 'gammavariate(20.0, 1.0)') test_generator(N, 'gammavariate(20.0, 1.0)')
test_generator(N, 'gammavariate(200.0, 1.0)') test_generator(N, 'gammavariate(200.0, 1.0)')
test_generator(N, 'gauss(0.0, 1.0)')
test_generator(N, 'betavariate(3.0, 3.0)')
def test_generator(n, funccall): def test_generator(n, funccall):
import sys import time
print '%d calls to %s:' % (n, funccall), print n, 'times', funccall
sys.stdout.flush()
code = compile(funccall, funccall, 'eval') code = compile(funccall, funccall, 'eval')
sum = 0.0 sum = 0.0
sqsum = 0.0 sqsum = 0.0
smallest = 1e10
largest = 1e-10
t0 = time.time()
for i in range(n): for i in range(n):
x = eval(code) x = eval(code)
sum = sum + x sum = sum + x
sqsum = sqsum + x*x sqsum = sqsum + x*x
smallest = min(x, smallest)
largest = max(x, largest)
t1 = time.time()
print round(t1-t0, 3), 'sec,',
avg = sum/n avg = sum/n
stddev = sqrt(sqsum/n - avg*avg) stddev = sqrt(sqsum/n - avg*avg)
print 'avg %g, stddev %g' % (avg, stddev) print 'avg %g, stddev %g, min %g, max %g' % \
(avg, stddev, smallest, largest)
if __name__ == '__main__': if __name__ == '__main__':
test() test()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment