Files
11b4a0398e Core: a small JS advisor reads the game from policy.json; engine.js is gone (#13)
Step 4b of the Python-first core.

- core/solver.js is now a 250-line advisor (was 900 lines): it reads the game (core/spec/game.yaml,
  published inside policy.json as model.game) and the count tables, and re-solves from any point in
  a run with the same interpreter as the Python model. Same API for the site and Ken's bot
  (advisor, recommendOption, solverHistory). The bot's on-screen text for each option moves to
  game.yaml (bot_text); the deadly rolls carry their risk labels there too.
- tests/core/advisor.test.js: the advisor gives exactly the Python model's values and choices for
  every colour and goal (to 1e-12, every decision on every published route), walks a run in the
  game's order, and maps advice onto the bot's screen.
- Removed core/engine.js and everything that only compared it with Python (tests/parity/run_engine,
  compare_runs, compare_results, compare_routes, compare_solver, compare_stats, analysis/stats/
  analyze.py). In their place: tests/core/test_stats.py checks the statistics against known answers,
  test_solver.py checks the route model on a synthetic world (tests/fixtures/synthetic_policy.py).
- analysis/network/build-graph.js reads the Python-coded runs; graph.json is byte-identical.
- docs/BOT_INTEGRATION.md: what changed for the bot (history keys, new choices, re-sync all three files).


Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 22:01:32 +00:00

50 lines
1.9 KiB
Python

"""The statistics behind every number on the page (analysis/stats/stats_core.py), against known answers."""
import sys
import unittest
from pathlib import Path
import numpy as np
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "analysis/stats"))
import stats_core as S # noqa: E402
class Stats(unittest.TestCase):
def test_wilson(self):
p, lo, hi = S.wilson(5, 10) # textbook: 0.2366 to 0.7634
self.assertAlmostEqual(lo, 0.2366, places=4)
self.assertAlmostEqual(hi, 0.7634, places=4)
self.assertEqual(S.wilson(0, 0), (None, None, None))
self.assertEqual(S.wilson(0, 20)[1], 0.0)
def test_odds_ratio_and_fisher(self):
or_, lo, hi, p, corr = S.odds_ratio(3, 1, 1, 3) # Fisher's lady tasting tea: two-sided p = 34/70
self.assertAlmostEqual(or_, 9.0)
self.assertLess(lo, or_)
self.assertGreater(hi, or_)
self.assertAlmostEqual(p, 34 / 70)
self.assertFalse(corr)
self.assertTrue(S.odds_ratio(3, 0, 1, 4)[4]) # a zero cell gets the +0.5 correction
def test_benjamini_hochberg(self):
q = S.bh([0.01, 0.04, 0.03, 0.5])
self.assertEqual([round(x, 4) for x in q], [0.04, 0.0533, 0.0533, 0.5])
def test_mantel_haenszel_equals_the_odds_ratio_when_strata_agree(self):
or_, lo, hi, p = S.mantel_haenszel([[10, 20, 5, 40], [20, 40, 10, 80]])
self.assertAlmostEqual(or_, 4.0)
self.assertLess(p, 0.001)
self.assertIsNone(S.mantel_haenszel([[0, 5, 0, 5]]))
def test_logistic_ridge_recovers_a_known_effect(self):
rng = np.random.default_rng(1)
x = rng.integers(0, 2, 20000)
y = (rng.random(20000) < 1 / (1 + np.exp(-(-1 + 1.2 * x)))).astype(float)
b, se = S.logistic_ridge(np.column_stack([np.ones_like(x), x]), y, lam=1.0)
self.assertAlmostEqual(b[1], 1.2, delta=0.1)
self.assertTrue(np.all(se > 0))
if __name__ == "__main__":
unittest.main()