"""Independent occurrence-label graph checker and exhaustive new toys.""" import collections import itertools import json import os import resource import sys from fractions import Fraction def legal(a, b): return any( all((r + offset) % 5 for offset in (0, 2, a, a + 2, a + b, a + b + 2)) for r in range(5) ) def graph_moments(counts, cutoff, keep_legal): # Treat each occurrence as a distinct vertex. The edge predicate is symmetric. values = sorted(counts) n = sum(counts.values()) degree = {} for a in values: degree[a] = sum( counts[b] - (a == b) for b in values if a + b > cutoff and legal(a, b) == keep_legal ) directed_edges = sum(counts[a] * degree[a] for a in values) wedges = sum(counts[a] * degree[a] * (degree[a] - 1) for a in values) disjoint = directed_edges ** 2 - 4 * wedges - 2 * directed_edges mean = Fraction(directed_edges, n) second = mean + Fraction(2 * wedges, n * (n - 1)) second += Fraction(disjoint, n * (n - 1)) return mean, second, (directed_edges, wedges, disjoint) def main(): data = json.load(open(sys.argv[1])) artifact = json.load(open(sys.argv[2])) assert artifact["scope"] == "New exact interior-half edge moments; no census or null draws." assert artifact["null"] == "Uniform permutations of the reported half multiset; NOT conditioned on admissibility." assert artifact["forbidden_residue_pairs_mod5"] == [ [1, 1], [1, 3], [2, 4], [3, 1], [4, 2], [4, 4] ] half = artifact["local_not_global_witness_half"] assert half == [12, 12, 12, 6, 30] cycle = half + [6] + half[::-1] assert sum(cycle) == 150 assert all(legal(a, b) for a, b in zip(cycle, cycle[1:] + cycle[:1])) starts = [0] for gap in cycle[:-1]: starts.append(starts[-1] + gap) assert not any( all((r + s) % 5 and (r + s + 2) % 5 for s in starts) for r in range(5) ) for row, out in zip(data["levels"], artifact["levels"], strict=True): full = {int(k): v for k, v in row["full_gap_counts"].items()} assert all(k > 0 and k % 6 == 0 and v > 0 for k, v in full.items()) assert sum(full.values()) == row["slots"] assert sum(k * v for k, v in full.items()) == row["period"] assert max(full) == row["A1"] assert all(v % 2 == (k == 6) for k, v in full.items()) counts = {int(k): (v - (int(k) == 6)) // 2 for k, v in row["full_gap_counts"].items()} assert row["x"] == out["x"] and row["A2"] == out["A2_reported"] for name, keep in (("admissible_exceedances", True), ("inadmissible_exceedances", False)): target = out["statistics"][name] assert target["half_length"] == sum(counts.values()) assert target["ordered_value_pairs"] == sum( a + b > row["A2"] and legal(a, b) == keep for a in counts for b in counts ) mean, second, coefficients = graph_moments(counts, row["A2"], keep) assert tuple(target[k] for k in ("C2", "C3", "C4")) == coefficients assert Fraction(target["mean"]) == mean assert Fraction(target["second_moment"]) == second assert Fraction(target["variance"]) == second - mean * mean bound = (second - mean * mean) / second if second else None assert target["zero_probability_upper_bound"] == (str(bound) if bound is not None else None) for field, exact in (("mean", mean), ("variance", second - mean * mean)): assert target[field + "_decimal"] == float(exact) assert target["zero_probability_upper_bound_decimal"] == (float(bound) if bound is not None else None) toys = [(6, 12, 18, 24, 30, 36), (6, 6, 12, 12, 18, 30), (12, 18, 24, 30, 30, 42)] profiles = completions = 0 for word in toys: words = set(itertools.permutations(word)) counts = collections.Counter(word) for cutoff in (18, 30, 42, 60): for keep in (False, True): scores = [ sum(a + b > cutoff and legal(a, b) == keep for a, b in zip(w, w[1:])) for w in words ] mean, second, _ = graph_moments(counts, cutoff, keep) assert mean == Fraction(sum(scores), len(scores)) assert second == Fraction(sum(s * s for s in scores), len(scores)) if second: assert Fraction(scores.count(0), len(scores)) <= (second - mean * mean) / second profiles += 1 completions += len(scores) print(json.dumps({ "pass": True, "source_levels_checked": len(data["levels"]), "new_toy_profiles": profiles, "new_toy_scores_checked": completions, "pair_local_not_global_witness_checked": True, "method": "Occurrence-degree/wedge identities; exhaustive unique-word toys.", }, sort_keys=True)) if __name__ == "__main__": resource.setrlimit(resource.RLIMIT_AS, (1 << 30, 1 << 30)) resource.setrlimit(resource.RLIMIT_CPU, (30, 35)) if hasattr(os, "sched_getaffinity"): os.sched_setaffinity(0, {min(os.sched_getaffinity(0))}) start = resource.getrusage(resource.RUSAGE_SELF) main() stop = resource.getrusage(resource.RUSAGE_SELF) print(json.dumps({ "cpu_seconds": stop.ru_utime + stop.ru_stime - start.ru_utime - start.ru_stime, "maxrss_kib_linux": stop.ru_maxrss, }), file=sys.stderr)