Sunday, March 7, 2021

A feat even Mike Trout can't manage ...

Suppose a baseball player has four at-bats per game (not including walks), so their batting average is the number of hits they got divided by four times the number of games they played. For many games, it’s possible to have a corresponding batting average that, when rounded to three digits, equals the number of games divided by 1,000. For example, if a player typically gets one hit per game in their at-bats, then they could very well have a .250 average over 250 games.


Again assuming $4$ AB per game, what is the greatest number of games for which it is not possible to have a matching rounded batting average?


My initial flippant reaction was $\infty,$ since it is obviously impossible to get a batting average over $1.000$ and so one cannot have a batting average over the past $n$ games equal to $n/1000$ for any $n \geq 1001.$ However, after a quick consultation with Riddlermeister Zach Wissner-Gross, it was deemed that the "greatest number of games ($\leq 1000$)" was roughly implied.


So with actual work to be done then, let's define the problem. For any number of games $N \leq 1000,$ we would need to check whether there is an integer $m \leq 4N$ such that $$\left\lfloor \frac{250m}{N} \right\rceil = N,$$ where $\lfloor x \rceil$ is the $x$ rounded to nearest integer. If we ignore the rounding, we would expect that that integer $m$ should be approximately $m \approx \frac{N^2}{250},$ so we should really only need to check whether $\left\lfloor \frac{N^2}{250} \right\rfloor$ and $\left\lceil \frac{N^2}{250} \right\rceil$ work. For larger values of $N,$ there may be additional values of $m$ that might work (e.g., any number of hits $m$ with $3991 \leq m \leq 3994$ will suffice for the obscene average of $0.999$ over the past $999$ games), but certainly if there is some integer $m$ solution than one of those two will work. Without any real care for optimizing, we can use a simple Python list comprehension to obtain that there are 128 such values of $N (\leq 1000)$ that cannot produce a matching rounded batting average and that the largest one is $N=239$.

MikeTrout
In [1]:
import numpy as np
nonround_ba = [n for n in range(1, 1001) if min([np.abs(np.round(np.floor(n*n/250)/(4*n),3) - n/1000),\
                                              np.abs(np.round(np.ceil(n*n/250)/(4*n),3) - n/1000),]) > 0]
print('''There are %d values of N that cannot produce a rounded batting average assuming 4 AB per game, 
the largest of which is %d''' % (len(nonround_ba), max(nonround_ba)))
There are 128 values of N that cannot produce a rounded batting average assuming 4 AB per game, 
the largest of which is 239

No comments:

Post a Comment