<p>I always get giddy when I can apply real statistics and math to problems in my life. Recently, I had an opportunity to apply the ‘Taxicab Problem’ to something that came up at work. Given that I work for a ridesharing platform and I was quite literally counting “taxis” (or at least cars meant to drive others around), this was doubly exquisite.</p>
<p>For the uninitiated, the Taxicab / Germany Tank problem is as follows:</p>
<p>Viewing a city from the train, you see a taxi numbered x. Assuming taxicabs are consecutively numbered, how many taxicabs are in the city?</p>
<p>This was also applied to counting German tanks in World War II to know when/if to attack. Statstical methods ended up being accurate within a few tanks (on a scale of 200-300) while “intelligence” (unintelligence) operations overestimated numbers about 6-7x. <a href="https://en.wikipedia.org/wiki/German_tank_problem">Read the full details on Wikipedia here</a> (and donate while you’re over there).</p>
<p>Somebody suggested using $2 * \bar{x}$ where $\bar{x}$ is the mean of the current observations. This made me feel all sorts of weird inside but I couldn’t quite put my finger on what was immediately wrong with this (outside of the glaringly obvious, touched upon shortly). On one hand, it makes some sense intuitively. If the distribution of the numbers you see is random, you can be somewhat sure that the mean of your subset is approxiately the mean of the true distribution. Then you simply multiply by 2 to get the max since the distribution is uniform. However, this doesn’t take into account that $2 * \bar{x}$ might be lower than $m$ where $m$ is the max number you’ve seen so far. An easy fix to this method is to simply use $max(m, 2 * \bar{x})$.</p>
<p>To be most correct, we can use the MVUE (Minimum Variance Unbiased Estimator). I won’t bore you with the derivation (and it’s also on the Wiki), but it works out to be $m + \frac{m}{N} - 1$ with $m$ is again the max number you’ve seen and $N$ is the total number you’ve seen. Let’s compare all 3 methods below:</p>
<div><span></span><div><table><tr><td><pre><span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
<span>11</span>
<span>12</span>
<span>13</span>
<span>14</span>
<span>15</span>
<span>16</span>
<span>17</span>
<span>18</span>
<span>19</span>
<span>20</span>
<span>21</span>
<span>22</span>
<span>23</span>
<span>24</span>
<span>25</span>
<span>26</span>
<span>27</span>
<span>28</span>
<span>29</span>
<span>30</span>
<span>31</span>
<span>32</span>
<span>33</span>
<span>34</span>
<span>35</span>
<span>36</span>
<span>37</span>
</pre></td><td><pre><code><span><span>library</span><span>(</span>reshape2<span>)</span>
</span><span><span>library</span><span>(</span>ggplot2<span>)</span>
</span><span>
</span><span>trueMax <span>&lt;-</span> <span>5000</span>
</span><span>n <span>&lt;-</span> <span>500</span>
</span><span>draws <span>&lt;-</span> <span>100</span>
</span><span>
</span><span>meanMethod <span>&lt;-</span> <span>replicate</span><span>(</span>n<span>,</span> <span>{</span>
</span><span> x <span>&lt;-</span> <span>sample</span><span>(</span>trueMax<span>,</span> draws<span>,</span> replace <span>=</span> <span>F</span><span>)</span>
</span><span> <span>2</span> <span>*</span> <span>cumsum</span><span>(</span>x<span>)</span> <span>/</span> <span>(</span><span>1</span><span>:</span><span>length</span><span>(</span>x<span>))</span>
</span><span><span>},</span>
</span><span>simplify <span>=</span> <span>&quot;data.frame&quot;</span><span>)</span>
</span><span>
</span><span>meanMethod <span>&lt;-</span> melt<span>(</span>meanMethod<span>)</span>
</span><span>
</span><span>tankMethod <span>&lt;-</span> <span>replicate</span><span>(</span>n<span>,</span> <span>{</span>
</span><span> x <span>&lt;-</span> <span>sample</span><span>(</span>trueMax<span>,</span> draws<span>,</span> replace <span>=</span> <span>F</span><span>)</span>
</span><span> <span>cummax</span><span>(</span>x<span>)</span> <span>+</span> <span>cummax</span><span>(</span>x<span>)</span> <span>/</span> <span>(</span><span>1</span><span>:</span><span>length</span><span>(</span>x<span>))</span> <span>-</span> <span>1</span>
</span><span><span>},</span>
</span><span>simplify <span>=</span> <span>&quot;data.frame&quot;</span><span>)</span>
</span><span>
</span><span>tankMethod <span>&lt;-</span> melt<span>(</span>tankMethod<span>)</span>
</span><span>
</span><span>myMethod <span>&lt;-</span> <span>replicate</span><span>(</span>n<span>,</span> <span>{</span>
</span><span> x <span>&lt;-</span> <span>sample</span><span>(</span>trueMax<span>,</span> draws<span>,</span> replace <span>=</span> <span>F</span><span>)</span>
</span><span> out <span>&lt;-</span> <span>2</span> <span>*</span> <span>cumsum</span><span>(</span>x<span>)</span> <span>/</span> <span>(</span><span>1</span><span>:</span><span>length</span><span>(</span>x<span>))</span>
</span><span> <span>ifelse</span><span>(</span>out <span>></span> <span>cummax</span><span>(</span>x<span>),</span> out<span>,</span> <span>cummax</span><span>(</span>x<span>))</span>
</span><span><span>},</span>
</span><span>simplify <span>=</span> <span>&quot;data.frame&quot;</span><span>)</span>
</span><span>
</span><span>myMethod <span>&lt;-</span> melt<span>(</span>myMethod<span>)</span>
</span><span>
</span><span>meanMethod<span>$</span>method <span>&lt;-</span> <span>'2 * mean'</span>
</span><span>tankMethod<span>$</span>method <span>&lt;-</span> <span>'m + m/N - 1'</span>
</span><span>myMethod<span>$</span>method <span>&lt;-</span> <span>'max(m, 2 * mean)'</span>
</span><span>
</span><span>df <span>&lt;-</span> <span>rbind</span><span>(</span>meanMethod<span>,</span> tankMethod<span>,</span> myMethod<span>)</span>
</span></code></pre></td></tr></table></div></div>
<p>Let’s plot it:</p>
<div><span></span><div><table><tr><td><pre><span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
</pre></td><td><pre><code><span>qplot<span>(</span>Var1<span>,</span> value<span>,</span> group <span>=</span> <span>interaction</span><span>(</span>Var2<span>,</span> method<span>),</span> color <span>=</span> method<span>,</span> data <span>=</span> df<span>,</span> geom <span>=</span> <span>&quot;line&quot;</span><span>,</span> alpha <span>=</span> <span>I</span><span>(</span><span>.05</span><span>))</span> <span>+</span>
</span><span> geom_hline<span>(</span>yintercept <span>=</span> <span>5000</span><span>,</span> size <span>=</span> <span>2</span><span>,</span> linetype <span>=</span> <span>2</span><span>)</span> <span>+</span>
</span><span> guides<span>(</span>colour <span>=</span> guide_legend<span>(</span>override.aes <span>=</span> <span>list</span><span>(</span>alpha <span>=</span> <span>1</span><span>)))</span> <span>+</span>
</span><span> ylab<span>(</span><span>&quot;Best Guess&quot;</span><span>)</span> <span>+</span>
</span><span> xlab<span>(</span><span>&quot;How many have we seen so far?&quot;</span><span>)</span> <span>+</span>
</span><span> xlim<span>(</span><span>1</span><span>,</span> <span>100</span><span>)</span>
</span></code></pre></td></tr></table></div></div>
<p align="center"></p>
<p>Zoom in a bit:</p>
<div><span></span><div><table><tr><td><pre><span>1</span>
</pre></td><td><pre><code><span>last_plot<span>()</span> <span>+</span> xlim<span>(</span><span>1</span><span>,</span> <span>25</span><span>)</span>
</span></code></pre></td></tr></table></div></div>
<p align="center"></p>
<p>And this one is just prettttyyyy:</p>
<div><span></span><div><table><tr><td><pre><span>1</span>
</pre></td><td><pre><code><span>last_plot<span>()</span> <span>+</span> xlim<span>(</span><span>1</span><span>,</span> <span>5</span><span>)</span>
</span></code></pre></td></tr></table></div></div>
<p align="center"></p>
<p>As we can see, the green line converges to the true max of 5000 very quickly, and has far less variance than the two other methods. The modified method I proposed is just an asymmetric version of $2 * \bar{x}$, which approaches the true max but seems to level out in local minima/maxima at certain points. Ultimately, you can’t deny that the MVUE method is best.</p>
<p>Another approach we can take is a Bayesian one. Since I’d never pass up an opportunity to showcase my R package, bayesAB, (<a href="https://github.com/FrankPortman/bayesAB">github repo here</a>) for Bayesian AB testing let’s work through an example with bayesAB. This is actually an extra cool example since it showcases that while bayesAB is primarily meant to fit and compare two sets of posteriors, it can still be used to fit a single Bayesian model.</p>
<p>For brevity’s sake, we fit a Bayesian model using the Pareto distribution as the conjugate prior (if this is all a foreign language to you, definitely check out my other blog posts on bayesAB <a href="http://fportman.com/blog/bayesab-a-new-r-package-for-bayesian-ab-testing/">1</a> <a href="http://fportman.com/blog/bayesab-0-dot-7-0-plus-a-primer-on-priors/">2</a>). We can use very small values for <strong>xm</strong> and <strong>alpha</strong> as diffuse priors so the model we build for the Uniform distribution is solely based on the data.</p>
<div><span></span><div><table><tr><td><pre><span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</pre></td><td><pre><code><span>x <span>&lt;-</span> <span>sample</span><span>(</span>trueMax<span>,</span> draws<span>,</span> replace <span>=</span> <span>F</span><span>)</span>
</span><span>
</span><span>mod <span>&lt;-</span> bayesTest<span>(</span>x<span>,</span> x<span>,</span> priors <span>=</span> <span>c</span><span>(</span><span>&quot;xm&quot;</span> <span>=</span> <span>.005</span><span>,</span> <span>&quot;alpha&quot;</span> <span>=</span> <span>.005</span><span>),</span> distribution <span>=</span> <span>&quot;uniform&quot;</span><span>)</span>
</span><span>
</span><span>plot<span>(</span>mod<span>,</span> priors <span>=</span> <span>FALSE</span><span>,</span> samples <span>=</span> <span>FALSE</span><span>)</span>
</span></code></pre></td></tr></table></div></div>
<p align="center"></p>
<p>As we can see, the posterior is fairly thin between 4980 and 5000. Taking the mean or median of this is a solid estimate for the maximum. Let’s see what the median says using the summary generic.</p>
<div><span></span><div><table><tr><td><pre><span>1</span>
</pre></td><td><pre><code><span><span>summary</span><span>(</span>mod<span>)</span>
</span></code></pre></td></tr></table></div></div>
<div><span></span><div><table><tr><td><pre><span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
<span>6</span>
<span>7</span>
<span>8</span>
<span>9</span>
<span>10</span>
<span>11</span>
<span>12</span>
<span>13</span>
<span>14</span>
<span>15</span>
<span>16</span>
<span>17</span>
<span>18</span>
<span>19</span>
<span>20</span>
<span>21</span>
<span>22</span>
<span>23</span>
<span>24</span>
<span>25</span>
<span>26</span>
<span>27</span>
<span>28</span>
<span>29</span>
<span>30</span>
<span>31</span>
<span>32</span>
<span>33</span>
</pre></td><td><pre><code><span>Quantiles of posteriors <span>for</span> A and B<span>:</span>
</span><span>
</span><span><span>$</span>Theta
</span><span><span>$</span>Theta<span>$</span>A_thetas
</span><span> <span>0</span><span>% 25%</span> <span>50</span><span>% 75%</span> <span>100</span><span>% </span>
</span><span><span>4980.000 4981.431 4983.458 4986.891 5038.651 </span>
</span><span>
</span><span><span>$Theta$B_thetas</span>
</span><span><span> 0%</span> <span>25</span><span>% 50%</span> <span>75</span><span>% 100%</span>
</span><span><span>4980.000</span> <span>4981.445</span> <span>4983.448</span> <span>4986.914</span> <span>5036.582</span>
</span><span>
</span><span>
</span><span><span>--------------------------------------------</span>
</span><span>
</span><span>P<span>(</span>A <span>></span> B<span>)</span> by <span>(</span><span>0</span><span>)</span><span>%: </span>
</span><span>
</span><span><span>$Theta</span>
</span><span><span>[1] 0.49782</span>
</span><span>
</span><span><span>--------------------------------------------</span>
</span><span>
</span><span><span>Credible Interval on (A - B) / B for interval length(s) (0.9) : </span>
</span><span>
</span><span><span>$Theta</span>
</span><span><span> 5%</span> <span>95</span>%
</span><span><span>-0.002282182</span> <span>0.002298932</span>
</span><span>
</span><span><span>--------------------------------------------</span>
</span><span>
</span><span>Posterior Expected Loss <span>for</span> choosing B over A<span>:</span>
</span><span>
</span><span><span>$</span>Theta
</span><span><span>[</span><span>1</span><span>]</span> <span>2504.588</span>
</span></code></pre></td></tr></table></div></div>
<p>For our purposes, we can ignore everything besides the quantiles of the posteriors. Using this, we arrive at an estimate of 4983 as our max. Using $2 * \bar{x}$ for this same sample results in 5034 which is overshooting by quite a bit.</p>