Zwei-Skalen-Schaetzer gegen statisch, versiegelte Spieler: VPIP +0,0026, Call gegen Raise +0,0040, Flop-Einsatz +0,0046 nat je Entscheidung, auch gegen einen Schaetzer, der die Tischgroesse kennt. Verlust-Merkmal ueber den kurzen Zustand hinaus <= +0,00003 nat: Tilt steckt im kurzen Zustand. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
199 lines
9.1 KiB
Python
199 lines
9.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation messung/innovation/drift - Allan-Varianz und Vergessen im Gegner-Gedaechtnis.
|
|
|
|
Bekannte Wahrheit:
|
|
weisse Bernoulli-Folge => sigma2_A(m) = p(1-p)/m; Zufallsweg => sigma2_A(m) ~ q*m/3 (Anstieg ~ m);
|
|
Handrechnung an einer 8er-Folge; exponentiell gewichteter Schaetzer mit Schrumpfung (Handrechnung);
|
|
Sitzungsgrenzen, Zeitgewicht, Log-Loss, Cluster-Bootstrap mit fester Saat; Stapel-Rekursion == Skalar-Referenz.
|
|
|
|
python3 tests/innovation_drift_spec.py -v
|
|
"""
|
|
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", "innovation", "drift"))
|
|
import numpy as np # noqa: E402
|
|
import drift # noqa: E402
|
|
|
|
ACHT = [1, 0, 1, 1, 0, 0, 1, 0]
|
|
|
|
|
|
class Allan(unittest.TestCase):
|
|
def test_handrechnung_acht(self):
|
|
# m=1: Differenzen -1,1,0,-1,0,1,-1 -> Quadrate 1,1,0,1,0,1,1 -> Mittel 5/7 -> halbe 5/14
|
|
self.assertAlmostEqual(drift.allan_varianz(ACHT, 1), 5 / 14, places=12)
|
|
# m=2: Block-Mittel .5,1,0,.5 -> Differenzen .5,-1,.5 -> Quadrate .25,1,.25 -> Mittel .5 -> halbe .25
|
|
self.assertAlmostEqual(drift.allan_varianz(ACHT, 2), 0.25, places=12)
|
|
# m=4: Mittel .75,.25 -> Differenz .5 -> Quadrat .25 -> halbe .125
|
|
self.assertAlmostEqual(drift.allan_varianz(ACHT, 4), 0.125, places=12)
|
|
|
|
def test_zu_kurz_ist_nan(self):
|
|
self.assertTrue(math.isnan(drift.allan_varianz(ACHT, 5))) # nur ein Block
|
|
|
|
def test_weisses_rauschen_ist_p_mal_1_minus_p_durch_m(self):
|
|
rng = np.random.default_rng(1)
|
|
p = 0.3
|
|
y = (rng.random(400_000) < p).astype(float)
|
|
for m in (25, 100, 400):
|
|
erw = p * (1 - p) / m
|
|
self.assertAlmostEqual(drift.allan_varianz(y, m) / erw, 1.0, delta=0.08)
|
|
|
|
def test_zufallsweg_steigt_mit_m(self):
|
|
rng = np.random.default_rng(2)
|
|
x = np.cumsum(rng.normal(0.0, 1.0, 600_000)) # q = 1 je Schritt, kein Messrauschen
|
|
v1, v2 = drift.allan_varianz(x, 100), drift.allan_varianz(x, 400)
|
|
self.assertAlmostEqual(v2 / v1, 4.0, delta=1.2) # ~ m
|
|
self.assertAlmostEqual(v1 / (100 / 3), 1.0, delta=0.35) # ~ q*m/3
|
|
|
|
def test_erwartung_stationaer(self):
|
|
self.assertAlmostEqual(drift.allan_erwartung_weiss([1] * 30 + [0] * 70, 50), 0.21 / 50, places=12)
|
|
|
|
def test_fit_nichtnegativ_kennt_plateau_und_zufallsweg(self):
|
|
ms = np.array([25.0, 50, 100, 200, 400, 800])
|
|
c, q = 0.004, 0.0002
|
|
c2, q2 = drift.allan_fit(ms, c + q * ms / 3)
|
|
self.assertAlmostEqual(c2, c, places=9)
|
|
self.assertAlmostEqual(q2, q, places=9)
|
|
c0, q0 = drift.allan_fit(ms, np.full(6, 0.003)) # reines Plateau
|
|
self.assertAlmostEqual(c0, 0.003, places=9)
|
|
self.assertEqual(q0, 0.0)
|
|
c1, q1 = drift.allan_fit(ms, 0.0003 * ms / 3) # reiner Zufallsweg
|
|
self.assertAlmostEqual(c1, 0.0, places=12)
|
|
self.assertAlmostEqual(q1, 0.0003, places=9)
|
|
|
|
def test_optimales_fenster(self):
|
|
# m* = sqrt(3 p (1-p) / q)
|
|
self.assertAlmostEqual(drift.optimales_fenster(0.5, 0.0003), math.sqrt(3 * 0.25 / 0.0003), places=9)
|
|
self.assertEqual(drift.optimales_fenster(0.5, 0.0), math.inf)
|
|
|
|
|
|
class Schaetzer(unittest.TestCase):
|
|
def test_statisch_handrechnung(self):
|
|
# y = 1,0,1; k=2, Pool .5, nie vergessen: Vorhersagen vor jeder Beobachtung
|
|
S, N = drift.zaehler_vergessen([1, 0, 1], [1.0, 1.0, 1.0])
|
|
p = drift.schrumpfung(S, N, 2.0, 0.5)
|
|
self.assertEqual(list(N), [0.0, 1.0, 2.0])
|
|
self.assertAlmostEqual(p[0], 0.5)
|
|
self.assertAlmostEqual(p[1], 2 / 3)
|
|
self.assertAlmostEqual(p[2], (1 + 1) / (2 + 2))
|
|
|
|
def test_exponentiell_handrechnung(self):
|
|
# Zerfall .5 je Schritt: S1=.5*(0+1)=.5, N1=.5*(0+1)=.5; S2=.5*(.5+0)=.25, N2=.5*(.5+1)=.75
|
|
S, N = drift.zaehler_vergessen([1, 0, 1], [1.0, 0.5, 0.5])
|
|
np.testing.assert_allclose(S, [0.0, 0.5, 0.25])
|
|
np.testing.assert_allclose(N, [0.0, 0.5, 0.75])
|
|
p = drift.schrumpfung(S, N, 2.0, 0.5)
|
|
np.testing.assert_allclose(p, [0.5, 1.5 / 2.5, 1.25 / 2.75])
|
|
|
|
def test_vorhersage_nutzt_nur_frueheres(self):
|
|
a, b = [1, 0, 1, 1], [1, 0, 1, 0]
|
|
Sa, Na = drift.zaehler_vergessen(a, [1, 0.7, 0.7, 0.7])
|
|
Sb, Nb = drift.zaehler_vergessen(b, [1, 0.7, 0.7, 0.7])
|
|
np.testing.assert_allclose(Sa[:4], Sb[:4]) # letzte Beobachtung unterscheidet sich, Vorhersage nicht
|
|
np.testing.assert_allclose(Na, Nb)
|
|
|
|
def test_schrumpfung_ohne_daten_ist_pool(self):
|
|
self.assertAlmostEqual(float(drift.schrumpfung(np.array([0.0]), np.array([0.0]), 10.0, 0.37)[0]), 0.37)
|
|
|
|
def test_stapel_gleich_skalar(self):
|
|
rng = np.random.default_rng(3)
|
|
lens = [5, 1, 9, 3]
|
|
ys = [rng.integers(0, 2, n).astype(float) for n in lens]
|
|
ds = [rng.uniform(0.3, 1.0, n) for n in lens]
|
|
y = np.concatenate(ys)
|
|
d = np.stack([np.concatenate(ds), np.concatenate([np.ones(n) for n in lens])]) # zwei Parameter
|
|
start = np.cumsum([0] + lens[:-1])
|
|
S, N = drift.zaehler_stapel(y, start, np.array(lens), d)
|
|
for j, (yy, dd) in enumerate(zip(ys, ds)):
|
|
s0, n0 = drift.zaehler_vergessen(yy, dd)
|
|
a, b = start[j], start[j] + lens[j]
|
|
np.testing.assert_allclose(S[0, a:b], s0, atol=1e-5)
|
|
np.testing.assert_allclose(N[0, a:b], n0, atol=1e-5)
|
|
s1, n1 = drift.zaehler_vergessen(yy, np.ones(lens[j]))
|
|
np.testing.assert_allclose(S[1, a:b], s1, atol=1e-5)
|
|
np.testing.assert_allclose(N[1, a:b], n1, atol=1e-5)
|
|
|
|
|
|
class Zeit(unittest.TestCase):
|
|
def test_zeitgewicht(self):
|
|
self.assertAlmostEqual(drift.zeitgewicht(10.0, 10.0), 0.5)
|
|
self.assertAlmostEqual(drift.zeitgewicht(20.0, 10.0), 0.25)
|
|
self.assertEqual(drift.zeitgewicht(0.0, 3.0), 1.0)
|
|
self.assertEqual(drift.zeitgewicht(500.0, math.inf), 1.0)
|
|
|
|
def test_sitzungsgrenzen(self):
|
|
ids = drift.sitzungs_ids([0, 100, 2000, 2100, 5000], 1800)
|
|
self.assertEqual(list(ids), [0, 0, 1, 1, 2])
|
|
|
|
def test_luecke_genau_30_min_bleibt_in_der_sitzung(self):
|
|
self.assertEqual(list(drift.sitzungs_ids([0, 1800, 3601], 1800)), [0, 0, 1])
|
|
|
|
def test_leere_folge(self):
|
|
self.assertEqual(len(drift.sitzungs_ids([], 1800)), 0)
|
|
|
|
|
|
class Verlust(unittest.TestCase):
|
|
def test_logloss(self):
|
|
self.assertAlmostEqual(float(drift.logloss(np.array([0.5]), np.array([1.0]))[0]), math.log(2))
|
|
self.assertAlmostEqual(float(drift.logloss(np.array([0.9]), np.array([1.0]))[0]), -math.log(0.9))
|
|
self.assertAlmostEqual(float(drift.logloss(np.array([0.9]), np.array([0.0]))[0]), -math.log(0.1))
|
|
|
|
def test_logloss_ist_endlich_bei_p_0_und_1(self):
|
|
l = drift.logloss(np.array([0.0, 1.0]), np.array([1.0, 0.0]))
|
|
self.assertTrue(np.all(np.isfinite(l)))
|
|
|
|
|
|
class Bootstrap(unittest.TestCase):
|
|
def test_gleiche_cluster_geben_enges_intervall(self):
|
|
pt, lo, hi = drift.cluster_bootstrap(np.full(50, 2.0), np.full(50, 4.0), 200, 7)
|
|
self.assertAlmostEqual(pt, 0.5)
|
|
self.assertAlmostEqual(lo, 0.5)
|
|
self.assertAlmostEqual(hi, 0.5)
|
|
|
|
def test_feste_saat_gleiches_ergebnis(self):
|
|
z = np.random.default_rng(5).normal(0.2, 1, 80)
|
|
n = np.full(80, 10.0)
|
|
self.assertEqual(drift.cluster_bootstrap(z, n, 300, 11), drift.cluster_bootstrap(z, n, 300, 11))
|
|
|
|
def test_intervall_schliesst_wahres_mittel_und_ist_geordnet(self):
|
|
z = np.random.default_rng(6).normal(0.0, 1, 400)
|
|
pt, lo, hi = drift.cluster_bootstrap(z, np.ones(400), 400, 3)
|
|
self.assertLess(lo, pt)
|
|
self.assertLess(pt, hi)
|
|
self.assertLess(lo, 0.0)
|
|
self.assertGreater(hi, 0.0)
|
|
self.assertLess(hi - lo, 0.4) # ~ 4 * 1/sqrt(400) = 0.2
|
|
|
|
def test_verschobenes_mittel_liegt_ausserhalb_von_null(self):
|
|
z = np.random.default_rng(8).normal(0.5, 1, 400)
|
|
_, lo, _ = drift.cluster_bootstrap(z, np.ones(400), 400, 3)
|
|
self.assertGreater(lo, 0.0)
|
|
|
|
|
|
class Eimer(unittest.TestCase):
|
|
def test_deterministisch_und_gleichmaessig(self):
|
|
k = np.arange(1, 40_001, dtype=np.uint64) * np.uint64(7919)
|
|
e = drift.spieler_eimer(k, 4)
|
|
np.testing.assert_array_equal(e, drift.spieler_eimer(k, 4))
|
|
anteil = np.bincount(e, minlength=4) / len(k)
|
|
self.assertTrue(np.all(np.abs(anteil - 0.25) < 0.02))
|
|
|
|
def test_offen_und_versiegelt_sind_disjunkt_und_decken_stichprobe(self):
|
|
k = np.random.default_rng(9).integers(1, 2**62, 20_000, dtype=np.int64).astype(np.uint64)
|
|
st, versiegelt = drift.stichprobe_und_versiegelt(k)
|
|
self.assertTrue(np.all(st[versiegelt])) # versiegelt liegt in der Stichprobe
|
|
self.assertGreater(versiegelt.sum(), 0)
|
|
self.assertGreater((st & ~versiegelt).sum(), 0) # offene Haelfte nicht leer
|
|
self.assertAlmostEqual(st.mean(), 0.25, delta=0.02)
|
|
self.assertAlmostEqual(versiegelt.sum() / st.sum(), 0.5, delta=0.05)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|