diff --git a/tests/core/test_model.py b/tests/core/test_model.py new file mode 100644 index 0000000..e0bf299 --- /dev/null +++ b/tests/core/test_model.py @@ -0,0 +1,185 @@ +"""The route model's arithmetic (core/atlas/model.py) on small hand-made datasets whose answers are worked out by hand +in the comments: the hierarchical back-off, the empirical-Bayes concentration and its guard, the gate on the shadow, +which bonds an encounter allows, the first-attempt given, and the reproducible random numbers.""" +import unittest + +from atlas import model, solver + + +def fs_run(direction, fs, company=None, left=None): + """A prepared run that says only which dragon came first (the fs roll).""" + return {"direction": direction, "fs": fs, "company": company, "left": left} + + +def tree_run(attempt, shadow, scream="Investigate the scream", clearing="Help your squadmate"): + """A prepared run that reached the tree; the shadow's gate is open unless scream / clearing say otherwise.""" + v = {"attempt": attempt, "scream": scream, "shadow": shadow} + if clearing: + v["clearing"] = clearing + return v + + +class BackOff(unittest.TestCase): + def test_counts_at_every_level_until_a_context_step_is_missing(self): + T = model.count_tables([fs_run("East", "Red", "Alone", model.ALONE), fs_run("West", "Brown")]) + t = T["fs"] + self.assertEqual(t["0:"], {"Red": 1, "Brown": 1}) + self.assertEqual(t["1:East"], {"Red": 1}) + self.assertEqual(t["2:East|Alone"], {"Red": 1}) + self.assertEqual(t["3:East|Alone|(alone)"], {"Red": 1}) + self.assertEqual(t["1:West"], {"Brown": 1}) + self.assertFalse(any(k.startswith("2:West") for k in t)) # no company: the West run stops at level 1 + + def test_weights_scale_the_counts_and_zero_leaves_a_run_out(self): + runs = [fs_run("East", "Red"), fs_run("East", "Red"), fs_run("East", "Brown")] + T = model.count_tables(runs, [{"fs": 0.5}, {"fs": 0}, {}]) + self.assertEqual(T["fs"]["0:"], {"Red": 0.5, "Brown": 1}) + + def test_each_level_is_shrunk_toward_the_level_above(self): + # 3 West runs saw a Red, 1 East run a Brown. Outcomes in game.yaml order: Red, Brown, Green, Orange. + T = model.count_tables([fs_run("West", "Red")] * 3 + [fs_run("East", "Brown")]) + p = model.Params(T, "mean", alphas={"fs": 12}) + got = lambda state: [float(p.chance("fs", state)[k][0]) for k in ("Red", "Brown", "Green", "Orange")] # noqa: E731 + # level 0: counts + 1/2 = [3.5, 1.5, .5, .5] / 6 = [7, 3, 1, 1] / 12 + top = [7 / 12, 3 / 12, 1 / 12, 1 / 12] + for a, b in zip(got({}), top): + self.assertAlmostEqual(a, b) + # East: its own count [0, 1, 0, 0] + alpha 12 x level 0 = [7, 4, 1, 1] / 13 + for a, b in zip(got({"direction": "East"}), [7 / 13, 4 / 13, 1 / 13, 1 / 13]): + self.assertAlmostEqual(a, b) + # West: [3, 0, 0, 0] + [7, 3, 1, 1] = [10, 3, 1, 1] / 15 + for a, b in zip(got({"direction": "West"}), [10 / 15, 3 / 15, 1 / 15, 1 / 15]): + self.assertAlmostEqual(a, b) + # a direction nobody took: no counts of its own, so exactly the level above + for a, b in zip(got({"direction": "Center"}), top): + self.assertAlmostEqual(a, b) + + +class Concentration(unittest.TestCase): + def fit(self, runs): + return model.fit_alpha(model.count_tables(runs), "fs")["alpha"] + + def test_routes_that_differ_get_a_small_concentration(self): + self.assertLessEqual(self.fit([fs_run("East", "Red")] * 200 + [fs_run("West", "Brown")] * 200), 2) + + def test_routes_that_do_not_differ_get_a_large_one(self): + same = [fs_run(d, c) for d in ("East", "West") for c in ("Red", "Brown") for _ in range(100)] + self.assertGreaterEqual(self.fit(same), 500) + + def test_guard_a_roll_seen_only_one_way_is_pooled_fully(self): + # without the guard the fit would let every route's chance of the unseen outcome go to zero + self.assertEqual(self.fit([fs_run("East", "Red")] * 50 + [fs_run("West", "Red")] * 5), max(model.ALPHA_GRID)) + + def test_no_context_levels_keeps_the_default(self): + T = model.count_tables([{"fs": "Red"}, {"fs": "Brown"}]) # no direction: only level 0 + self.assertEqual(model.fit_alpha(T, "fs"), {"alpha": model.ALPHA, "logLik": None}) + + def test_levels_that_are_not_pooled_are_not_fitted(self): + # the shadow's first context step (the attempt) is `distinct`: its level is not one of the fitted pairs + T = model.count_tables([tree_run("First attempt", True)] * 10 + [tree_run("Later attempt", False)] * 10) + self.assertEqual(model.fit_alpha(T, "shadow")["alpha"], model.ALPHA) + + +class Gate(unittest.TestCase): + def test_gate_open(self): + self.assertTrue(model.gate_open("shadow", tree_run("First attempt", True))) + self.assertFalse(model.gate_open("shadow", tree_run("First attempt", True, scream="Keep walking", clearing=None))) + self.assertFalse(model.gate_open("shadow", tree_run("First attempt", True, clearing="Walk away"))) + self.assertTrue(model.gate_open("fs", {})) # an ungated roll + + def test_runs_through_a_closed_gate_say_nothing_about_the_roll(self): + runs = [tree_run("Later attempt", False)] * 4 + [tree_run("Later attempt", False, scream="Keep walking", clearing=None)] * 6 \ + + [tree_run("Later attempt", True, clearing="Walk away")] # a shadow through a closed gate: not counted + self.assertEqual(model.count_tables(runs)["shadow"]["0:"], {"no": 4}) + + def test_a_closed_gate_always_gives_otherwise(self): + p = model.Params(model.count_tables([tree_run("Later attempt", True)] * 5), "mean") + c = p.chance("shadow", {"scream": "Keep walking"}) + self.assertEqual((float(c["yes"][0]), float(c["no"][0])), (0.0, 1.0)) + self.assertGreater(float(p.chance("shadow", tree_run(None, None))["yes"][0]), 0.5) + + +class FirstAttempt(unittest.TestCase): + # first attempts: 1 shadow in 2; later attempts: none in 8 + RUNS = [tree_run("First attempt", True), tree_run("First attempt", False)] + [tree_run("Later attempt", False)] * 8 + + def test_each_attempt_is_its_own_roll_with_a_token_prior(self): + p = model.Params(model.count_tables(self.RUNS), "mean", alphas={"shadow": 20}) + gate = {"scream": "Investigate the scream", "clearing": "Help your squadmate"} + # level 0: [yes 1, no 9] + 1/2 = [1.5, 9.5] / 11. The attempt level is distinct, so its prior weighs one run + # (not alpha = 20): first [1 + 1.5/11, 1 + 9.5/11] / 3, later [0 + 1.5/11, 8 + 9.5/11] / 9 + first = float(p.having(attempt="First attempt").chance("shadow", gate)["yes"][0]) + later = float(p.chance("shadow", gate)["yes"][0]) # the default given is a later attempt + self.assertAlmostEqual(first, (1 + 1.5 / 11) / 3) + self.assertAlmostEqual(later, (1.5 / 11) / 9) + + def test_givens_and_their_names(self): + self.assertEqual(model.DEFAULT_GIVEN, {"attempt": "Later attempt"}) + self.assertEqual(solver.other_givens(), [{"attempt": "First attempt"}]) + self.assertEqual(solver.given_key({"attempt": "First attempt"}), "firstAttempt") + + +class Bonds(unittest.TestCase): + def test_can_bond(self): + self.assertTrue(model.can_bond("tree-Greens", "Green")) + self.assertTrue(model.can_bond("tree-Greens", "Orange")) # an Orange or a Brown can swoop in + self.assertTrue(model.can_bond("tree-Greens", "Brown")) + self.assertFalse(model.can_bond("tree-Greens", "Red")) + self.assertTrue(model.can_bond("meadow-Blue", "burn")) + self.assertFalse(model.can_bond("meadow-Blue", "Orange")) + self.assertFalse(model.can_bond("shadow-Black", "Blue")) + + def test_encounter_odds_count_only_possible_bonds(self): + enc = lambda o: {"encCtx": "tree-Greens", "encOutcome": o} # noqa: E731 + T = model.count_tables([enc("Green"), enc("Green"), enc("burn"), enc("Orange"), enc("Red")]) + self.assertEqual(T["encounter"]["tree-Greens"]["outcomes"], {"Green": 2, "burn": 1, "Orange": 1}) + e = model.Params(T, "mean").encounter("tree-Greens") + # outcomes that can happen here and were seen (or are the encounter's own colour), + 1/2: burn, Orange, Green + self.assertEqual(list(e), ["burn", "Orange", "Green"]) + for k, n in (("burn", 1.5), ("Orange", 1.5), ("Green", 2.5)): + self.assertAlmostEqual(float(e[k][0]), n / 5.5) + + +class Solved(unittest.TestCase): + """The gate and the given carried through the solver: no Black without investigating and helping.""" + + def setUp(self): + runs = self.RUNS = FirstAttempt.RUNS + [{"encCtx": "shadow-Black", "encOutcome": "Black"}] * 3 + self.T = model.count_tables(runs) + self.support, _ = model.support_from(runs, 0) + + def black(self, params, **only): + return float(solver.solve(params, "Black", {**self.support, **only}).value[0][0]) + + def test_closing_the_gate_makes_the_black_impossible(self): + p = model.Params(self.T, "mean") + self.assertGreater(self.black(p), 0) + self.assertEqual(self.black(p, scream=["Keep walking"]), 0.0) + self.assertEqual(self.black(p, clearing=["Walk away"]), 0.0) + + def test_a_first_attempt_is_better_for_the_black(self): + p = model.Params(self.T, "mean") + self.assertGreater(self.black(p.having(attempt="First attempt")), 5 * self.black(p)) + + +class Random(unittest.TestCase): + def test_xorshift_matches_the_javascript_model(self): + r = model.Rng(1) + # 1 ^ 1<<13 = 8193; ^ >>17 leaves it; ^ <<5: 8193 ^ 262176 = 270369 + self.assertEqual(r(), 270369 / 2 ** 32) + self.assertEqual(model.Rng(0).s, 1) # a zero seed would stay zero forever + + def test_dirichlet_draws_average_to_the_normalised_counts(self): + r = model.Rng(20261101) + draws = [model.dirichlet([2, 6, 2], r) for _ in range(4000)] + for i, want in enumerate((0.2, 0.6, 0.2)): + self.assertAlmostEqual(sum(d[i] for d in draws) / len(draws), want, delta=0.01) + + def test_lgamma(self): + import math + for x in (0.5, 1, 2.5, 10, 300): + self.assertAlmostEqual(model.lgamma(x), math.lgamma(x), places=8) + + +if __name__ == "__main__": + unittest.main()