Cells by hand group x situation x stack band with cluster bootstrap over players, within-player difference, held-out day files, shown-only / fold-through / Welt v5.1 posterior estimators, no-card cross-check against the jam pool. Numbers only; hands and player ids stay on the SSD. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
244 lines
11 KiB
Python
244 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""Spec: preflop all-in vs raise vs call measurement (messung/motor/vorflop_allin_gegen_raise). Synthetic data only.
|
|
Run: python3 tests/vorflop_allin_spec.py"""
|
|
import os
|
|
import sys
|
|
import unittest
|
|
|
|
W = os.path.dirname(os.path.abspath(__file__))
|
|
sys.path.insert(0, os.path.join(W, "..", "messung", "motor", "vorflop_allin_gegen_raise"))
|
|
sys.path.insert(0, os.path.join(W, "..", "messung", "spiegel"))
|
|
sys.path.insert(0, os.path.join(W, "..", "entscheiden"))
|
|
import numpy as np # noqa: E402
|
|
|
|
import kern as K # noqa: E402
|
|
import haende as HD # noqa: E402
|
|
|
|
|
|
class Situation(unittest.TestCase):
|
|
def test_first_in_and_limp(self):
|
|
self.assertEqual(K.situation(0, 0), "first_in")
|
|
self.assertEqual(K.situation(0, 2), "vs_limp")
|
|
|
|
def test_raise_levels(self):
|
|
self.assertEqual([K.situation(r, 0) for r in (1, 2, 3)], ["vs_open", "vs_3bet", "vs_4bet"])
|
|
|
|
def test_limpers_do_not_change_raised_pots(self):
|
|
self.assertEqual(K.situation(1, 2), "vs_open")
|
|
|
|
|
|
class StackBand(unittest.TestCase):
|
|
def test_edges_belong_to_lower_band(self):
|
|
self.assertEqual([K.stack_band(x) for x in (5, 20, 20.01, 40, 80, 80.5, 150, 150.1, 400)], [0, 0, 1, 1, 2, 3, 3, 4, 4])
|
|
|
|
|
|
class Moves(unittest.TestCase):
|
|
def test_move_codes(self):
|
|
self.assertEqual(K.move(4, 4), "allin")
|
|
self.assertEqual(K.move(4, 0), "raise")
|
|
self.assertEqual(K.move(2, 255), "call")
|
|
self.assertIsNone(K.move(0, 255))
|
|
self.assertIsNone(K.move(1, 255))
|
|
|
|
|
|
class Groups(unittest.TestCase):
|
|
def test_groups(self):
|
|
g = K.hand_group
|
|
self.assertEqual([g(x) for x in ("AA", "KK", "QQ", "JJ", "TT", "99", "22")], ["AA", "KK", "QQ", "JJ-TT", "JJ-TT", "small_pair", "small_pair"])
|
|
self.assertEqual([g(x) for x in ("AKs", "AKo", "AQs", "AQo")], ["AK", "AK", "AQ", "AQ"])
|
|
self.assertEqual([g(x) for x in ("AJs", "KQo", "QJs", "JTo", "ATo")], ["broadway"] * 5)
|
|
self.assertEqual([g(x) for x in ("T9s", "98s", "54s")], ["suited_connector"] * 3)
|
|
self.assertEqual([g(x) for x in ("T9o", "J9s", "A5s", "72o")], ["rest"] * 4)
|
|
self.assertIsNone(g(""))
|
|
|
|
def test_every_class_has_a_group(self):
|
|
names = [a + a for a in K.RANKS] + [a + b + s for i, a in enumerate(K.RANKS) for b in K.RANKS[:i] for s in "so"]
|
|
self.assertEqual(len(names), 169)
|
|
for n in names:
|
|
self.assertIn(K.hand_group(n if n[0] == n[1] else n[0] + n[1] + n[2]), K.GROUPS)
|
|
|
|
|
|
class Behind(unittest.TestCase):
|
|
def test_six_max(self):
|
|
self.assertEqual([K.seats_behind(p, 6) for p in ("UTG", "HJ", "CO", "BTN", "SB", "BB")], [5, 4, 3, 2, 1, 0])
|
|
|
|
def test_short_tables(self):
|
|
self.assertEqual([K.seats_behind(p, 4) for p in ("CO", "BTN", "SB", "BB")], [3, 2, 1, 0])
|
|
self.assertEqual([K.seats_behind(p, 2) for p in ("SB", "BB")], [1, 0])
|
|
|
|
|
|
class PlayerType(unittest.TestCase):
|
|
def test_types(self):
|
|
self.assertEqual(K.player_type(0, 500), 0)
|
|
self.assertEqual(K.player_type(150, 500), 1)
|
|
self.assertEqual(K.player_type(450, 500), 2)
|
|
|
|
def test_shrinkage_pulls_few_hands_to_middle(self):
|
|
self.assertEqual(K.player_type(0, 2), 1)
|
|
|
|
|
|
class ValueFromDecision(unittest.TestCase):
|
|
"""Sunk money (blinds posted before the decision) is not counted; all-in showdown uses the expectation over random boards."""
|
|
|
|
def hu(self, brett):
|
|
return {"v": 1, "q": "t", "h": "1", "t": 0, "s": [{"id": "a" * 16, "st": 100.0}, {"id": "b" * 16, "st": 100.0}], "bl": [0.5, 1.0], "an": 0,
|
|
"z": [[0, 0, "r", 100.0], [1, 0, "c", 100.0]], "b": brett, "k": {"0": "AsAh", "1": "KsKh"}}
|
|
|
|
def test_expectation_replaces_runout_luck(self):
|
|
e, st = HD.lese_hand_vorflop(self.hu("2c7d9hJsQc"), "NL2", bretter=2000)
|
|
self.assertEqual(st, "ok")
|
|
sb = e[0]
|
|
self.assertEqual(sb["zug"], "raise")
|
|
self.assertEqual(sb["fach"], "a")
|
|
self.assertAlmostEqual(sb["wert_ev"], 2 * 0.819 * 100 - 100 + 0.5, delta=3.0) # equity AA vs KK ~81.9 %, blind 0.5 is sunk
|
|
|
|
def test_realized_value_differs_from_expectation_on_a_lost_board(self):
|
|
e, _ = HD.lese_hand_vorflop(self.hu("2c7dKd9hJs"), "NL2", bretter=2000) # KK flops a set: AA loses
|
|
sb = e[0]
|
|
self.assertAlmostEqual(sb["wert"], -100 + 0.5, delta=1.0)
|
|
self.assertGreater(sb["wert_ev"], 50)
|
|
|
|
|
|
class Aggregation(unittest.TestCase):
|
|
def test_sums_per_player(self):
|
|
out = K.sums_per_player([1, 1, 1, 2], [0, 0, 1, 0], [1.0, 3.0, 5.0, 7.0])
|
|
p, s, c = out[1]
|
|
self.assertEqual(list(p), [0, 1])
|
|
self.assertEqual(list(s), [4.0, 5.0])
|
|
self.assertEqual(list(c), [2.0, 1.0])
|
|
self.assertEqual(float(out[2][1][0]), 7.0)
|
|
|
|
def test_weighted_sums_use_posterior_mass(self):
|
|
out = K.sums_per_player([1, 1, 1], [0, 0, 1], [10.0, 20.0, 5.0], weight=[0.5, 0.0, 1.0])
|
|
p, s, c = out[1]
|
|
self.assertEqual(list(p), [0, 1])
|
|
self.assertEqual(list(s), [5.0, 5.0]) # the weight-0 row is dropped
|
|
self.assertEqual(list(c), [0.5, 1.0])
|
|
|
|
def test_seat_of_inverts_position_naming(self):
|
|
for n in range(2, 7):
|
|
seats = {HD.position(i, n): i for i in range(n)}
|
|
for name, i in seats.items():
|
|
self.assertEqual(K.seat_of(name, n), i)
|
|
|
|
def test_mean_and_interval_cover_truth(self):
|
|
rng = np.random.default_rng(1)
|
|
P = 400
|
|
p = np.arange(P)
|
|
c = np.full(P, 5.0)
|
|
s = rng.normal(2.0, 1.0, P) * c
|
|
Wb = K.poisson_weights(P, reps=300)
|
|
point, rep = K.boot_mean(p, s, c, Wb)
|
|
lo, hi = K.interval(rep)
|
|
self.assertAlmostEqual(point, s.sum() / c.sum())
|
|
self.assertLess(lo, 2.1)
|
|
self.assertGreater(hi, 1.9)
|
|
self.assertLess(hi - lo, 0.3)
|
|
|
|
def test_cluster_bootstrap_reflects_player_effect(self):
|
|
rng = np.random.default_rng(2)
|
|
P = 200
|
|
mu = rng.normal(0, 3.0, P) # strong player effect, no row noise
|
|
c = np.full(P, 50.0)
|
|
Wb = K.poisson_weights(P, reps=300)
|
|
_, rep = K.boot_mean(np.arange(P), mu * c, c, Wb)
|
|
lo, hi = K.interval(rep)
|
|
self.assertGreater(hi - lo, 0.7) # ~ 2*1.96*3/sqrt(200) = 0.83; row-level bootstrap would give ~0
|
|
|
|
def test_best_other_reselected_and_difference(self):
|
|
P = 300
|
|
p = np.arange(P)
|
|
c = np.full(P, 4.0)
|
|
Wb = K.poisson_weights(P, reps=200)
|
|
cells = {"allin": (p, np.full(P, 1.0) * c, c), "raise": (p, np.full(P, 3.0) * c, c), "call": (p, np.full(P, 2.0) * c, c)}
|
|
r = K.best_other(cells, Wb)
|
|
self.assertEqual(r["best"], "raise")
|
|
self.assertAlmostEqual(r["diff"][0], -2.0)
|
|
self.assertLess(r["diff"][1][1], 0)
|
|
|
|
def test_best_other_none_without_allin_or_other(self):
|
|
P = 5
|
|
c = np.ones(P)
|
|
Wb = K.poisson_weights(P, reps=10)
|
|
self.assertIsNone(K.best_other({"raise": (np.arange(P), c, c)}, Wb))
|
|
self.assertIsNone(K.best_other({"allin": (np.arange(P), c, c)}, Wb))
|
|
|
|
|
|
class FixedBest(unittest.TestCase):
|
|
def test_fixed_best_other_is_used_for_out_of_sample_evaluation(self):
|
|
P = 100
|
|
p = np.arange(P)
|
|
c = np.full(P, 2.0)
|
|
Wb = K.poisson_weights(P, reps=50)
|
|
cells = {"allin": (p, np.full(P, 1.0) * c, c), "raise": (p, np.full(P, 3.0) * c, c), "call": (p, np.full(P, 2.0) * c, c)}
|
|
r = K.best_other(cells, Wb, best="call") # training days said call; held-out raise is better, but the choice stays fixed
|
|
self.assertEqual(r["best"], "call")
|
|
self.assertAlmostEqual(r["diff"][0], -1.0)
|
|
|
|
|
|
class AllinWithFold(unittest.TestCase):
|
|
def strata(self, P=200):
|
|
p = np.arange(P)
|
|
# 100 jams per player-block: 80 % uncontested (+1.5), 20 % called; called, shown jams of the group average -50
|
|
unc = (p, np.full(P, 8 * 1.5), np.full(P, 8.0))
|
|
alle = (p, np.zeros(P), np.full(P, 10.0))
|
|
gerufen = (p, np.full(P, 2 * -50.0), np.full(P, 2.0))
|
|
return [{"unc": unc, "alle": alle, "gerufen": gerufen}]
|
|
|
|
def test_value_mixes_fold_through_and_called_part(self):
|
|
Wb = K.poisson_weights(200, reps=100)
|
|
pt, (lo, hi), _ = K.allin_with_fold(self.strata(), Wb)
|
|
self.assertAlmostEqual(pt, 0.8 * 1.5 + 0.2 * -50.0)
|
|
self.assertLessEqual(lo, pt + 1e-9)
|
|
self.assertGreaterEqual(hi, pt - 1e-9)
|
|
|
|
def test_no_called_shown_jam_gives_none(self):
|
|
P = 5
|
|
st = [{"unc": None, "alle": (np.arange(P), np.zeros(P), np.ones(P)), "gerufen": None}]
|
|
self.assertIsNone(K.allin_with_fold(st, K.poisson_weights(P, reps=5)))
|
|
|
|
def test_strata_are_weighted_by_called_shown_counts(self):
|
|
P = 50
|
|
p = np.arange(P)
|
|
a = {"unc": (p, np.full(P, 10.0), np.full(P, 10.0)), "alle": (p, np.zeros(P), np.full(P, 10.0)), "gerufen": (p, np.full(P, -10.0), np.full(P, 1.0))}
|
|
b = {"unc": None, "alle": (p, np.zeros(P), np.full(P, 10.0)), "gerufen": (p, np.full(P, -30.0), np.full(P, 3.0))}
|
|
pt, _, _ = K.allin_with_fold([a, b], K.poisson_weights(P, reps=5))
|
|
# stratum a: all uncontested -> value 1.0*pu... pu = 1 -> 1.0 ; stratum b: pu 0, m = -10 ; weights 1/4, 3/4 (called counts 50 vs 150)
|
|
self.assertAlmostEqual(pt, 0.25 * 1.0 + 0.75 * -10.0)
|
|
|
|
|
|
class WithinPlayer(unittest.TestCase):
|
|
def test_within_player_removes_player_type_selection(self):
|
|
"""Jammers are better players overall (+5), but each player loses 1 by jamming: pooled difference positive, within negative."""
|
|
P = 300
|
|
rng = np.random.default_rng(3)
|
|
skill = rng.normal(0, 3, P)
|
|
p = np.arange(P)
|
|
c = np.full(P, 6.0)
|
|
Wb = K.poisson_weights(P, reps=200)
|
|
half = P // 2
|
|
strong = np.argsort(-skill)[:half]
|
|
a = (np.sort(strong), (skill[np.sort(strong)] - 1) * c[:half], c[:half])
|
|
o = (p, skill * c, c)
|
|
pooled = K.boot_mean(*a, Wb)[0] - K.boot_mean(*o, Wb)[0]
|
|
self.assertGreater(pooled, 1.0) # selection: jammers are the strong half
|
|
pt, (lo, hi), n = K.within_player(a, o, Wb)
|
|
self.assertEqual(n, half)
|
|
self.assertAlmostEqual(pt, -1.0, places=6)
|
|
self.assertLess(hi, -0.99)
|
|
|
|
def test_no_common_players(self):
|
|
Wb = K.poisson_weights(4, reps=5)
|
|
self.assertIsNone(K.within_player((np.array([0, 1]), np.ones(2), np.ones(2)), (np.array([2, 3]), np.ones(2), np.ones(2)), Wb))
|
|
|
|
def test_weights_favor_players_with_more_hands(self):
|
|
Wb = np.ones((3, 2), dtype=np.float32)
|
|
a = (np.array([0, 1]), np.array([10.0, 0.0]), np.array([10.0, 1.0])) # means 1.0 (10 hands), 0.0 (1 hand)
|
|
o = (np.array([0, 1]), np.array([0.0, 0.0]), np.array([10.0, 1.0]))
|
|
pt, _, _ = K.within_player(a, o, Wb)
|
|
self.assertGreater(pt, 0.9)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|