Simulating Poisson Waiting Times
Asked Answered
H

2

7

I need to simulate Poisson wait times. I've found many examples of simulating the number of arrivals, but I need to simulate the wait time for one arrival, given an average wait time.

I keep finding code like this:

public int getPoisson(double lambda) 
{   
    double L = Math.exp(-lambda);   
    double p = 1.0;   
    int k = 0;   

    do 
    {    
        k++;     
        p *= rand.nextDouble(); 
        p *= Math.random(); 
    } while (p > L);   

    return k - 1; 
} 

but that is for number of arrivals, not arrival times.

Efficieny is preferred to accuracy, more because of power consumption than time. The language I am working in is Java, and it would be best if the algorithm only used methods available in the Random class, but this is not required.

Hebraist answered 29/6, 2011 at 21:15 Comment(2)
The easiest thing is to simulate inter-arrivals: https://mcmap.net/q/832273/-how-do-i-generate-discrete-random-events-with-a-poisson-distributionSoloist
You should for sure rely on well known implementations. Check this out: http://maths.uncommons.org/api/org/uncommons/maths/random/PoissonGenerator.html.Croze
O
6

Time between arrivals is an exponential distribution, and you can generate a random variable X~exp(lambda) with the formula:

-ln(U)/lambda` (where U~Uniform[0,1]). 

More info on generating exponential variable.

Note that time between arrival also matches time until first arrival, because exponential distribution is memoryless.

Ogilvie answered 29/6, 2011 at 21:23 Comment(3)
Thanks, but something isn't right; When I call this with 1/100, I get values of Infinity every time. The code is public static double waitingTime(double lambda) { return -Math.log(rand.nextDouble())/lambda; } Shouldn't it be valid to say that less than one arrival is expected per unit time? Or do I put in 1 and multiply by the expected wait time?Hebraist
@Alex: I am only 99% sure, but I think in exponential distribution, lamda is number of occurences per time unit, if you have your average waiting time, you should set lamda=1/average waiting time, could that be the problem?Ogilvie
Nevermind, I always make this mistake. I called the method with 1/100 instead of 1.0/100.0 This answer works. Thank you so much, I've been reading stackoverflow for a long time, and this is my first time posting, and I'm amazed at how fast and accurate your answer was.Hebraist
S
0

If you want to simulate earthquakes, or lightning or critters appearing on a screen, the usual method is to assume a Poisson Distribution with an average arrival rate λ.

The easier thing to do is to simulate inter-arrivals:

With a Poisson distribution, the arrivals get more likely as time passes. It corresponds to the cumulative distribution for that probability density function. The expected value of a Poisson-distributed random variable is equal to λ and so is its variance. The simplest way is to 'sample' the cumulative distribution which has an exponential form (e)^-λt which gives t = -ln(U)/λ. You choose a uniform random number U and plug in the formula to get the time that should pass before the next event. Unfortunately, because U usually belongs to [0,1[ that could cause issues with the log, so it's easier to avoid it by using t= -ln(1-U)/λ.

Sample code can be found at the link below.

https://mcmap.net/q/832273/-how-do-i-generate-discrete-random-events-with-a-poisson-distribution

Soloist answered 9/3, 2013 at 5:9 Comment(1)
Hi, I need to generate random numbers in Poisson interval rate using java.. I tried using your function and method poissonRandomInterarrivalDelay always returns zero for any value of lambda.Sudderth

© 2022 - 2024 — McMap. All rights reserved.