"""Independent high-precision recomputation of the published teaching example.""" import hashlib import json from decimal import Decimal, localcontext from pathlib import Path def verify(): root = Path(__file__).resolve().parent data = json.loads((root / "magnetic-asymmetry-results.json").read_text()) checks = 0 with localcontext() as context: context.prec = 70 for row in data["rows"]: p = Decimal(0) hazard = Decimal(0) for step in row["steps"]: a, b, t = (Decimal(str(step[key])) for key in ("aHz", "bHz", "seconds")) q = a / (a + b) p = q + (p - q) * (-(a + b) * t).exp() hazard += a * t if abs(p - Decimal(str(step["finalB"]))) > Decimal("2e-15"): raise ValueError("step differs from Decimal recomputation") checks += 1 for actual, expected in [(row["finalB"], p), (row["firstReversalProbability"], 1 - (-hazard).exp())]: if abs(Decimal(str(actual)) - expected) > Decimal("2e-15"): raise ValueError("result differs from Decimal recomputation") checks += 1 for name, expected in data["provenance"]["sourceSha256"].items(): if hashlib.sha256((root / name).read_bytes()).hexdigest() != expected: raise ValueError("source hash mismatch: " + name) checks += 1 print(json.dumps({"decimalPrecision": 70, "checks": checks, "status": "pass"})) if __name__ == "__main__": verify()