"""Chapter 5 (sections 5.4, 5.6 and exercise 5.2): temperature, repeated sampling, and long tasks. All numbers are illustrative, constructed for teaching; they describe no particular model. """ from math import comb # 1. Temperature. Three candidate first words with probabilities at temperature 1. # Sampling at temperature T uses probabilities proportional to p ** (1 / T) # (equivalently: divide the model's scores by T before turning them into probabilities). cands = [("不会", 0.6), ("会", 0.3), ("可能", 0.1)] print("temperature table") print(" T = 0 : " + ", ".join(f"{w} {1.0 if i == 0 else 0.0:.3f}" for i, (w, _) in enumerate(cands))) for T in (0.5, 1.0, 2.0): weights = [p ** (1 / T) for _, p in cands] s = sum(weights) print(f" T = {T:<4}: " + ", ".join(f"{w} {x / s:.3f}" for (w, _), x in zip(cands, weights))) print() # 2. Repeated sampling of one question that the model answers correctly with probability p per sample. p, k = 0.6, 5 dist = [comb(k, j) * p**j * (1 - p) ** (k - j) for j in range(k + 1)] print(f"one question, p = {p}, {k} samples") for j, q in enumerate(dist): print(f" P({j} correct) = {q:.4f}") print(f" expected share correct = {p:.0%}") print(f" P(at least one correct) = {1 - (1 - p) ** k:.4f}") print(f" P(majority of {k} correct) = {sum(dist[3:]):.4f}") print(f" P(a single sample is wrong) = {1 - p:.0%}") print() # 3. A rare behavior with probability r per response. r = 0.01 for n in (5, 100, 300): print(f"rare behavior r = {r:.0%}: P(seen at least once in {n} samples) = {1 - (1 - r) ** n:.4f}") print(f"expected occurrences in 1,000,000 responses = {r * 1_000_000:,.0f}") print() # 4. Long tasks: each step independently right with probability s, all of m steps must be right. for s, m in ((0.99, 50), (0.99, 200), (0.999, 200)): print(f"step success {s}, {m} steps: P(all right) = {s ** m:.4f}")