[Math] Application of Exponential Distribution

normal distributionprobabilityprobability distributions

I'm in the process of developing a traffic simulation using Discrete Event Simulation approach.

So I have the core stuff working but I need to have some sort of distribution to tell the simulation how many vehicles to be generated for instance per minute. Mathematics isn't exactly my strong side but so far I've figured that I need to implement a negative exponential distribution.

What I want is to have vehicles or let's simply call them events in this case, to be generated with some distribution which I think (correct me if I'm wrong please) I can use the exponential distribution for my needs here.

The problem I'm having (a quite big problem) is that I don't understand how it works. Researching about it seems extremely overwhelming and I end up getting confused. So I obviously need to understand it before I can implement it.

Could someone be so kind and try and explain to me how it works. I understand so far that it tells us for example if we know that there are two cars passing a certain point on a road every minute then we can calculate the probability for how long we have to wait until the next car arrives?

Also any material that you think I should read and lastly whether using the exponential distribution is suitable in this case.

Best Answer

Yes, you can use the exponential distribution to model the time between cars: set it up with the appropriate rate (2 cars/min or 20 cars/min or whatever) and then do a cumulative sum (cumsum in R) to find the time in minutes at which each car passes. Here's an example in R:

> waits <- rexp(10,2)
> waits
 [1] 0.14713730 0.26549532 0.83353238 0.19284503 0.30513264 0.62242778
 [7] 0.01943296 0.25699842 0.40801597 0.31635216
> cumsum(waits)
 [1] 0.1471373 0.4126326 1.2461650 1.4390100 1.7441427 2.3665704 2.3860034
 [8] 2.6430018 3.0510178 3.3673699

Here we have an average of two cars per minute. The first comes at about .15 minutes, the second at .26 minutes after that (i.e., at .41 minutes), and so on. The tenth one comes at 3.36 minutes.

There's another way that doesn't require doing the cumulative sum, though, which may be easier: a Poisson process, which directly generates the times at which cars come. To do it this way, you first sample the total number of cars $n$ that you see in the time interval from a Poisson distribution with parameter Rate*Time (e.g. if you have 2 cars per minute, and your time interval is an hour, your parameter will be 2*60 = 120), and then sample $n$ points from a uniform distribution on the time interval. The result ends up being the same either way. If you want, you can then calculate the waiting times (what you sampled from an Exponential distribution using the first method) by taking the differences between the ordered samples (diff in R will do this for you).

Here's an example of the alternative approach:

> n <- rpois(10,2*5)
> runif(n,0,5)
 [1] 4.3983792 2.0030962 3.6927402 1.5854187 0.3782581 2.0634806 1.0005454
 [8] 0.1659071 2.4213297 3.5768906
> diff(c(0,sort(a)))
 [1] 0.16590711 0.21235101 0.62228724 0.58487331 0.41767751 0.06038446
 [7] 0.35784905 1.15556095 0.11584959 0.70563900

Here, we first sampled $n$, which turned out to be nine, so we're simulating seeing 9 cars in the first 5 minutes (2 cars per minute average). Then we sample the times uniformly from 0 to 5, getting the vector of times. Note that they aren't in order. If we want to get the waiting times back, then we can run the line of code beginning with diff.

Related Question