There is something beautiful about a journey on public transport. Every individual you come across in the train carriage has their own destination, both in life and geographically, and yet you are all commuting together on the same train.
However, that community spirit shatters the moment the train arrives. Everyone piles onto the platform, ready to complete their own individual commute, and starts hollering for a taxi. In a world where cities are expanding and more and more taxi drivers are available, your next ride is only a few taps and eight minutes away.
While waiting outside the station I found myself wondering — how many taxis serve the city in total? I started to jot down the license plates of the ones coming and going through the station and noticed each plate had one number, different from the rest, uniquely identifying its taxi.
Of this small subset, one had a number larger than the rest, and so I knew there were at least this many roaming around. But this had to be an underestimate: it’s a very small chance that I would have seen the latest release on this arbitrary day, and so there were higher-numbered cabs that weren’t in my sample.
My eight minutes were up and I continued my journey in taxi number 17. Once I arrived back home I sat down and thought about this more. How many taxis actually are there in the city? And how can I figure this out just using the plate numbers I saw?
Sample too small? Fuhgeddaboudit
Let’s start off with $N$ unique cabs serving the city numbered $1, 2, 3, \dots, N$. Our goal is to estimate the value of $N$ using what we know about some smaller sample of taxis of size $k < N$. To make this problem easier we can assume that each cab is equally likely to drive past us. This isn't exactly realistic, given that 1) we were originally stood at a train station and taxis are almost certainly disproportionately serving this side of town and 2) cabs based near a particular street will turn up far more often. Let's pretend that we take our sample from some randomly chosen street corner; we also assume that the manufacturers numbered their cabs in sequential order as they were produced.
We established that taking the maximum number seen in the sample will undershoot the real population, so how much should we add on to get our estimate?

Above we have a sample size of 5 with a maximum taxi number 17; in general we’d have a sample of size $k$ whose maximum value is $m$. The $k$ taxis split up the number line into $k+1$ gaps in total: $k$ below the maximum $m$ and one unknown between $m$ and $N$. We know the size of the $k$ gaps between the sampled taxis, and there is no particular reason why the size of the last gap should be significantly different, so we can estimate the size of this last gap using the $k$ we’ve already seen!
Since the maximum value in our set is $m$, we know for certain that taxis $1$ to $m$ all exist. However, only $k$ were sampled, leaving at least $m-k$ unspotted taxis driving around somewhere else in the city. It doesn’t matter where they are geographically; what does matter is that these missing cabs will have serial numbers spread across the $k$ gaps created by the sample. The average gap size below $m$ is then $$\frac{m-k}{k} = \frac{m}{k} – 1.$$ We can use this to estimate the size of the unknown gap between $m$ and $N$, so to find an estimate for the total number of taxis $N$ then we can add this onto the largest number we observed. If we call this estimated total $\hat{N}$, our estimator is $$\hat{N} = m + \left(\frac{m}{k}-1\right) = \frac{m(k+1)}{k} – 1.$$ As this estimator scales as $m/k$, a small sample leaves bigger gaps and larger uncertainty for the total number of taxis. Conversely, a larger sample reduces the gap sizes and will provide a more accurate estimate for $\hat{N}$.
“Eyy, I’m chalkin’ ‘ere!”
What we did above satisfied our intuition, but there is much left to be desired in terms of a rigorous proof. It turns out that we can obtain the same answer using techniques from probability!
First, we can reframe our problem slightly. Instead of trying to find the total number of taxis serving the city, we aim to find the expected value of the sample maximum. For any sample of size $k$ we may treat its maximum value as a random variable $M$ that takes values between $k$ and $N$. The expected value of this random variable is $$\mathbb{E}[M] = \sum_{m=k}^{N} m \cdot \mathbb{P}(M=m):$$ the sum of each possible sample maximum multiplied by how likely it is to occur. Our next step is to figure out what these probabilities actually are.
In a sample of size $k$, $k$ random cabs can be chosen out of the $N$ total that exist. We have $$\text{number of possible samples} = {N\choose k}.$$ We can then think about the number of ways to choose a sample whose maximum is exactly $m$. We know that $m$ must exist in this sample and that the other $k-1$ taxis must be chosen from the cabs numbered from one to $m-1$. We can count, $$\begin{array}{c}\text{number of samples}\\ \text{whose maximum is }m\end{array}\!=\, {m-1\choose k-1}.$$ Therefore, the probability that the maximum of a random sample is exactly $m$ is $$\mathbb{P}(M=m) = \frac{{m-1\choose k-1}}{{N\choose k}}.$$ This gives us an analytic form for the expectation: $$\mathbb{E}[M] = \sum_{m=k}^{N} m \cdot \frac{{m-1\choose k-1}}{{N\choose k}} = \frac{k(N+1)}{k+1}.$$ OK, you’ll have to trust me that the last part is correct… the actual step-by-step simplification of this is more involved but is too long for this article so I’ll leave it for you to do while waiting for your next taxi.
Since our actual goal is to find an estimator for the total number of taxis in the city, we can rearrange the above for $N$, to give $$ N = \frac{\mathbb{E}[M] (k+1)}{k} – 1.$$ Now if we can estimate $\mathbb{E}[M]$, we can find an estimator for $\hat{N}$. If we had multiple samples (say, we asked all our friends to stand on different street corners and write down $k$ taxi numbers), we’d have to be careful; but for one sample, like the one we took earlier, the best replacement we have for the expected value of the maximum is just $m$, leaving us with an estimator $\hat{N}$ in the form $$\hat{N} = \frac{m(k+1)}{k} – 1.$$ This is the exact same formula as before! Isn’t that neat?
BEC: best estimate coder
This formula works on paper, but does it work in practice? To test this, I programmed a simulation that takes a known total $N$ value (in this case, 1000) and draws thousands of random samples of varying sizes. For each of these the estimator formula was applied. We can then see how the calculated estimate varies around on the true value $N$.
The graph below represents a simulation where the true number of cabs is 1000 and we happen to spot 20 cabs from this total at random. After it runs 10,000 times, we can plot two things: the frequency of each maximum $m$ and its corresponding estimator total $\hat{N}$.

Look at the beautiful histograms! The blue-dashed line is the mean over all estimators, $\overline{N}$, and the orange-dashed line is the mean over all maxima, $\overline{m}$. Our values for $\hat{N}$ stop abruptly at $N + m/k – 1$. This makes sense as $m$ can never be bigger than $N$ (since taxi number 1001 does not exist). The distance between our two lines is $m/k – 1 =49$, representing the `final gap’ correction that we add on to $m$ to get our estimator.

Simulation results averaged over 10,000 runs
In the table above, we can really see the size of this gap shrinking as $k$ increases: even looking out for 10 taxis instead of five is enough to move the mean of $m$ from around 830 to around 910.
However, we should also think about the standard deviation of the data collected, to see how widely spread our results will be. We see that it shrinks proportionally to $1/k$, since a larger sample will reduce uncertainty.

Historical evidence
While estimating the number of taxis is a fun train-station diversion, this method has actually had a much more crucial application. For Britain in the second world war, knowing how many tanks the Germans had was crucial for planning invasions and preparing for counterattacks. However, the only information the Brits had to work with were the tanks they had already seized in battle.
One approach was to use conventional intelligence techniques (spies, intercepted reports etc) to gain extra information. The other approach was to keep track of the serial numbers, just as we did with our taxi plates.
As it turned out, the mathematical approach dominated in accuracy by a margin that is almost embarrassing for the intelligence forces. For example, in August 1941 the intelligence estimate was that the Germans had produced 1500 tanks. Using our statistical approach we get the rather smaller estimate of 321. In reality there were 342 German tanks at this time — we come pretty close to the right answer!
This became a well-known problem called the German tank problem. The method we have shown to solve it is good, but an equally valid way to approach this problem is via a Bayesian method, where a continuous probability distribution over the total value $N$ is used rather than a single estimate. Trying to compress this entire method into the end of an article would be an injustice, so I recommend finding A Bayesian treatment of the German tank problem by Cory Simon and giving it a read.
When you next end up at a train station or a New York City cafe, you can now try this method out for yourself and try to figure out how many taxis are actually serving the city. Spoiler alert: it will be a lot! And if you’re a taxi driver reading this, you can drive around and play this game and almost certainly get a better estimate than the rest of us!







