Use poisson distribution to generate random discrete events over a time for simulation
05 Feb 2020
Reading time ~3 minutes
Poisson distribution:
the probabily mass function of discrete random variable \(X\) having a poisson distribution:
\(f(k;\lambda) = \Pr(X = k) = \frac{\lambda^{k}e^{-\lambda}}{k!}\)
where \(e\) is Euler’s number, \(k = 0,1,2,...\) , \(\lambda > 0\). and \(\lambda = E(X) = Var(X)\).
Some key notes:
- k is the number of occurence of events occured in an interval, k is 0,1,2…
- the events are all independent, the occurence of one event will not affect the probability of another event occur
- Two events will not occur at the same time.
Understanding implementation in Discrete Events Simulation:
For Discrete Events Simulations, as we would like to randomly generate discrete and independent events, poisson is a very good fit.
For example, I want to simulate a car flow, to see at certain traffic flow rate (how many cars can pass in an unit time), how the traffic will be. Before we getting to any queue model or other simulation models, we need to fisrt generate some timestamps for each car’s arrival time. Poisson distribution can help on this step!
We could derive the \(\lambda\) from this traffic flow rate. E.g. 500 car per hour. which is about 0.137 car per second.
And poisson distribution will gave us the probability distribution of \(k = 0,1,2 ...\) events happening in each time interval that for this \(\lambda\). This is exactly what we want to generate the events happening.
I write this function with numpy
package for the implementation:
And the outputs are:
Based on input parameters [500 events , 1 hr], expected rate is 0.139 events/second,
Sampled 510 events based on poisson process within time 1 hr,sample mean is 0.142 events/second, sample var is 0.142
saving ... 500_plot plot ...
Based on input parameters [50000 events , 1 hr], expected rate is 13.889 events/second,
Sampled 50101 events based on poisson process within time 1 hr,sample mean is 13.917 events/second, sample var is 13.500
saving ... 50000_plot plot ...
Based on input parameters [5000000 events , 1 hr], expected rate is 1388.889 events/second,
Sampled 5000949 events based on poisson process within time 1 hr,sample mean is 1389.152 events/second, sample var is 1337.519
saving ... 5000000_plot plot ...
As can be seen from these histograms, as N increases,while t stays the same, thus \(\lambda\) is increasing, the distribution is more close to normal, but probability of K is actually lower (compare the count over the N). Just like the probabilily distribution cited from the wikipedia page here:
And our program validated that the generated events mean and variance are very close to our expectation \(\lambda\).
In this way we can generate random discrete events using poisson distribution.
Copyright 2019 Muyang Guo