# Thread: Programming Poisson Distribution :: C++

1. ## Programming Poisson Distribution :: C++

Hi.

I would like to design a function that will return a random number based on Poisson Distribution. Here is an example of a problem I want to solve using this function.

Given: One car passes through an intersection every 5 minutes.

Question: Determine the time of arrival of each car based on the time of the last car in 30 days. For example:

Start: t = 0
...
car1: t += 4 -> (t would then equal 4 : t = 4)
car2: t += 4.8 -> (t = 8.8)
car3: t += 3.2
car4: t += 4.6
car5: t += 5.3
car6: t += 4.9
...

Given a start time of 0, I want this function to calculate the arrival time of every car from there on relative to the previous car. This is based on Poisson Distribution.

Please post if you have any idea on this problem.

Thanks,
Kuphryn

2. A lot of people might be familiar with Poisson's Distribution - much like me. Those with some statistics knowledge might want a short explanantion : go to http://stat.tamu.edu/stat30x/notes/node70.html .

If my understanding of the problem is accurate (maybe it is not ; sorry then), here's how I'd do it :

1. Start at t=0.
2. Calculate probability for a set number of points, say numerous points starting your initial time, at a 0,1 interval (since times had a precision of 0.1 in your example problem).
Ex.: the formula is P(x) = u^x e^-u / x!. In this case, u = 5. So you calculate for x = 0, x = 1, etc. , until the total probability would get near 1 (getting to 0,999999 should be enough).
3. Get yourself a good pseudorandom number generator. Get a random number between 0 and 1. Interpolate the result with answers found in 2. The nearest value to that number would then be your time.
4. Get back to 2., using the t you found as your new initial time, determining new intervals. (Yep, recursive function).

Hope I could be of any help. If I was mistaken on that, please disregard my post ; I know nothing on Poisson's distributions, only what I read from your post and that site

Good luck.

3. Okay. Thanks.

I understand your algorithm up to step 3. What do you mean "interpolate" using a random number? Why is that necessary?

Kuphryn

4. What I meant is that the pseudorandom number you will find will most likely not be exactly one of your t's. You will need to find the value that is closer to the value you get. I admit that that particular point was not what I would call 'clear'.

Ex.: t = 0 has P(t) = 0.006738.
t=0.1 has P(t) = 0.007915, for a total probability of 0.01465. So, if that random number was, say, 0.006849, its corresponding (i.e. interpolated) probability would be in the range of t=0.1 . If it was higher than the total probability, then t > 0.1.

It is necessary to use random numbers because data is not homogenous (i.e. cars don'r arrive at regular intervals).

Hope it cleared things.