Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let me preface all this by the fact that none of this is truly random, I am talking about pseudo random number generators.</p> <p>Let me also say that I have never had to do this for production quality code. I have done this for a hw assignment though, in Python. I simulated Poisson random variables.</p> <p>The way that I did it made use of the following facts: </p> <ol> <li>A Poisson random variable is a sum of exponential random variables.</li> <li>We can use the inverse transform method to generate exponential random variables. <a href="http://en.wikipedia.org/wiki/Inverse_transform_sampling" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Inverse_transform_sampling</a>.</li> </ol> <p>In particular, you can use the fact that: if X<sub>1</sub>, ..., X<sub>n</sub> are independent <em>standard</em> exponential random variables, then Z = min(k : X<sub>1</sub> + ... + X<sub>k</sub> &lt; &lambda;) - 1 is Poisson(&lambda;).</p> <p>So, with that, I wrote the following code in python to generate Poisson values:</p> <pre><code>class Poisson: """Generate Poisson(lambda) values by using exponential random variables.""" def __init__(self, lam): self.__lam = lam def nextPoisson(self): sum = 0 n = -1 while sum &lt; self.__lam: n += 1 sum -= math.log(random.random()) return n </code></pre> <p>Example usage of the class is:</p> <pre><code># Generates a random value that is Poisson(lambda = 5) distributed poisson = Poisson(5) poisson_value = poisson.nextPoisson </code></pre> <p>I posted this here because it is good to know that these kinds of relationships exist, and this inverse transform method gives you a general way to deal with generating random values following a particular continuous distribution.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload