- #1
Othman0111
- 27
- 0
I trying to generate a random distribution
λe-λy
Which distributed exponentially.
I want to Improve the acceptance ratio by using a linear function f(x)=1-ax. is there a certain choice for a?
λe-λy
Which distributed exponentially.
Python:
%matplotlib inline
import numpy as np
from matplotlib import pyplot
N = 1000
r = np.random.random(N)
xlambda = 0.1
x = -np.log(r)/xlambda
binwidth=xlambda*5
pyplot.hist(x,bins=np.arange(0.,100., binwidth),density=True);
pyplot.plot(np.arange(0.,100.,binwidth),xlambda*np.exp(-xlambda*np.arange(0.,100.,binwidth)),ls='-',c='red',lw=3);N = 10000
xmax = 100
ymax = xlambda
rx = np.random.random(N)*xmax
ry = np.random.random(N)*ymax
values = []
Nin = 0
for i in range(N):
if(ry[i] <= xlambda*np.exp(-xlambda*rx[i])):
# Accept
values.append(rx[i])
Nin += 1
x = np.asarray(values)
print("Acceptance Ratio: ",Nin/float(N))
binwidth=xlambda*5
pyplot.hist(x,bins=np.arange(0.,100., binwidth),density=True);
pyplot.plot(np.arange(0.,100.,binwidth),xlambda*np.exp(-xlambda*np.arange(0.,100.,binwidth)),ls='-',c='red',lw=3);
I want to Improve the acceptance ratio by using a linear function f(x)=1-ax. is there a certain choice for a?