Karte vor dem Lauf (Tor: ab welchem Fenster >= 80 % des Jahres), Spec 34 Checks, nur Summen im Ergebnis. Fuer die Kaufentscheidung hhDealer. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
111 lines
5.9 KiB
Python
111 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation der Datenmengen-Messung (messung/motor/gedaechtnis2/datenmenge/datenmenge.py): Fenster, Abdeckung, Gewinn
|
|
(Unbekannte = 0), Anteil am Jahr, Tor, k-Wahl, Bootstrap. Jede Zahl ist von Hand gerechnet; Ausgabe: "N Checks, alle ok".
|
|
|
|
python3 tests/gedaechtnis2_datenmenge_spec.py
|
|
"""
|
|
import math
|
|
import os
|
|
import sys
|
|
|
|
import numpy as np
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "betrieb")):
|
|
_w = os.path.dirname(_w)
|
|
sys.path.append(os.path.join(_w, "messung", "motor", "gedaechtnis2", "datenmenge"))
|
|
import datenmenge as D # noqa: E402
|
|
|
|
ZAEHLER = {"n": 0}
|
|
TAG = 86400
|
|
|
|
|
|
def check(name, bedingung):
|
|
ZAEHLER["n"] += 1
|
|
if not bedingung:
|
|
print("FEHLER:", name)
|
|
raise SystemExit(1)
|
|
|
|
|
|
def nah(a, b, tol=1e-9):
|
|
return abs(a - b) <= tol
|
|
|
|
|
|
# ---------------------------------------------------------------- Pruefzeitraum und Fenster
|
|
# Daten bis Sekunde 1000 Tage*TAG; letzte 30 Tage: [t_max + 1 - 30 d, t_max + 1)
|
|
t_max = 100 * TAG
|
|
s, e = D.pruefzeitraum(t_max, 30, 0)
|
|
check("Pruefzeitraum Ende", e == t_max + 1)
|
|
check("Pruefzeitraum Start 30 Tage davor", s == t_max + 1 - 30 * TAG)
|
|
# Probe: die 30 Tage davor, Ende = Start des Hauptzeitraums
|
|
s2, e2 = D.pruefzeitraum(t_max, 30, 30)
|
|
check("Probe grenzt an Haupt", e2 == s and s2 == s - 30 * TAG)
|
|
a, b = D.fenster(s, 91)
|
|
check("Fenster endet am Pruefstart", b == s and a == s - 91 * TAG)
|
|
t = np.array([a - 1, a, b - 1, b, e - 1, e])
|
|
check("im Intervall halboffen", list(D.im_intervall(t, a, b)) == [False, True, True, False, False, False])
|
|
check("Monatsfenster Tage", D.FENSTER_TAGE == [30, 91, 182, 365])
|
|
|
|
# ---------------------------------------------------------------- Abdeckung
|
|
# Spieler: Haende im Fenster [0, 10, 60, 300, 2000], Begegnungen im Pruefzeitraum [5, 5, 10, 20, 60] (Summe 100)
|
|
nf = np.array([0, 10, 60, 300, 2000])
|
|
npz = np.array([5, 5, 10, 20, 60])
|
|
check("Abdeckung >= 1: 5+10+20+60 = 95 von 100", nah(D.abdeckung(nf, npz, 1), 0.95))
|
|
check("Abdeckung >= 50: 10+20+60 = 90", nah(D.abdeckung(nf, npz, 50), 0.90))
|
|
check("Abdeckung >= 200: 20+60 = 80", nah(D.abdeckung(nf, npz, 200), 0.80))
|
|
check("Abdeckung >= 1000: 60", nah(D.abdeckung(nf, npz, 1000), 0.60))
|
|
check("Abdeckung Schwelle genau erreicht zaehlt", nah(D.abdeckung(np.array([50]), np.array([7]), 50), 1.0))
|
|
check("Abdeckung ohne Begegnungen = nan", math.isnan(D.abdeckung(nf, np.zeros(5), 1)))
|
|
|
|
# ---------------------------------------------------------------- Stammspieler
|
|
check("Stamm >= 200", list(D.stamm_maske(np.array([199, 200, 201]))) == [False, True, True])
|
|
|
|
# ---------------------------------------------------------------- Gewinn (Log-Loss Pool minus Profil, positiv = besser)
|
|
# Profil 10 von 10, k = 10, Pool 0,5: p = 15/20 = 0,75; Pruefmenge 4 von 4: 4 ln(0,75/0,5)
|
|
g = D.gewinn_je_spieler(np.array([10.0]), np.array([10.0]), np.array([4.0]), np.array([4.0]), 10, 0.5)
|
|
check("Gewinn Treffer", nah(g[0], 4 * math.log(1.5)))
|
|
# gleiches Profil, Pruefmenge 0 von 4: 4 ln(0,25/0,5) = -4 ln 2 (negativ)
|
|
g = D.gewinn_je_spieler(np.array([10.0]), np.array([10.0]), np.array([0.0]), np.array([4.0]), 10, 0.5)
|
|
check("Gewinn Fehlschuss negativ", nah(g[0], -4 * math.log(2)))
|
|
# Unbekannter (n = 0): p = Pool, Gewinn exakt 0, egal was in der Pruefmenge passiert
|
|
g = D.gewinn_je_spieler(np.array([0.0, 0.0]), np.array([0.0, 0.0]), np.array([3.0, 0.0]), np.array([5.0, 9.0]), 30, 0.2)
|
|
check("Unbekannte Gewinn 0", g[0] == 0.0 and g[1] == 0.0)
|
|
# Spieler ohne Entscheidung im Pruefzeitraum: Gewinn 0
|
|
g = D.gewinn_je_spieler(np.array([10.0]), np.array([10.0]), np.array([0.0]), np.array([0.0]), 10, 0.5)
|
|
check("keine Pruefentscheidung Gewinn 0", g[0] == 0.0)
|
|
# Summe ueber Spieler = Summe der Einzelgewinne
|
|
g = D.gewinn_je_spieler(np.array([10.0, 0.0]), np.array([10.0, 0.0]), np.array([4.0, 2.0]), np.array([4.0, 4.0]), 10, 0.5)
|
|
check("Vektor", nah(g[0], 4 * math.log(1.5)) and g[1] == 0.0)
|
|
|
|
# ---------------------------------------------------------------- Anteil am Jahr und Tor
|
|
check("Anteil 0,3 von 0,4", nah(D.anteil(0.3, 0.4), 0.75))
|
|
check("Anteil Jahr gegen sich = 1", D.anteil(0.4, 0.4) == 1.0)
|
|
check("Anteil negatives Jahr = nan", math.isnan(D.anteil(0.1, 0.0)))
|
|
check("Tor erstes Fenster >= 0,8 (genau 0,8 zaehlt)", D.tor_fenster([30, 91, 182, 365], [0.5, 0.79, 0.8, 1.0]) == 182)
|
|
check("Tor spaet", D.tor_fenster([30, 91, 182, 365], [0.5, 0.7, 0.79, 1.0]) == 365)
|
|
check("Tor nie", D.tor_fenster([30, 91, 182, 365], [0.1, 0.2, 0.3, 0.4]) is None)
|
|
check("Tor nan wird uebersprungen", D.tor_fenster([30, 91], [float("nan"), 0.9]) == 91)
|
|
|
|
# ---------------------------------------------------------------- k-Wahl
|
|
# Profil immer 50/50 Treffer, Pruefmenge immer 50/50: groesstes Gewicht des Profils = kleinstes k
|
|
check("k klein bei treuem Profil", D.waehle_k(np.array([50.0, 50.0]), np.array([50.0, 50.0]), 50, 50, 0.5) == 1)
|
|
# Profil 50/50, Pruefmenge 0/50: Profil taeuscht, groesstes k (naechst am Pool)
|
|
check("k gross bei taeuschendem Profil", D.waehle_k(np.array([50.0, 50.0]), np.array([0.0, 0.0]), 50, 50, 0.5) == 300)
|
|
|
|
# ---------------------------------------------------------------- Bootstrap ueber Spieler
|
|
X = np.array([[2.0, 1.0]] * 4)
|
|
w = D.bootstrap_summen(X, 20, 1)
|
|
check("Bootstrap Form", w.shape == (20, 2))
|
|
check("Bootstrap konstante Zeilen: Summen 8 und 4", np.all(w[:, 0] == 8.0) and np.all(w[:, 1] == 4.0))
|
|
X = np.array([[1.0, 0.0], [0.0, 1.0]])
|
|
w = D.bootstrap_summen(X, 200, 1)
|
|
check("Bootstrap zieht genau c Spieler", np.all(w.sum(axis=1) == 2.0))
|
|
check("Bootstrap gleiche Saat gleiche Zahl", np.array_equal(D.bootstrap_summen(X, 50, 7), D.bootstrap_summen(X, 50, 7)))
|
|
check("Bootstrap andere Saat anders", not np.array_equal(D.bootstrap_summen(X, 50, 7), D.bootstrap_summen(X, 50, 8)))
|
|
lo, hi = D.intervall(np.arange(0, 1001, dtype=float))
|
|
check("Intervall 2,5 und 97,5 Prozent", nah(lo, 25.0) and nah(hi, 975.0))
|
|
lo, hi = D.intervall(np.array([1.0, float("nan"), 3.0, float("inf")]))
|
|
check("Intervall ignoriert nicht endliche", lo >= 1.0 and hi <= 3.0)
|
|
|
|
print("%d Checks, alle ok" % ZAEHLER["n"])
|