How to simulate Poisson arrival?
Asked Answered
N

3

5

I want to generate events that "arrive" with "t" seconds as mean inter-arrival delay? Starting at time 0, how do I generate the times when the event occurs? Basically, I want to generate sequence of times such as t1, t2, t3, ... when the event occurs. How do I write such a function?

Thank you.

Nimitz answered 28/2, 2011 at 23:5 Comment(1)
See the discussion here: linkDebarath
M
3

You don't say what language - but take a look at Generate (Poisson?) random variable in real-time

Mayes answered 28/2, 2011 at 23:10 Comment(0)
T
3

The easiest solution is to compute the time of the next event based on the "L" inter-arrival delay. This is based on the cumulative distribution function for the exponential: F(x) = 1 - e**(-lambda * x) where lambda is 1/L, the mean time, and x is the amount of time.

This can be solved for x and fed with a uniform random number:

x = -ln(1-U)/lambda where U is a random value 0..1.

From the link 1:

#include <math.h> 
#include <stdlib.h>

float nextTime(float rateParameter) {
  return -logf(1.0f - (float) random() / (RAND_MAX + 1)) / rateParameter;
}

This link provides a lot of information on how to do it plus examples in How to Generate Random Timings for a Poisson Process

Note that there are other probability distribution functions that can be used for event generation (uniform, triangle, etc.). Many of these can be generated either by code from Boost or by using GNU Scientific Library (GSL).

So to compute times of events: next_event = time() + nextTime(D); following_event = next_event + nextTime(D);

If events have a duration, the duration can be another, independent Poisson distribution, random distribution, fixed interval, etc. However, will need to check that the interval to the next event is not shorter than the duration of the event you are simulating:

deltaT = nextTime(MEAN_EVT);
dur    = nextTime(MEAN_DUR);
if (deltaT <= dur) {
  // either fix duration or get another event....
}
Thermoelectricity answered 14/8, 2013 at 16:59 Comment(0)
T
3

Python contains random.expovariate which makes this very easy in Python. For example to create 10 samples:

import random
random.expovariate(0.2) for i in range(10)]

Typically, this will be converted to integer:

import random
[int(random.expovariate(0.2)) for i in range(10)]

Thanks to this link.

Thermoelectricity answered 5/2, 2015 at 15:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.