Python How to increase the acceptance ratio

AI Thread Summary
The discussion centers around generating a random distribution using the exponential function λe^(-λy) and implementing it in Python with NumPy and Matplotlib. The user shares code for generating random samples and creating histograms to visualize the distribution. They express a desire to improve the acceptance ratio in their sampling method by using a linear function f(x) = 1 - ax, inquiring about the optimal choice for 'a'. Additionally, there is a mention of a mathematical concept regarding the relationship between flat distributions and the function f, suggesting a deeper theoretical aspect of the problem. The thread concludes with a note that the question has been deemed a duplicate and is directed to a homework forum for further assistance.
Othman0111
Messages
27
Reaction score
0
I trying to generate a random distribution

λ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?
 
Technology news on Phys.org
This homework ? Then please post in the homework forum.

Do you know that if ##F'=f## then a flat distribution of F yields a distribution like ##f## ?
 
BvU said:
This homework ? Then please post in the homework forum.

Do you know that if ##F'=f## then a flat distribution of F yields a distribution like ##f## ?
I didn't get what you're saying
 
Thread closed. There's a duplicate of the question here: https://www.physicsforums.com/threads/how-to-increase-the-acceptance-ratio-in-python.966421/
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
1
Views
1K
Replies
6
Views
3K
Back
Top