Farm: Richter-Probe fuer die Antwort-Eichung (Sim-Folds je Typ gegen die echte Zaehltafel, Spec 5)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
2026-12-02 00:16:08 +01:00
co-authored by Claude Fable 5.2
parent d4731ac4d8
commit 44255808c8
2 changed files with 123 additions and 0 deletions
@@ -0,0 +1,80 @@
# Passt der Sim-Richter zur Antwort-Eichung? MOTOR_ANTWORT_EICHUNG rechnet mit der ECHTEN Foldrate des VPIP-Typs eines Gegners auf unsere Einsaetze (Turn/River).
# Hier: wie oft folden die SIM-Gegner je Sim-Typ auf die Einsaetze der Basis (Merkmal mk.fz der Batterie, nur Bets, Turn/River), daneben die echte Zaehlung des
# VPIP-Typs, den der Motor diesem Sim-Typ aus dem Profil zuordnet. Liegt die Sim-Rate weit von der Tafel, bestraft oder belohnt die Sim den Schalter fuer etwas,
# das an echten Tischen anders ist; das Duell-Ergebnis ist dann kein Urteil ueber den Schalter. Spec: tests/farm_richter_spec.py.
# python3 -X utf8 richter_antwort.py <teile-ordner> <antwort-tafel.txt> <gegner_profile.json> [--basis motor] [--bb 0.02] [--json aus.json]
# Naeherung: mk.fz zaehlt jede Antwort eines Gegners auf einen unserer Einsaetze der Strasse (nicht nur den ersten, auch Mehrweg); die Tafel zaehlt erste Einsaetze zu zweit.
import sys; sys.dont_write_bytecode = True
import collections, glob, json, math, os
STRASSE = {"T": 2, "R": 3}
TAFEL_BAND = (0.45, 0.85, 0.85, 1.3, 99.0, 99.0) # Sim-Band (batterie_v2.BAENDER 0,4/0,6/0,9/1,3/2,5) -> Band der Tafel (bis 0,45/0,85/1,3/darueber)
VPIP_ENG, VPIP_LOCKER = 0.22, 0.45 # wie antwort_eichung.rs
def tafel_band(i): return TAFEL_BAND[i]
def vpip_typ(vpip): return 1 if vpip < VPIP_ENG else 3 if vpip >= VPIP_LOCKER else 2
def wilson(k, n, z=1.96):
if n == 0: return (0.0, 1.0)
p = k / n; d = 1 + z * z / n; m = (p + z * z / (2 * n)) / d; h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d
return (m - h, m + h)
def sim_folds(zeilen, basis):
"""{(sim-typ, strasse 2|3, tafel-band): [folds, antworten]} aus den Zeilen der Basis-Fassung."""
out = collections.defaultdict(lambda: [0, 0])
for z in zeilen:
if z["engine"].split("=", 1)[0] != basis: continue
for st, band, fold, art in (z.get("mk") or {}).get("fz", []):
if st in STRASSE and art == "B":
e = out[(z["typ"], STRASSE[st], tafel_band(band))]; e[0] += bool(fold); e[1] += 1
return dict(out)
def tafel(zeilen, bb):
"""{(vpip-typ, strasse, band): (folds, n)} der Zaehltafel fuer ein Limit."""
out = {}
for z in zeilen:
v = z.split()
if len(v) == 7 and v[0] == "Z" and abs(float(v[1]) - bb) < 1e-9: out[(int(v[2]), int(v[3]), float(v[4]))] = (int(v[5]), int(v[6]))
return out
def lies(pfad):
for f in sorted(glob.glob(os.path.join(pfad, "*.jsonl"))) if os.path.isdir(pfad) else [pfad]:
with open(f, encoding="utf-8") as h:
for l in h:
try: yield json.loads(l)
except json.JSONDecodeError: pass
def main():
a = sys.argv[1:]
if len(a) < 3: sys.exit(__doc__)
opt = lambda k, std=None: a[a.index(k) + 1] if k in a else std # noqa: E731
sim = sim_folds(lies(a[0]), opt("--basis", "motor"))
with open(a[1], encoding="utf-8") as f: echt = tafel(f, float(opt("--bb", "0.02")))
prof = json.load(open(a[2], encoding="utf-8"))["typen"]
vpip = {t: v["reads"]["vpip"][1] / v["reads"]["vpip"][0] for t, v in prof.items() if "vpip" in v.get("reads", {})}
zeilen = []
print("Sim-Typ (VPIP, Typ der Tafel) | Strasse | Band bis | Sim-Fold [95 %] (n) | echt Fold [95 %] (n) | Abstand")
for (t, st, band), (k, n) in sorted(sim.items()):
vt = vpip_typ(vpip[t]) if t in vpip else 0; ke, ne = echt.get((vt, st, band), (0, 0))
ls, hs = wilson(k, n); le, he = wilson(ke, ne)
z = {"typ": t, "vpip": vpip.get(t), "tafel_typ": vt, "strasse": st, "band": band, "sim": [k, n], "echt": [ke, ne]}
zeilen.append(z)
print("%-8s (%.2f, T%d) | %s | %-4g | %5.1f %% [%4.1f; %4.1f] (%d) | %5.1f %% [%4.1f; %4.1f] (%d) | %+.1f" % (
t, vpip.get(t, float("nan")), vt, "Turn" if st == 2 else "River", band, 100 * k / max(n, 1), 100 * ls, 100 * hs, n,
100 * ke / max(ne, 1), 100 * le, 100 * he, ne, 100 * (k / max(n, 1) - ke / max(ne, 1))))
if opt("--json"):
tmp = opt("--json") + ".neu"
with open(tmp, "w", encoding="utf-8") as f: json.dump(zeilen, f, indent=1)
os.replace(tmp, opt("--json"))
if __name__ == "__main__":
main()
+43
View File
@@ -0,0 +1,43 @@
# Spezifikation von messung/farm/nacht-0112/richter_antwort.py (passt der Sim-Richter zur Antwort-Tafel? Fold der Sim-Gegner auf unsere Einsaetze je Typ gegen die echte Zaehlung des VPIP-Typs).
# python3 tests/farm_richter_spec.py
import sys; sys.dont_write_bytecode = True
import os, unittest
HIER = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HIER, "..", "messung", "farm", "nacht-0112"))
import richter_antwort as R # noqa: E402
class Baender(unittest.TestCase):
def test_sim_band_auf_tafel_band(self):
# Sim-Baender (batterie_v2.BAENDER): <0,4 | <0,6 | <0,9 | <1,3 | <2,5 | darueber; Tafel: <=0,45 | <=0,85 | <=1,3 | 99
self.assertEqual([R.tafel_band(i) for i in range(6)], [0.45, 0.85, 0.85, 1.3, 99.0, 99.0])
class Typen(unittest.TestCase):
def test_vpip_klasse_wie_antwort_eichung_rs(self):
self.assertEqual((R.vpip_typ(0.10), R.vpip_typ(0.22), R.vpip_typ(0.449), R.vpip_typ(0.45)), (1, 2, 2, 3))
class Zaehlen(unittest.TestCase):
def test_folds_der_sim_je_typ_strasse_band_nur_bets_auf_turn_und_river(self):
zeilen = [{"typ": "nit", "engine": "motor", "mk": {"fz": [["T", 0, True, "B"], ["T", 0, False, "B"], ["F", 0, True, "B"], ["R", 3, True, "R"]]}},
{"typ": "nit", "engine": "motor", "mk": {"fz": [["T", 0, True, "B"]]}},
{"typ": "nit", "engine": "kandidat=x", "mk": {"fz": [["T", 0, False, "B"]]}},
{"typ": "reg", "engine": "motor", "mk": {"fz": [["R", 5, False, "B"]]}}, {"typ": "reg", "engine": "motor"}]
self.assertEqual(R.sim_folds(zeilen, "motor"), {("nit", 2, 0.45): [2, 3], ("reg", 3, 99.0): [0, 1]})
def test_tafel_lesen_nur_das_limit(self):
text = "# x\nZ 0.02 1 2 0.45 8 35\nZ 0.02 1 2 0.85 49 97\nZ 0.05 1 2 0.45 0 2\n"
self.assertEqual(R.tafel(text.splitlines(), 0.02), {(1, 2, 0.45): (8, 35), (1, 2, 0.85): (49, 97)})
class Wilson(unittest.TestCase):
def test_intervall(self):
lo, hi = R.wilson(50, 100)
self.assertAlmostEqual(lo, 0.4038, places=3); self.assertAlmostEqual(hi, 0.5962, places=3)
self.assertEqual(R.wilson(0, 0), (0.0, 1.0))
if __name__ == "__main__":
unittest.main()