开发者

How to simulate Poisson arrival?

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 t开发者_JAVA技巧he 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.


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


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....
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜