"""Figure 16.x (fig-16-flag-checking): the numbers the chapter quotes from the illustrative model. Repeats, in plain Python, the arithmetic of src/figures/cr-flag-checking.ts. The code has 4 real errors. The critic's flags, in its rank order, are real or false in the fixed order R R F R F F R F. Confirming a real flag takes 4 minutes and adds one confirmed error; ruling out a false flag takes `false_min` minutes and adds nothing. An unaided reviewer given the same total minutes finds errors at an average `rate` per minute, capped at 4. All values are illustrative, not measured. """ from fractions import Fraction as F ERRORS = 4 REAL_MIN = 4 ORDER = [True, True, False, True, False, False, True, False] def run(n, false_min, rate): flags = ORDER[:n] tp = sum(flags) fp = n - tp total = tp * REAL_MIN + fp * false_min unaided = min(F(ERRORS), F(rate) * total) return { "flags": n, "real": tp, "false": fp, "precision": F(tp, n), "recall": F(tp, ERRORS), "minutes": total, "false_minutes": fp * false_min, "unaided": unaided, "gain": tp - unaided, } def show(name, r): print(f"{name}: {r['flags']} flags, {r['real']} real, {r['false']} false") print(f" precision = {r['real']}/{r['flags']} = {float(r['precision']):.0%}") print(f" recall = {r['real']}/{ERRORS} = {float(r['recall']):.0%}") print(f" minutes = {r['minutes']}, of which on false flags {r['false_minutes']}") print(f" unaided reviewer in the same time: {float(r['unaided']):g} errors on average") print(f" gain from the critique: {float(r['gain']):+g}") rate = F(5, 100) show("default (5 flags, 8 min per false flag, 0.05 per minute)", run(5, 8, rate)) show("all 8 flags", run(8, 8, rate)) show("unaided rate 0.11 per minute", run(5, 8, F(11, 100))) # The unaided rate at which the gain is zero for the default flags: 3 errors in 28 minutes. r = run(5, 8, rate) print(f"break-even unaided rate = {r['real']}/{r['minutes']} = {r['real'] / r['minutes']:.4f} per minute")