Monday, April 9, 2012

Expected Wait Time given Multiple Trains

Suppose a train comes every 10 minute, and we go to the train station at a random time. How much time do we expect to wait before the train comes? In this scenario, it's 5 minutes, because the probability is uniformly distributed between 0 and 10 minutes. This situation highly resembles waiting at the 116th Street - Columbia University station for a Downtown 1 train. Sometimes we get lucky and don't wait much; other times, we luck out.

Now suppose we take the train down to 96th Street and want to transfer to the 2 or 3 train. Suppose each 2 train and each 3 train comes every 10 minutes, independent of each other (this is a bit unrealistic, since they can't both come at the same moment on the same tracks). Anyhow, say if our destination is Chambers Street, we don't care whether we take the 2 or the 3 train; we just want to get on the train that comes first. What's the expected wait time here?

The expected time here is the time that elapses when neither the 2 train nor the 3 train comes. Let's backtrack first:
  • Let f(x) be the probability density function of waiting x minutes at the platform. The function f(x) is simply 1/10 for x = [0,10], and 0 for the rest of the values of x.
  • Let F(x) be the cumulative density function, which is simply the antiderivative of f(x) at F(x) = x/10 for x = [0,10]. 
  • In example, F(7) = 70% means that probability that waiting time is 7 min or fewer is 70%. Or in another word, 1-F(7) = 30% means that there's a 30% chance that waiting time is 7 min or greater.
Let G(x) be the waiting time when there are two trains, each with f(x) = 1/10 for x = [0,10]. Then, the term 1-G(x) denotes the probability that neither train comes within x minutes. That happens with neither of the trains comes within x minutes. Since the two trains come independently in this exercise, 1-G(x) = (1-F(x))*(1-F(x)). Using the example of x=7 again, 1-G(x) = (30%)^2, so G(x) = 1-.3^2 = 91%. This means that there's a 91% chance that one of the trains will come within 7 minutes. So we saw that G(x) = 1-(1-F(x))^2. Recall that F(x) = x/10 for x = [0,10]. Therefore, we have G(x) = 1-(1-x/10)^2. Here's the graph of that:

What's the average waiting time? We need the probability density function g(x), which is just the derivative of G(x), and then integrate x*g(x) over the variable x from 0 to 10. Upon executing that command on MATLAB:
syms x;
y = 1-(1-(x/10))^2;
pdf = diff(y,x);
average = int(x*pdf,x,0,10)
The average time comes out to be 10/3 minutes. To verify that, a simulation of 100 million trials was run on MATLAB. The code isn't shown here (maybe shown in a future post), but the result came dead on at 3.3334.

What if there are more than 2 trains? With a frequency that isn't every 10 minutes? No problem. Look back at G(x) = 1-(1-x/10)^2. If we have understood what each number and variable has stood for, we should have no problem deducing that in the general situation with n trains, each independently coming every f minutes, the cumulative density function is G(x) = 1-(1-x/f)^n. To get the average, differentiate G(x) by x to get g(x), and integrate x*g(x) over x from 0 to n. Just for sake of curiosity, suppose it's rush hour at Chambers Street, and we can catch either one of the 1/2/3 trains to go back Uptown. If each train's frequency is 5 minutes, the average expected waiting time, following the formulas investigated here, is only 1.026 minutes.

For reference, the source of inspiration for the investigation of this subject was the countless amount of time spent waiting on the Subway platforms.