I can't solve the following exercise:
A random number generator generates random values $U \sim \text{U}(0,1)$ from the standard uniform distribution. Use $U$ to generate a random variable $P \sim \text{Pois}(\lambda = 5)$ from a Poisson distribution with rate parameter equal to five.
Comment: In previous tasks I was asked to use $U$ to generate an exponential random variable $E \sim \text{Exp}(\lambda)$. The solution was to take $E \equiv -\tfrac{1}{\lambda} \ln(1-U)$. I think that this can be helpful because of the relation between Poisson distributions and exponential, but I'm not sure.
$\endgroup$ 52 Answers
$\begingroup$There are many ways to do this; some of which are more computationally efficient than others. If you want to use a single generated value $U \sim \text{U}(0,1)$ then you can use inverse transform sampling using the cumulative distribution function for the Poisson distribution. This gives the output:
$$P \equiv \min \Bigg\{ p =0,1,2,... \Bigg| U \leqslant \exp(-\lambda) \sum_{i=0}^p \frac{\lambda^p}{p!} \Bigg\}.$$
Alternatively, if you are willing to use multiple independent generated values $U_1,U_2,U_3, ... \sim \text{IID U}(0,1)$ then you can use the fact that a Poisson random variable with parameter $\lambda$ is given by the number of sequential events occurring in time $\lambda$ where the times between the events are independent exponential random variables with unit rate. Applying this relationship yields the alternative method:
$$P \equiv \min \Bigg\{ p =0,1,2,... \Bigg| - \sum_{i=1}^p \ln(1-U_i) \leqslant \lambda \Bigg\}.$$
In both cases you can program these with a simple while loop in an appropriate computational platform with a uniform pseudo-random number generator.
You can draw exponentials with mean one. These are the wait times of a Poisson process with rate one. The number of arrivals within time interval of one is Poisson with mean one. So draw exponentials and add them until he sum exceeds one. The total number of times you drew before this happened is going to be Poisson.
So, for instance, if the first exponential is greater than one, then you output zero. If the first exponential is .25 and the second is .8, then you stop cause the total is greater than one and output 1. If the first is .23, the second is. 5, and the third is .3, then you output 2. And so on.
$\endgroup$