为了利用NumPy的速度,您需要使用大型数组,而不是在循环中处理单个标量。例如,你可以生成
N
randous = np.random.uniform(0, 1, size=N)
randohs = c * np.random.exponential(0.2, size=N)
然后像这样选择那些通过过滤器的:
mask = randous <= f(randohs) / g(randohs)
return randohs[mask]
唯一的问题是不能保证
randohs[mask]
具有所需数量的值(或任何值)。所以我们可能要重复这个直到我们产生足够的样本:
while len(samples) < N:
randohs = generate_samples()
samples.extend(randohs)
samples = samples[:N]
import numpy as np
from scipy.special import gamma
import matplotlib.pyplot as plt
def f(x):
return 1. / gamma(3) * x * np.exp(-1 * x)
def g(x):
return c * lambd * np.exp(-1 * lambd * x)
def generate_samples(N=10**5):
randous = np.random.uniform(0, 1, size=N)
randohs = c * np.random.exponential(0.2, size=N)
mask = randous <= f(randohs) / g(randohs)
return randohs[mask]
lambd = .2
c = (1 / lambd / gamma(3) * (2./(1-lambd)) ** 2
* np.exp(-1 * (1 - lambd) * (2. / (lambd - 1))))
x = np.linspace(0, 50, 1000)
samples = []
N = 10**5
while len(samples) < N:
randohs = generate_samples()
samples.extend(randohs)
samples = samples[:N]
plt.hist(samples, 100, density=True, label='Simulated PDF')
plt.plot(x, f(x), label='True PDF', lw=2)
plt.xlim(0, 10)
plt.show()