"""Exact masked-edge moments under a uniform fixed-multiset half-word.""" import json import os import resource import sys from fractions import Fraction def admissible(u, v): return len({h % 5 for h in (0, 2, u, u + 2, u + v, u + v + 2)}) < 5 def weight(word, counts): used = {} result = 1 for value in word: result *= counts[value] - used.get(value, 0) used[value] = used.get(value, 0) + 1 return result def moments(counts, predicate): n = sum(counts.values()) assert n >= 4 edges = [(a, b) for a in counts for b in counts if predicate(a, b)] c2 = sum(weight(edge, counts) for edge in edges) c3 = sum( weight((a, b, c), counts) for a, b in edges for c in counts if predicate(b, c) ) c4 = sum( weight((a, b, c, d), counts) for a, b in edges for c, d in edges ) p2 = Fraction(c2, n * (n - 1)) p3 = Fraction(c3, n * (n - 1) * (n - 2)) p4 = Fraction(c4, n * (n - 1) * (n - 2) * (n - 3)) mean = (n - 1) * p2 second = mean + 2 * (n - 2) * p3 + (n - 2) * (n - 3) * p4 variance = second - mean * mean bound = variance / second if second else None return { "half_length": n, "ordered_value_pairs": len(edges), "C2": c2, "C3": c3, "C4": c4, "mean": str(mean), "second_moment": str(second), "variance": str(variance), "zero_probability_upper_bound": str(bound) if bound is not None else None, "mean_decimal": float(mean), "variance_decimal": float(variance), "zero_probability_upper_bound_decimal": float(bound) if bound is not None else None, } def produce(data): rows = [] for row in data["levels"]: counts = {int(k): v for k, v in row["full_gap_counts"].items()} assert sum(counts.values()) == row["slots"] assert sum(k * v for k, v in counts.items()) == row["period"] assert max(counts) == row["A1"] assert all(v % 2 == (k == 6) for k, v in counts.items()) half = {k: (v - (k == 6)) // 2 for k, v in counts.items()} cutoff = row["A2"] records = {} for name, predicate in ( ("admissible_exceedances", lambda a, b: a + b > cutoff and admissible(a, b)), ("inadmissible_exceedances", lambda a, b: a + b > cutoff and not admissible(a, b)), ): records[name] = moments(half, predicate) rows.append({"x": row["x"], "A2_reported": cutoff, "statistics": records}) return { "scope": "New exact interior-half edge moments; no census or null draws.", "null": "Uniform permutations of the reported half multiset; NOT conditioned on admissibility.", "forbidden_residue_pairs_mod5": [ [a, b] for a in range(5) for b in range(5) if not admissible(a, b) ], "local_not_global_witness_half": [12, 12, 12, 6, 30], "levels": rows, } 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) with open(sys.argv[1]) as source: result = produce(json.load(source)) print(json.dumps(result, sort_keys=True, indent=2)) 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)