# Expected Wait Time and Variance in Headway

Consider that there are 6 buses in an hour. The time between the arrival of the next bus is called headway. If there are evenly spaced out, the headway will be 10,10,10,10,10,10 minutes. But in a city with unpredictable traffic, the arrival time is often disturbed. What will be the perceived performance in those situation? For example, if the only second bus is delayed by 5 minutes, the headway will be 10,15,5,10,10,10. Or consider a more extreme case where the last three bus bunches together so that after a long gap, they all arrive together within a minutes. That gives 10,10,10,28,1,1. If we just takes the arithmetic mean, it will be 10 in all cases. This number of course does not describe that disturbance it causes. Instead we will calculate the expected wait time over the entire period of time.

Define $W(t)$ as the wait time when a person arrive the station at time t. The $W(t)$ chart looks like a sawtooth. It is highest when the passenger arrive just after a bus has left, and it will be 0 when the passenger arrive . Here $t_{i}$ is when bus i arrives., $h_{i}$ is the headway to the next bus and $T$ is the sum of $t_{i}$.

The expected wait time is given by this formula

$\int_{0}^{T} W(t)P(t)dt$

$P(t)$ is the probability density function of people arrive at time t. We assume people come uniformly. So $P(t)$ is simply $\frac{1}{T}$.

$\frac{1}{T} \sum_{i=1}^{n} \int_{t_{i-1}}^{t_{i}} W(t)dt$

$= \frac{1}{T} \sum_{i=1}^{n} \frac{ {h_{i}}^{2}}{2}$

$= \frac{ \sum h_{i}^{2}} {2T}$

In general communication, it is often more natural to talk about frequency than expected wait time. For example we often say the bus arrive every 10 minutes. But it requires a little bit of math to translate it to 5 minutes of expected wait time. Since the expected wait time is just half of the headway, we can multiple the formula above by to get a effective frequency. We can implement a simple function to calculate the effective frequency below.

>>> def effective_frequency(times):
...         h2 = sum(t*t for t in times)
...         T = sum(times)
...         return float(h2)/T
...
>>> effective_frequency([10,10,10,10,10,10])
10.0
>>> effective_frequency([10,15,5,10,10,10])
10.833333333333334
>>> effective_frequency([10,10,10,28,1,1])
18.1


As expected the last example give a much worst effective frequency 18.1 minutes than the baseline of 10 minutes.

## Relation with Variance

It is interesting to study the relationship between expected wait time and the variance of the headway. This is given by the formula.

$\sigma^2 = \frac {\sum h_i^2 - (\sum h_i)^2/N}{N}$

$\frac { \sigma^2 N } {T} = \frac {\sum h_i^2}{T} - \frac {T^2/N}{T}$

$\frac { \sigma^2 N } {T} + \frac {T}{N} = \frac {\sum h_i^2}{T}$

$\frac { \sigma^2 }{ \mu } + \mu = \frac {\sum h_i^2}{T}$ where $\mu = \frac{T}{N}$

or $\frac {\sigma^2}{\mu} + \mu$ = 2 × Expected Wait Time = effective frequency

There is a very nice interpretation of effective frequency. First of all it is smallest when $\sigma^2$ is 0. That is when the headway are evenly distributed, it will achieve the optimal frequency μ. Secondly effective frequency is larger than μ by $\frac {\sigma^2}{\mu}$. So the variance can be interpret as the penalty above the baseline frequency. In order to minimize wait time, it is necessary to minimize variance.

One implication is how to mitigate missed run. If it is necessary to skip a bus run due to equipment break down or availability of drivers, it is better to redistribute the remaining run evenly than to simply miss a run.

>>> effective_frequency([10,20,10,10,10])
13.333333333333334
>>> effective_frequency([12,12,12,12,12])
12.0


Using the formula above, dropping a run with headway 10,20,10,10,10 gives an effective frequency of 13.3. This is worst than if we can redistribute the remain 5 buses to get a frequency of 12.