Schaetzer mit exakt aufgezaehlter Erwartungstreue (tests/ope_spec.py), pi0 aus der Mischung im Zuggrund (geeicht an 59.145 Zuegen) und K-Seed-Nachspiel, Fold als exakt bekannter Wert (Traeger-Fall), Spielzeug mit bekannter Wahrheit (1.000 Wdh.), Fold-Test und A/A auf Echtdaten, Sperre +1,12 [+0,06; +2,27] bb/100. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
275 lines
14 KiB
Python
275 lines
14 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation messung/ope - Off-Policy-Auswertung: Wert einer neuen Motor-Fassung aus den Haenden der alten.
|
|
|
|
Mathematik (Beweisskizze in messung/ope/BERICHT.md):
|
|
W = Produkt ueber unsere Entscheidungen von pi1(a|s)/pi0(a|s); E_pi1[G] = E_pi0[W G] (pi0 > 0, wo pi1 > 0).
|
|
Delta = E[(W-1)(G-b)] fuer jedes b mit E_pi1[b] = E_pi0[b] (Konstante, AIVAT-Gluecksterme).
|
|
Doppelt robust (rekursiv, fuer JEDES Q^ erwartungstreu): v1 = V1(s_t) + w_t (v1' - Q^(s_t,a_t)), v0 = V0(s_t) + (v0' - Q^(s_t,a_t)).
|
|
Die Erwartungstreue wird hier EXAKT geprueft: alle Verlaeufe eines kleinen Spiels werden aufgezaehlt und mit pi0 gewichtet.
|
|
|
|
python3 tests/ope_spec.py -v
|
|
"""
|
|
import itertools
|
|
import math
|
|
import os
|
|
import random
|
|
import sys
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "messung")):
|
|
_w = os.path.dirname(_w)
|
|
sys.path.insert(0, os.path.join(_w, "messung", "ope"))
|
|
import ope # noqa: E402
|
|
import ope_daten # noqa: E402,F401
|
|
import ope_lauf # noqa: E402,F401
|
|
import verteilung # noqa: E402
|
|
|
|
# Spiel mit zwei Entscheidungen: Zustand 1 (Aktionen A/B), danach Zustand 2 (Aktionen A/B, Zustand haengt von a1 ab).
|
|
P0 = {1: {"A": 0.5, "B": 0.5}, 2: {"A": 0.7, "B": 0.3}, 3: {"A": 0.4, "B": 0.6}}
|
|
P1 = {1: {"A": 0.2, "B": 0.8}, 2: {"A": 0.7, "B": 0.3}, 3: {"A": 0.9, "B": 0.1}} # Zustand 2 unberuehrt
|
|
ZUSTAND2 = {"A": 2, "B": 3}
|
|
ENDWERT = {("A", "A"): 1.0, ("A", "B"): 4.0, ("B", "A"): -2.0, ("B", "B"): 6.0}
|
|
|
|
|
|
def verlaeufe(p0, p1):
|
|
for a1, a2 in itertools.product("AB", "AB"):
|
|
s2 = ZUSTAND2[a1]
|
|
wk = p0[1][a1] * p0[s2][a2]
|
|
yield wk, a1, a2, s2, ENDWERT[(a1, a2)]
|
|
|
|
|
|
def wahrheit(p):
|
|
return sum(p[1][a1] * p[ZUSTAND2[a1]][a2] * ENDWERT[(a1, a2)] for a1, a2 in itertools.product("AB", "AB"))
|
|
|
|
|
|
def hand(p0, p1, a1, a2, s2, g, q=None):
|
|
e = [{"p0": p0[1], "p1": p1[1], "a": a1, "q": (q or {}).get(1)},
|
|
{"p0": p0[s2], "p1": p1[s2], "a": a2, "q": (q or {}).get(s2)}]
|
|
return {"g": g, "e": e}
|
|
|
|
|
|
def erwartung(schaetzer, p0=P0, p1=P1, q=None, **kw):
|
|
return sum(wk * schaetzer(hand(p0, p1, a1, a2, s2, g, q), **kw) for wk, a1, a2, s2, g in verlaeufe(p0, p1) if wk > 0)
|
|
|
|
|
|
class Gewicht(unittest.TestCase):
|
|
def test_produkt_der_verhaeltnisse(self):
|
|
h = hand(P0, P1, "B", "A", 3, 0.0)
|
|
self.assertAlmostEqual(ope.gewicht(h), (0.8 / 0.5) * (0.9 / 0.4))
|
|
|
|
def test_gleiche_strategie_gibt_gewicht_eins_und_delta_null_exakt(self):
|
|
for a1, a2 in itertools.product("AB", "AB"):
|
|
h = hand(P0, P0, a1, a2, ZUSTAND2[a1], 7.3, q={1: {"A": 1.0, "B": 2.0}, 2: {"A": 0.5, "B": 0.1}, 3: {"A": -3.0, "B": 9.0}})
|
|
self.assertEqual(ope.gewicht(h), 1.0)
|
|
self.assertEqual(ope.schaetzer_einfach(h), 0.0)
|
|
self.assertEqual(ope.schaetzer_basis(h), 0.0)
|
|
self.assertEqual(ope.schaetzer_dr(h), 0.0)
|
|
|
|
def test_unberuehrte_entscheidung_aendert_nichts(self):
|
|
h = hand(P0, P1, "A", "B", 2, 3.0)
|
|
h2 = {"g": 3.0, "e": h["e"][:1]}
|
|
self.assertAlmostEqual(ope.gewicht(h), ope.gewicht(h2))
|
|
|
|
def test_kappung_begrenzt_je_entscheidung(self):
|
|
h = {"g": 1.0, "e": [{"p0": {"A": 0.01, "B": 0.99}, "p1": {"A": 0.9, "B": 0.1}, "a": "A"}]}
|
|
self.assertAlmostEqual(ope.gewicht(h), 90.0)
|
|
self.assertAlmostEqual(ope.gewicht(h, kappe=10.0), 10.0)
|
|
|
|
|
|
class Erwartungstreue(unittest.TestCase):
|
|
"""Exakte Erwartung ueber alle Verlaeufe (gewichtet mit pi0) gleich der wahren Differenz."""
|
|
|
|
def test_einfach(self):
|
|
self.assertAlmostEqual(erwartung(ope.schaetzer_einfach), wahrheit(P1) - wahrheit(P0))
|
|
|
|
def test_basis_mit_konstante(self):
|
|
for b in (0.0, 2.5, -7.0):
|
|
hs = lambda h, b=b: ope.schaetzer_basis(dict(h, b=b))
|
|
self.assertAlmostEqual(erwartung(hs), wahrheit(P1) - wahrheit(P0))
|
|
|
|
def test_dr_mit_beliebigem_q(self):
|
|
rng = random.Random(5)
|
|
for _ in range(20):
|
|
q = {s: {"A": rng.uniform(-9, 9), "B": rng.uniform(-9, 9)} for s in (1, 2, 3)}
|
|
self.assertAlmostEqual(erwartung(ope.schaetzer_dr, q=q), wahrheit(P1) - wahrheit(P0))
|
|
|
|
def test_dr_mit_wahrem_q_hat_keine_streuung(self):
|
|
# nur die erste Entscheidung ist beruehrt: dann ist Q unter pi1 auch Q unter pi0, und das wahre Q macht jede Hand exakt
|
|
p1 = {1: P1[1], 2: P0[2], 3: P0[3]}
|
|
q = {s: {a: ENDWERT[(a1, a)] for a in "AB"} for a1, s in ZUSTAND2.items()}
|
|
q[1] = {a1: sum(P0[ZUSTAND2[a1]][a2] * ENDWERT[(a1, a2)] for a2 in "AB") for a1 in "AB"}
|
|
wahr = wahrheit(p1) - wahrheit(P0)
|
|
for _, a1, a2, s2, g in verlaeufe(P0, p1):
|
|
self.assertAlmostEqual(ope.schaetzer_dr(hand(P0, p1, a1, a2, s2, g, q)), wahr)
|
|
|
|
def test_stuetze_fehlt_aber_wert_exakt_bekannt(self):
|
|
# pi0 spielt B in Zustand 1 nie; pi1 spielt B mit 0,3. Wert von B ist exakt bekannt (wie Fold = 0 ab der Entscheidung).
|
|
p0 = {1: {"A": 1.0, "B": 0.0}, 2: P0[2], 3: P0[3]}
|
|
p1 = {1: {"A": 0.7, "B": 0.3}, 2: P0[2], 3: P0[3]}
|
|
qb = sum(P0[3][a2] * ENDWERT[("B", a2)] for a2 in "AB")
|
|
q = {1: {"A": 0.0, "B": qb}, 2: {"A": 0.0, "B": 0.0}, 3: {"A": 0.0, "B": 0.0}}
|
|
wahr = wahrheit(p1) - wahrheit(p0)
|
|
# A bekommt Q^ = beliebig (hier 0): DR bleibt erwartungstreu, weil B nie gezogen wird und sein Q^ stimmt
|
|
self.assertAlmostEqual(erwartung(ope.schaetzer_dr, p0=p0, p1=p1, q=q), wahr)
|
|
|
|
def test_kappung_verzerrt_nicht_im_gleichen_gewicht(self):
|
|
self.assertAlmostEqual(erwartung(ope.schaetzer_einfach, kappe=1e9), wahrheit(P1) - wahrheit(P0))
|
|
|
|
|
|
class Traeger(unittest.TestCase):
|
|
def test_masse_ohne_stuetze(self):
|
|
h = {"g": 0.0, "e": [{"p0": {"A": 1.0, "B": 0.0}, "p1": {"A": 0.6, "B": 0.4}, "a": "A"}]}
|
|
self.assertAlmostEqual(ope.ohne_stuetze(h), 0.4)
|
|
self.assertAlmostEqual(ope.ohne_stuetze(h, exakt=frozenset({"B"})), 0.0)
|
|
|
|
def test_gestuetzte_hand_hat_masse_null(self):
|
|
self.assertEqual(ope.ohne_stuetze(hand(P0, P1, "A", "A", 2, 0.0)), 0.0)
|
|
|
|
def test_null_wahrscheinlichkeit_in_pi1_ist_keine_masse(self):
|
|
h = {"g": 0.0, "e": [{"p0": {"A": 1.0}, "p1": {"A": 1.0, "B": 0.0}, "a": "A"}]}
|
|
self.assertEqual(ope.ohne_stuetze(h), 0.0)
|
|
|
|
def test_beruehrt(self):
|
|
self.assertTrue(ope.beruehrt(hand(P0, P1, "A", "A", 2, 0.0)))
|
|
self.assertFalse(ope.beruehrt(hand(P0, P0, "A", "A", 2, 0.0)))
|
|
|
|
|
|
class Intervall(unittest.TestCase):
|
|
def test_konstante_null_gibt_intervall_null(self):
|
|
self.assertEqual(ope.intervall([0.0] * 50, seed=1), (0.0, 0.0, 0.0))
|
|
|
|
def test_skala_bb_je_100_und_mittel_im_intervall(self):
|
|
x = [random.Random(3).gauss(0.01, 0.2) if i % 10 == 0 else 0.0 for i in range(2000)]
|
|
m, lo, hi = ope.intervall(x, seed=2, boot=500)
|
|
self.assertAlmostEqual(m, 100 * sum(x) / len(x))
|
|
self.assertLess(lo, m)
|
|
self.assertGreater(hi, m)
|
|
|
|
def test_gleitkomma_rauschen_zaehlt_nicht_als_beruehrt(self):
|
|
x = [0.0] * 500 + [1e-16] * 100
|
|
self.assertEqual(ope.intervall(x, seed=1, boot=100)[1:], (0.0, 0.0))
|
|
|
|
def test_deterministisch_bei_gleichem_seed(self):
|
|
x = [math.sin(i) for i in range(300)]
|
|
self.assertEqual(ope.intervall(x, seed=9, boot=200), ope.intervall(x, seed=9, boot=200))
|
|
|
|
|
|
def test_breite_entspricht_dem_normalverteilten_standardfehler(self):
|
|
rng = random.Random(11)
|
|
n, k = 20000, 400 # 2 % beruehrte Haende
|
|
x = [rng.gauss(0.0, 3.0) if i < k else 0.0 for i in range(n)]
|
|
m, lo, hi = ope.intervall(x, seed=4, boot=1000)
|
|
se = 100.0 * math.sqrt(sum(v * v for v in x) - sum(x) ** 2 / n) / n # Standardfehler der Summe / n, bb/100
|
|
self.assertAlmostEqual((hi - lo) / (2 * 1.96), se, delta=0.12 * se)
|
|
self.assertAlmostEqual((hi + lo) / 2, m, delta=0.25 * se)
|
|
|
|
|
|
class SelbstNormiert(unittest.TestCase):
|
|
def test_alle_gewichte_null_ist_nicht_definiert(self):
|
|
h = {"g": 1.0, "e": [{"p0": {"A": 0.5, "B": 0.5}, "p1": {"A": 0.0, "B": 1.0}, "a": "A"}]}
|
|
self.assertTrue(math.isnan(ope.delta_selbstnormiert([h, h])))
|
|
|
|
def test_bekanntes_beispiel(self):
|
|
a = {"g": 2.0, "e": [{"p0": {"A": 0.5, "B": 0.5}, "p1": {"A": 1.0, "B": 0.0}, "a": "A"}]} # W = 2
|
|
b = {"g": 0.0, "e": [{"p0": {"A": 0.5, "B": 0.5}, "p1": {"A": 1.0, "B": 0.0}, "a": "B"}]} # W = 0
|
|
self.assertAlmostEqual(ope.delta_selbstnormiert([a, b]), (2 * 2.0) / 2.0 - 1.0)
|
|
|
|
def test_gleiche_strategie_ist_null(self):
|
|
hs = [hand(P0, P0, a1, a2, ZUSTAND2[a1], g) for _, a1, a2, _, g in verlaeufe(P0, P0)
|
|
for g in (g,)]
|
|
self.assertEqual(ope.delta_selbstnormiert(hs), 0.0)
|
|
|
|
|
|
class Mischung(unittest.TestCase):
|
|
Q = {"street": "RIVER", "toCall": 0.1, "pot": 0.4, "maxRaiseTo": 1.68, "bigBlind": 0.02, "heroSeat": 1, "history": []}
|
|
|
|
def test_suche(self):
|
|
d, rein = verteilung.mischung(self.Q, "motor RIVER genau (67 ms, Topf 3200 Rest 8400): Fold 20%, Call 18%, Raise(4800) 18%, AllIn(8400) 43%")
|
|
self.assertTrue(rein)
|
|
self.assertAlmostEqual(d["FOLD"], 0.20 / 0.99) # Summe 99 %: auf 1 normiert
|
|
self.assertAlmostEqual(d["RAISE"], 0.18 / 0.99)
|
|
self.assertAlmostEqual(d["JAM"], 0.43 / 0.99)
|
|
self.assertAlmostEqual(sum(d.values()), 1.0)
|
|
|
|
def test_baum_jam_als_etikett(self):
|
|
d, rein = verteilung.mischung(self.Q, "motor FLOP (2 im Topf, Klasse 1730): fold 0%, call 26%, 0.80x 11%, all-in 63%")
|
|
self.assertTrue(rein)
|
|
self.assertAlmostEqual(d["JAM"], 0.63)
|
|
self.assertAlmostEqual(d["CALL"], 0.26)
|
|
|
|
def test_regel_praefix_ist_nicht_rein(self):
|
|
d, rein = verteilung.mischung(self.Q, "NL2-Typ [VPIP 22-26 passiv] CALL->FOLD (Call gegen ihn -2.3 bb); motor TURN genau (1366 ms, Topf 1500 Rest 9250): Fold 18%, Call 79%, Raise(3690) 3%, AllIn(9250) 0%")
|
|
self.assertFalse(rein)
|
|
self.assertAlmostEqual(d["CALL"], 0.79)
|
|
|
|
def test_baum_etikett_mit_groesse_am_maximum_ist_jam(self):
|
|
q = dict(self.Q, toCall=0.1, pot=0.4, maxRaiseTo=0.6)
|
|
d, _ = verteilung.mischung(q, "motor FLOP (2 im Topf, Klasse 1): fold 0%, call 0%, 1.00x 100%") # 0,1 + 1,0 * 0,5 = 0,6
|
|
self.assertAlmostEqual(d["JAM"], 1.0)
|
|
d, _ = verteilung.mischung(dict(q, maxRaiseTo=5.0), "motor FLOP (2 im Topf, Klasse 1): fold 0%, call 0%, 1.00x 100%")
|
|
self.assertAlmostEqual(d["RAISE"], 1.0)
|
|
|
|
def test_dienst_ohne_mischung(self):
|
|
self.assertEqual(verteilung.mischung(self.Q, "sharingan blueprint (preflop)"), (None, False))
|
|
|
|
def test_klasse_des_zugs(self):
|
|
self.assertEqual(verteilung.klasse(self.Q, {"action": "ALL_IN"}), "JAM")
|
|
self.assertEqual(verteilung.klasse(self.Q, {"action": "RAISE", "size": 1.68}), "JAM")
|
|
self.assertEqual(verteilung.klasse(self.Q, {"action": "RAISE", "size": 0.5}), "RAISE")
|
|
self.assertEqual(verteilung.klasse(self.Q, {"action": "CALL"}), "CALL")
|
|
self.assertEqual(verteilung.klasse(dict(self.Q, toCall=0.0), {"action": "CALL"}), "CHECK")
|
|
|
|
|
|
class SperreAufDaten(unittest.TestCase):
|
|
"""Schwachjam-Sperre als pi1: JAM-Masse wandert auf den Ersatz-Zug, alles andere bleibt; Erwartung des Gewichts ist 1."""
|
|
import ope_daten
|
|
import ope_lauf
|
|
GRUND = "motor PREFLOP (2 im Topf, Klasse 5): fold 30%, call 20%, all-in 50%"
|
|
Q = {"street": "PREFLOP", "bigBlind": 0.02, "heroSeat": 2, "heroCards": [{"rank": "SEVEN", "suit": "SPADES"}, {"rank": "TWO", "suit": "DIAMONDS"}],
|
|
"pot": 0.09, "toCall": 0.06, "heroStack": 2.0, "maxRaiseTo": 2.0, "minRaiseTo": 0.2, "legalActions": ["FOLD", "CALL", "RAISE"],
|
|
"villains": [{"seat": 1, "stack": 2.0, "folded": False}], "board": {},
|
|
"history": [{"street": "PREFLOP", "seat": 2, "action": "POST_BB", "amount": 0.02}, {"street": "PREFLOP", "seat": 1, "action": "RAISE", "amount": 0.08}]}
|
|
CACHE = {1: {"nr": 1, "k": 64, "klassen": {"JAM": 30, "FOLD": 20, "CALL": 14}}}
|
|
|
|
def antwort(self, aktion):
|
|
return {"q": self.Q, "motor": {"action": aktion, "size": None, "reason": self.GRUND}}
|
|
|
|
def test_ersatz_ist_fold_wenn_fold_anteil_groesser(self):
|
|
self.assertEqual(self.ope_daten.sperre_ersatz(self.Q, self.GRUND), "FOLD")
|
|
self.assertEqual(self.ope_daten.sperre_ersatz(self.Q, self.GRUND.replace("fold 30%, call 20%", "fold 10%, call 40%")), "CALL")
|
|
|
|
def test_flacher_stack_wird_nicht_beruehrt(self):
|
|
flach = dict(self.Q, heroStack=0.4, maxRaiseTo=0.4, villains=[{"seat": 1, "stack": 0.4, "folded": False}])
|
|
self.assertIsNone(self.ope_daten.sperre_ersatz(flach, self.GRUND))
|
|
|
|
def test_masse_wandert_auf_den_ersatz(self):
|
|
art, p0, p1, a = self.ope_lauf.Sperre(self.CACHE).verteilung(1, self.Q, self.antwort("ALL_IN"))
|
|
self.assertEqual((art, a), ("ok", "JAM"))
|
|
self.assertAlmostEqual(p1["FOLD"], p0["FOLD"] + p0["JAM"])
|
|
self.assertEqual(p1["JAM"], 0.0)
|
|
self.assertAlmostEqual(p1["CALL"], p0["CALL"])
|
|
|
|
def test_erwartung_des_gewichts_ist_eins(self):
|
|
art, p0, p1, a = self.ope_lauf.Sperre(self.CACHE).verteilung(1, self.Q, self.antwort("FOLD"))
|
|
self.assertAlmostEqual(sum(p0[k] * (p1[k] / p0[k]) for k in p0 if p0[k] > 0), sum(p1.values()))
|
|
self.assertAlmostEqual(sum(p1.values()), sum(p0.values()))
|
|
|
|
def test_gespielter_zug_mit_null_prozent_bekommt_untergrenze(self):
|
|
grund = "motor PREFLOP (2 im Topf, Klasse 5): fold 0%, call 0%, all-in 100%"
|
|
art, p0, p1, a = self.ope_lauf.Sperre(self.CACHE).verteilung(1, self.Q, {"q": self.Q, "motor": {"action": "CALL", "reason": grund}})
|
|
self.assertGreater(p0["CALL"], 0.0)
|
|
|
|
def test_etikett_all_in_ohne_jam_antwort_ist_kein_jam(self):
|
|
cache = {1: {"nr": 1, "k": 64, "klassen": {"RAISE": 64}}}
|
|
art, p0, p1, a = self.ope_lauf.Sperre(cache).verteilung(1, self.Q, {"q": self.Q, "motor": {"action": "RAISE", "size": 0.9, "reason": self.GRUND}})
|
|
self.assertEqual(art, "unberuehrt") # die Sperre sieht in dieser Antwort keinen Jam: pi1 = pi0
|
|
|
|
def test_ohne_nachspiel_fehlt_die_antwort_zum_etikett(self):
|
|
art = self.ope_lauf.Sperre({}).verteilung(1, self.Q, self.antwort("FOLD"))[0]
|
|
self.assertEqual(art, "offen")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|