How do I generate discrete random events with a Poisson distribution?

浪子不回头ぞ 提交于 2019-12-09 10:11:46

问题


I'm aware of Knuth's algorithm for generating random Poisson distributed numbers (below in Java) but how do I translate that into calling a method, generateEvent(), randomly over time?

int poissonRandomNumber(int lambda) {
    double L = Math.exp(-lambda);
    int k = 0;
    double p = 1;
    do {
        k = k + 1;
        double u = Math.random();
        p = p * u;
    } while (p > L);
    return k - 1;
}

回答1:


If you are looking to simulate the inter-event arrival time, you want the exponential distribution.

Take a look at Pseudorandom Number Generator - Exponential Distribution

Your code would then look like this:

// Note L == 1 / lambda
public double poissonRandomInterarrivalDelay(double L) {
    return (Math.log(1.0-Math.random())/-L;
}

...

while (true){
    // Note -- lambda is 5 seconds, convert to milleseconds
    long interval= (long)poissonRandomInterarrivalDelay(5.0*1000.0);
    try {
        Thread.sleep(interval);
        fireEvent();
}



回答2:


The Poisson random numbers you are generating, as Scott mentioned, represent the frequency of your events. Once you have the frequency, you can fit their occurrences over the interval using a second distribution, say Uniform.

Suppose the number of events generated for an interval of N is k. Then you simply need to generate (k+1) random numbers that sum to N.

|<----------------------- N ------------------------->|
--r_0--(event)---r_1-..-(event_k)--r_(k+1)--

To do so, simply generate (k+1) random numbers and divide them by their sum, divided by N. The first k of these numbers become the timestamps of your events.



来源:https://stackoverflow.com/questions/2206199/how-do-i-generate-discrete-random-events-with-a-poisson-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!