assembly-economics/simulations/gadm02/test_matching.py
tegwick 0ccf4f7cf7 ASSEMBLY-WP-0017-T04 passed: flips judged on anchor weights (6/6 switches in 0.5 y, 0 false flips); reliance on investment documented; T06 AI-specific investment series
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assistant: claude-code
Assistant-Model: opus
Assistant-Process: 14913@bnt-lap001
Assistant-Session: add7ef69-b901-4756-9a67-34cf1125d96f
2026-10-13 22:07:26 +02:00

36 lines
1.5 KiB
Python

"""Tests for the matching engine (ASSEMBLY-WP-0012)."""
import math
import unittest
import gadm04 as g
import matching as m
class TestMatching(unittest.TestCase):
def test_fit_recovers_alignment_and_scale(self):
rows = g.simulate(T=m.T_PATH, record_every=1)
pts = [(tau, 25.0 * m.interp(rows, 3.0 + tau, "Ko")) for tau in (0.0, 0.25, 0.5, 0.75, 1.0)]
ll, t0, ls = m.fit_series(rows, pts, 0.03)
self.assertAlmostEqual(t0, 3.0, places=6)
self.assertAlmostEqual(math.exp(ls), 25.0, places=4)
self.assertGreater(ll, -1e-6)
def test_flip_needs_two_consecutive_strong_evaluations(self):
a, b = ("B2", "off"), ("B3", "off")
def ev(t, wa):
return (t, {a: {"w": wa}, b: {"w": 1 - wa}})
self.assertEqual(m.flips([ev(0, 0.9), ev(1, 0.1), ev(2, 0.9)]), [])
self.assertEqual(m.flips([ev(0, 0.9), ev(1, 0.1), ev(2, 0.1)]), [(2, a, b)])
self.assertEqual(m.flips([ev(0, 0.5), ev(1, 0.1), ev(2, 0.1)]), []) # nothing established yet
def test_anchor_flip_ignores_unidentifiable_loop_split(self):
def ev(t, w):
return (t, {("B3", "off"): {"w": w[0]}, ("B2", "off"): {"w": w[1]}, ("B2", "on"): {"w": w[2]}})
evals = [ev(0, (0.9, 0.05, 0.05)), ev(1, (0.1, 0.45, 0.45)), ev(2, (0.1, 0.45, 0.45))]
self.assertEqual(m.flips(evals), []) # full candidates: split, no flip
self.assertEqual(m.flips(evals, by=0), [(2, ("B3",), ("B2",))])
if __name__ == "__main__":
unittest.main()