Chapter 272
Can The Hare Beat The Tortoise?
Riddler Express
Your true batting average is (a chance of a hit each at-bat), with four at-bats per game. What is your chance of batting at least over a -game season, and how does that compare with a -game season? Extra credit: your chance of a hitting streak of at least games (tying DiMaggio) in each season.
The Riddler, FiveThirtyEight, July 17, 2020(original post)
Solution
Each at-bat is an independent hit with probability . Over games that is at-bats, and batting means at least hits; the number of hits is Binomial, so the chance is the upper tail . Over games it is at-bats needing hits. The tails are so a short season makes about times more reachable: fewer at-bats mean more sampling variation around the true mean.
The streak runs the other way. A single game yields a hit with probability , but stringing such games together is punishing. The chance of a streak of at least games is roughly ten times likelier in the long season, which simply has more starting points for a long run. (Batting and streaking games is rarer still, and positively correlated, since both demand a glut of hits.)
The computation
Encode the binomial tail for the question and a short dynamic program over games (carry the current streak length, absorbing at ) for the streak.
from math import comb
from functools import lru_cache
def tail(at_bats, need, p=0.35):
return sum(comb(at_bats, k) * p**k * (1 - p)**(at_bats - k)
for k in range(need, at_bats + 1))
print(round(tail(240, 96) * 100, 2), round(tail(648, 260) * 100, 3)) # 6.08 0.379
pg = 1 - (1 - 0.35) ** 4 # hit in a game
def streak(G, L=56):
@lru_cache(None)
def f(g, run):
if run >= L: return 1.0
if g == 0: return 0.0
return pg * f(g - 1, run + 1) + (1 - pg) * f(g - 1, 0)
return f(G, 0)
print(round(streak(60) * 100, 4), round(streak(162) * 100, 4)) # 0.0028 0.0329
Riddler Classic
A -mile race runs along a road that instantly stretches by miles at the end of every minute (uniformly, carrying the cars with it). The tortoise drives mph, the hare mph. The tortoise starts at the gun; how long should the hare wait so that the two finish at the same instant?
The Riddler, FiveThirtyEight, July 17, 2020(original post)
Solution
Because the road stretches uniformly, the right variable is the fraction of the road a car has covered, not its distance: stretching carries a car forward in proportion, leaving its fraction unchanged. At mph the tortoise covers mile in the first minute, of the then -mile road. In the second minute it adds mile of a -mile road, ; in the th minute, . So after minutes its fraction covered is which first reaches at minutes (about days): the harmonic series diverges, so the tortoise does finish, eventually.
The hare drives mph, exactly faster, so over any shared stretch of time it covers more of the road. To tie, it should have more road left to cover when it starts, that is, it should leave when the tortoise has completed ... equivalently the hare may wait until the tortoise has completed and then cover the rest in lockstep to the line. The tortoise’s fraction reaches partway through the fourth minute, at
The computation
Encode the tortoise’s fraction-per-minute : sum it until the road is complete (the harmonic finish), and find the instant the fraction crosses (the hare’s cue).
H, n = 0.0, 0
while H < 10:
n += 1; H += 1 / n # fraction covered = H/10
print("tortoise finishes at minute", n) # 12367
frac, t = 0.0, 0
while frac < 0.2:
t += 1; frac += 1 / (10 * t)
prev = frac - 1 / (10 * t) # fraction at start of minute t
into = (0.2 - prev) / (1 / (10 * t)) # fraction into minute t
start = (t - 1) + into
print(f"hare waits {int(start)} min {round((start % 1) * 60)} s") # 3 min 40 s