#!/usr/bin/env python3 """Spezifikation messung/innovation/tilt_lambda - Verlust-Merkmale (letzte 30 Haende, Sitzung) als Logit-Versatz. python3 tests/innovation_tilt_lambda_spec.py -v """ import os 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", "tilt_lambda")) import numpy as np # noqa: E402 import tilt # noqa: E402 class Fenster(unittest.TestCase): def test_summe_der_letzten_w_ohne_aktuelle_hand(self): r = tilt.fenster_vorher(np.array([1.0, 2, 3, 4, 5]), np.array([1, 0, 0, 0, 0], bool), 2) np.testing.assert_allclose(r, [0, 1, 3, 5, 7]) def test_spielerwechsel_setzt_zurueck(self): r = tilt.fenster_vorher(np.array([1.0, 2, 3, 4]), np.array([1, 0, 1, 0], bool), 5) np.testing.assert_allclose(r, [0, 1, 0, 3]) def test_fenster_groesser_als_historie(self): r = tilt.fenster_vorher(np.array([5.0, -1, -1]), np.array([1, 0, 0], bool), 30) np.testing.assert_allclose(r, [0, 5, 4]) class Sitzung(unittest.TestCase): def test_summe_vor_der_hand_in_derselben_sitzung(self): r = tilt.sitzung_vorher(np.array([1.0, 2, 3, 4]), np.array([0, 0, 1, 1])) np.testing.assert_allclose(r, [0, 1, 0, 3]) def test_eine_sitzung(self): r = tilt.sitzung_vorher(np.array([-1.0, -2, -3]), np.array([7, 7, 7])) np.testing.assert_allclose(r, [0, -1, -3]) class Merkmale(unittest.TestCase): def test_schwellen(self): b = tilt.bits(np.array([-50.0, -49.9, 50.0, 0.0]), np.array([-100.0, 0.0, 100.0, 99.0])) self.assertEqual(list(b), [1 | 2, 0, 4 | 8, 0]) class Fit(unittest.TestCase): def test_kennt_wahren_versatz(self): rng = np.random.default_rng(1) n = 300_000 o = rng.normal(-0.5, 1.0, n) X = np.stack([(rng.random(n) < 0.2), (rng.random(n) < 0.1)], axis=1).astype(float) beta = np.array([0.7, -0.4]) y = (rng.random(n) < 1 / (1 + np.exp(-(o + X @ beta)))).astype(float) b = tilt.logit_versatz(o, y, X) np.testing.assert_allclose(b, beta, atol=0.06) def test_ohne_signal_null(self): rng = np.random.default_rng(2) n = 200_000 o = rng.normal(0, 1, n) X = (rng.random((n, 1)) < 0.3).astype(float) y = (rng.random(n) < 1 / (1 + np.exp(-o))).astype(float) self.assertAlmostEqual(float(tilt.logit_versatz(o, y, X)[0]), 0.0, delta=0.05) def test_leere_spalte_gibt_null(self): o = np.zeros(10) b = tilt.logit_versatz(o, np.array([1, 0] * 5, float), np.zeros((10, 1))) self.assertEqual(float(b[0]), 0.0) if __name__ == "__main__": unittest.main()