Jump to content

The Principia Machina/TPC-PY/diff beta.py

From The Invariance Itself
"""diff_beta 0.1.0: the cross-residence differential for tpc_beta 0.1.0.
Random (a, b, x, p, level) over the ranges the scores use (shapes from
1e-2 to 1e4, weights that are sums of aged conduct weights), both
residences on the same inputs, rendered through canon_float, 0
differences the pass. Usage: python3 diff_beta.py [seed] [cases]."""
import json, os, random, subprocess, sys, tempfile
here = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, here)
from tpc_beta import run_case
seed = int(sys.argv[1]) if len(sys.argv) > 1 else 1
count = int(sys.argv[2]) if len(sys.argv) > 2 else 20000
random.seed(seed)
def shape():
    r = random.random()
    if r < 0.3: return 1 + random.randint(0, 40)
    if r < 0.6: return round(1 + random.uniform(0, 30) * random.random(), 6)
    if r < 0.8: return round(10 ** random.uniform(-2, 4), 6)
    return random.choice([0.5, 1, 2, 3, 0.001, 100, 1000, 5000])
cases = []
for t in range(count):
    fn = random.choice(["mean", "interval", "betainc", "betainc_inv", "lgamma", "canon"])
    a, b = shape(), shape()
    if fn == "mean": args = {"a": a, "b": b}
    elif fn == "interval": args = {"a": a, "b": b, "level": random.choice([0.95, 0.9, 0.99, 0.5, round(random.random(), 4)])}
    elif fn == "betainc": args = {"a": a, "b": b, "x": random.choice([0, 1, round(random.random(), 8), round(random.random() ** 4, 10), 0.5])}
    elif fn == "betainc_inv": args = {"a": a, "b": b, "p": random.choice([0, 1, round(random.random(), 8), 0.025, 0.975, 0.5])}
    elif fn == "lgamma": args = {"x": random.choice([shape(), round(random.uniform(0, 1), 6), round(random.uniform(0, 200), 3)])}
    else:
        r = random.random()
        v = random.uniform(-1, 1) * 10 ** random.randint(-20, 20) if r < 0.8 else random.choice([0.0, -0.0, 1e-4, 1e-5, 1e12, 1e11, 123456789012345.0, 999999999999.5])
        args = {"x": v}
    cases.append({"fn": fn, "args": args})
CASES_PATH = os.environ.get("TPC_DIFF_CASES")  # named: kept, for crosspy.py
if not CASES_PATH:
    fd, CASES_PATH = tempfile.mkstemp(prefix="tpc_diff_", suffix=".json")  # outside the tree
    os.close(fd)
with open(CASES_PATH, "w", encoding="utf-8") as f:
    json.dump(cases, f)
py = [run_case(c["fn"], c["args"]) for c in cases]
php = json.loads(subprocess.run(["php", os.path.join(here, "diff_beta.php"), CASES_PATH], capture_output=True, text=True, check=True).stdout)
if not os.environ.get("TPC_DIFF_CASES"):
    os.remove(CASES_PATH)
diffs = sum(1 for a, b in zip(py, php) if a != b)
print("seed", seed, "cases", len(cases), "differences", diffs, "errors", sum(1 for x in py if x.startswith("ERROR")))
for c, a, b in zip(cases, py, php):
    if a != b:
        print(c); print(a); print("---"); print(b); break