"""Exercise 23.1: optimizing a keyword-count proxy for several rounds (a simulation, not real data). Each "answer" is reduced to two numbers: content -- content score 0..10, which the optimizer never sees; keywords -- how many times the keyword appears in the answer. Proxy score = keywords. Human score = a written rule standing in for a human rater: content - 2 * (keywords beyond 4), floored at 0. (Up to 4 mentions read naturally; each extra mention is stuffing and costs 2 points.) Start: 16 answers. content is a random integer 2..7; keywords = content // 3 + (0 or 1 at random), so at the start better answers tend to mention the keyword more often. Each round: keep the 8 answers with the highest proxy score (ties broken at random), and rewrite each kept answer into 2 new versions. A rewrite either inserts one more keyword (probability 1/2, content unchanged) or edits the content (content changes by -1, 0 or +1 at random, kept within 0..10). The random seed is fixed so the run is reproducible. Run: python3 tools/exercises/ch23_keyword_proxy.py [threshold] [seed] (defaults: threshold 4, seed 2026; e.g. "python3 tools/exercises/ch23_keyword_proxy.py 6" moves the threshold of natural mentions from 4 to 6) """ import random import statistics import sys THRESHOLD = int(sys.argv[1]) if len(sys.argv) > 1 else 4 # keyword mentions that still read naturally SEED = int(sys.argv[2]) if len(sys.argv) > 2 else 2026 N = 16 ROUNDS = 8 PENALTY = 2 # points lost per extra mention rng = random.Random(SEED) def proxy(answer): content, keywords = answer return keywords def human(answer): content, keywords = answer return max(0, content - PENALTY * max(0, keywords - THRESHOLD)) def corr(xs, ys): if len(set(xs)) < 2 or len(set(ys)) < 2: return float("nan") return statistics.correlation(xs, ys) population = [] for _ in range(N): content = rng.randint(2, 7) population.append((content, content // 3 + rng.randint(0, 1))) history = [] def record(round_no): ps = [proxy(a) for a in population] hs = [human(a) for a in population] history.append((round_no, statistics.mean(ps), statistics.mean(hs), corr(ps, hs))) record(0) for r in range(1, ROUNDS + 1): rng.shuffle(population) # random tie-breaking population.sort(key=proxy, reverse=True) # stable sort keeps the shuffled order among ties kept = population[: N // 2] new = [] for content, keywords in kept: for _ in range(2): c, k = content, keywords if rng.random() < 0.5: k += 1 else: c = min(10, max(0, c + rng.choice([-1, 0, 1]))) new.append((c, k)) population = new record(r) print(f"threshold {THRESHOLD}, seed {SEED}") print("round mean proxy mean human corr(proxy, human) across the 16 answers") for r, p, h, c in history: shown = " n/a" if c != c else f"{c:8.2f}" # n/a: every answer has the same human score print(f"{r:5d} {p:10.2f} {h:10.2f} {shown}") peak = max(history, key=lambda row: row[2]) print(f"human score peaks at round {peak[0]} ({peak[2]:.2f})") for prev, cur in zip(history, history[1:]): if cur[1] > prev[1] and cur[2] < prev[2]: print(f"first round where the proxy rises while the human score falls: round {cur[0]}") break for r, p, h, c in history: if h < history[0][2]: print(f"first round where the human score is below its starting value: round {r}") break