diff --git a/score/aggregation.py b/score/aggregation.py index 0c2a165f..23d9c238 100644 --- a/score/aggregation.py +++ b/score/aggregation.py @@ -56,3 +56,23 @@ def bootstrap_diff(spots_a, spots_b, je100, value, draws=1000, seed=1): res.append(tot / CHIPS_PER_BB) res.sort() return point, (res[int(0.025 * draws)], res[min(draws - 1, int(0.975 * draws))]), len(pairs) + + +def reference_error_bound(pairs, je100): + """Bound (bb per 100 hands) of what the error of the reference can change in a PAIRED difference, or None if a spot has no measured error. + + Derivation. Both versions A, B are valued with the SAME Q-table of the reference, Q^ = Q + d (d = its error). The loss of a version is + L(pi) = sum_h w(h) [ max_a Q(h,a) - sum_a pi(a|h) Q(h,a) ]; in L(A) - L(B) the max term cancels, so what remains is + sum_h w(h) sum_a (pi_B - pi_A)(h,a) Q(h,a). The error part of this is sum_h w(h) sum_a (pi_B - pi_A)(h,a) d(h,a). Since + sum_a (pi_B - pi_A) = 0, every error that is the same for all actions of a hand cancels; with |d(h,a)| <= eps the rest is at most + (L1/2) * (2 eps) = eps * L1 per hand, L1 = sum_a |pi_A - pi_B|(h, .). Per spot: bound = eps * sum_h w(h) sum_a |pi_A - pi_B|, with + eps = the exploitability of the reference at the spot (chips, sum of both players' best-response gains). + ASSUMPTION: |d(h,a)| <= eps entrywise. NashConv bounds the reference's error in the reach-weighted sum, not entrywise, so this is the + stated working bound, not a proof. The spot bounds are added linearly over the strata means (errors of different spots may point the same way). + `pairs`: dicts with `stratum`, `eps` (chips or None), `l1` (sum_h w sum_a |pi_A - pi_B|).""" + if any(p["eps"] is None for p in pairs): + return None + by = {} + for p in pairs: + by.setdefault(p["stratum"], []).append(p["eps"] * p["l1"]) + return sum(je100[st] * sum(v) / len(v) for st, v in by.items()) / CHIPS_PER_BB diff --git a/score/score.py b/score/score.py index 05f2970a..aa29522e 100644 --- a/score/score.py +++ b/score/score.py @@ -22,6 +22,10 @@ HEADER = [ "exakte Loesung, nicht die Suche der Fassung.", "V0 (Calling-Station: nie folden, nie raisen) ist nur der Nullpunkt der Staerke, keine Decke.", "Regret gegen GG: Verlust gegen den Gleichgewichts-Gegner (Untergrenze von V). Mehrspieler und Vorflop sind nicht gemessen (0 Punkte, Gewicht siehe Luecken).", + "Intervall der gepaarten Differenz: Schranke aus dem Referenzfehler. Beide Fassungen werden mit derselben Referenz bewertet; im Unterschied " + "der Verluste faellt der Teil des Fehlers, der fuer alle Aktionen einer Hand gleich ist, heraus. Je Spot: Schranke = eps * Summe_h r(h) * Summe_a |pi_A - pi_B|, " + "eps = Ausnutzbarkeit der Referenz am Spot (Annahme |Fehler von Q(h,a)| <= eps je Eintrag, NashConv belegt das nur in der gewichteten Summe: " + "Arbeitsschranke, kein Beweis), linear ueber die Strata summiert. Der Bootstrap ueber die Spots steht nur als Auskunft (Tragweite ueber diese Spots hinaus).", "G (Gesamt) und Ausbeute A: nicht berechnet (Gegnertypen / q fehlen; w noch nicht entschieden, q noch nicht gemessen).", ] @@ -60,6 +64,7 @@ def spot_result(spot, ref, ev_by_name, profile, fassung): if gg is not None: res["floor"] = val_gg - gg # a perfect reference mixture would be 0: what the solver leaves res["unsere"], res["beste"] = shares(ref, pi) + res["_pi"], res["_w"] = {c: list(p) for c, p in profile.items()}, {canon(h["c"]): h["w"] for h in ref["hands"]} res["board"], res["in_round"] = ref["board"], ref["in_round"] return res @@ -198,16 +203,31 @@ def main(): base_line, diff_reg, ci_reg = "", None, None if a.basis: base = collect(a.basis, a, meta) - d, ci, n = G.bootstrap_diff(res, base, je100, lambda s: s["regret"]) - out.update({"gepaart": True, "basis": a.basis, "n_spots_gepaart": n, "differenz_regret_bb100": d, "intervall": list(ci), - "intervall_bedeutung": "Bootstrap ueber die Spots je Stratum (Stichprobe des Spot-Satzes), nur Auskunft; Solver-Zufall ist nicht enthalten (eine Referenz)"}) + d, ci_boot, n = G.bootstrap_diff(res, base, je100, lambda s: s["regret"]) + bmap = {s["id"]: s for s in base} + pairs = [] + for s in res: + b = bmap.get(s["id"]) + if b is None: + continue + l1 = sum(w * sum(abs(x - y) for x, y in zip(s["_pi"][c], b["_pi"][c])) for c, w in s["_w"].items() if c in b["_pi"]) + pairs.append({"stratum": s["stratum"], "eps": s["ref_expl_chips"], "l1": l1}) + bound = G.reference_error_bound(pairs, je100) + ci = None if bound is None else (d - bound, d + bound) + out.update({"gepaart": True, "basis": a.basis, "n_spots_gepaart": n, "differenz_regret_bb100": d, + "intervall": list(ci) if ci else None, "schranke_referenzfehler_bb100": bound, + "intervall_bedeutung": "Differenz +- Schranke aus dem Referenzfehler (siehe Kopf); None, wenn ein Spot keinen gemessenen Referenzfehler hat", + "auskunft_bootstrap_intervall": list(ci_boot), + "auskunft_bootstrap_bedeutung": "Tragweite ueber diese Spots hinaus: Bootstrap ueber die Spots je Stratum, kein Solver-Zufall"}) diff_reg, ci_reg = d, ci vs = [s for s in res if s["v_perfect"] is not None] bv = [s for s in base if s["v_perfect"] is not None] if vs and bv: dv, civ, nv = G.bootstrap_diff(vs, bv, je100, lambda s: s["v_perfect"]) out.update({"differenz_V_bb100": dv, "intervall_V": list(civ), "n_spots_gepaart_V": nv}) - base_line = f"gegen {a.basis} auf denselben {n} Spots: Regret-Differenz {d:+.3f} bb/100 [{ci[0]:+.3f}, {ci[1]:+.3f}] (negativ = besser, Intervall nur Spot-Stichprobe)" + base_line = (f"gegen {a.basis} auf denselben {n} Spots: Regret-Differenz {d:+.3f} bb/100 " + + (f"[{ci[0]:+.3f}, {ci[1]:+.3f}] (Schranke aus dem Referenzfehler)" if ci else "(Referenzfehler fehlt: kein Intervall)") + + f"; Auskunft Bootstrap ueber die Spots [{ci_boot[0]:+.3f}, {ci_boot[1]:+.3f}]; negativ = besser") out["buch_zeile"] = (f"{a.fassung}: S {('%.1f' % sm['S']) if sm['S'] is not None else 'n/a'} (Wurzel), Regret gegen GG {sm['regret_gg_bb100']:.3f} bb/100, " f"{len(res)} Spots v1 {meta['sha256'][:8]}" + (f", gegen {a.basis} {out['differenz_regret_bb100']:+.3f}" if a.basis else "")) txt = text(a.fassung, sm, lst, meta, base_line) diff --git a/score/test_aggregation.py b/score/test_aggregation.py index f55b4c6f..568f564b 100644 --- a/score/test_aggregation.py +++ b/score/test_aggregation.py @@ -42,6 +42,17 @@ class Aggregation(unittest.TestCase): self.assertEqual(n, 2) self.assertAlmostEqual(point, 0.0) + def test_reference_error_bound_hand_computed(self): + # spot bounds eps * L1: 20*0.5 = 10 and 40*1.0 = 40 chips, mean 25 chips = 0.25 bb, x 10 decisions per 100 hands + pairs = [{"stratum": "FLOP|srp|eroeffnen", "eps": 20.0, "l1": 0.5}, {"stratum": "FLOP|srp|eroeffnen", "eps": 40.0, "l1": 1.0}] + self.assertAlmostEqual(G.reference_error_bound(pairs, JE100), 2.5) + + def test_identical_mixtures_have_no_reference_error(self): + self.assertAlmostEqual(G.reference_error_bound([{"stratum": "FLOP|srp|eroeffnen", "eps": 99.0, "l1": 0.0}], JE100), 0.0) + + def test_an_unmeasured_reference_error_gives_no_bound_instead_of_zero(self): + self.assertIsNone(G.reference_error_bound([{"stratum": "FLOP|srp|eroeffnen", "eps": None, "l1": 0.3}], JE100)) + if __name__ == "__main__": unittest.main() diff --git a/score/test_score.py b/score/test_score.py index a74be959..6f8d945b 100644 --- a/score/test_score.py +++ b/score/test_score.py @@ -54,6 +54,11 @@ class ScoreEndToEnd(unittest.TestCase): self.assertAlmostEqual(s["S"], 100 * (1 - 0.125 / 2.375)) self.assertTrue(s["gepaart"]) self.assertAlmostEqual(s["differenz_regret_bb100"], -0.5) + # reference error bound: eps 10 chips x L1 (KsAs differs: 0.25 x 2 = 0.5) = 5 chips -> 10 per 100 x 5/100 = 0.5 bb/100 + self.assertAlmostEqual(s["schranke_referenzfehler_bb100"], 0.5) + self.assertAlmostEqual(s["intervall"][0], -1.0) + self.assertAlmostEqual(s["intervall"][1], 0.0) + self.assertIn("auskunft_bootstrap_intervall", s) self.assertIn("Gemessen wird die Entscheidung am Spot mit exakter Fortsetzung, nicht die volle Hand.", open(os.path.join(out, "score.txt")).read()) def test_tor7_file_format(self):