From Werner KRAUTH
Revision as of 13:20, 6 June 2024;
view current revision←Older revision |
Newer revision→
import random, math
import matplotlib.pyplot as plt
N_trials = 100000
data = []
lam = 2.7
for iter in range(N_trials):
Upsilon = random.uniform(0.0, 1.0)
#
# This is the sample transformation SMAC eqs (1.30), (1.31)
#
x = -math.log(Upsilon) / lam
data.append(x)
plt.title('exponential random numbers (sample transformation) $\lambda = $ '+ str(lam))
plt.xlabel('$x$')
plt.ylabel('$\pi(x)$')
plt.hist(data, bins=100, density=True,label='data')
XValues = []
YValues = []
for i in range(1000):
x = i / 200.0
XValues.append(x)
YValues.append(lam * math.exp(-lam * x))
plt.plot(XValues, YValues, label='theory')
plt.legend(loc='upper right')
plt.show()