"""POST HOC checks (not pre-registered), run after analysis.py's first run. Usage: python3 -I posthoc.py PH1 The high-frequency spectral index: which reasonable choices, if any, give the paper's 1.40 +/- 0.06 for the 224/338/485 GHz bands? PH4 The primary epoch-level test (P1) as a function of a common phase offset delta in [-0.15, +0.15], step 0.01 (exact block permutation at each). PH6 How large would the per-epoch scatter have to be before the six-epoch pattern is as likely as not? (Monte Carlo: e_epoch = e0 + A*T1 + noise, with A = 0 under the null; noise SD = observed residual SD; 50,000 draws.) """ import itertools import json import math import random import sys JD_CONJ, P_MAC = 2459984.0, 2109.2 def frac(x): return x - math.floor(x) def T1(p): return math.sin(2 * math.pi * p) ** 2 def pearson(x, y): n = len(x) mx, my = sum(x) / n, sum(y) / n sxy = sum((a - mx) * (b - my) for a, b in zip(x, y)) sxx = sum((a - mx) ** 2 for a in x) syy = sum((b - my) ** 2 for b in y) return sxy / math.sqrt(sxx * syy) def fit(x, y, s=None): w = [1.0] * len(x) if s is None else [1 / v ** 2 for v in s] W = sum(w); Wx = sum(a * b for a, b in zip(w, x)); Wy = sum(a * b for a, b in zip(w, y)) Wxx = sum(a * b * b for a, b in zip(w, x)); Wxy = sum(a * b * c for a, b, c in zip(w, x, y)) D = W * Wxx - Wx ** 2 b = (W * Wxy - Wx * Wy) / D a = (Wxx * Wy - Wx * Wxy) / D if s is None: rss = sum((c - a - b * xx) ** 2 for xx, c in zip(x, y)) se = math.sqrt(rss / (len(x) - 2) * W / D) if len(x) > 2 else float("nan") else: se = math.sqrt(W / D) return round(b, 3), round(se, 3) def main(): d = json.load(open(sys.argv[1], encoding="utf-8")) res = json.load(open(sys.argv[2], encoding="utf-8")) out = {} t5 = d["table5"] hi = [r for r in t5 if r["nu"] > 200] PH1 = {} def sets(): indiv = [r for r in hi if r["date"] != "(Combined)"] comb = [r for r in hi if r["date"] == "(Combined)"] + [r for r in hi if r["nu"] > 400] aug3_4 = [r for r in indiv if r["date"] in ("2023-08-03", "2023-08-04")] return {"individual5": indiv, "combined3": comb, "aug03-04_only": aug3_4, "individual_no_B8": [r for r in indiv if r["nu"] < 400]} for name, rows in sets().items(): x = [math.log10(r["nu"]) for r in rows] y = [math.log10(r["S"]) for r in rows] tot = [r["S_err"] / (r["S"] * math.log(10)) for r in rows] formal = [max(r["S_fit_err"], 1e-6) / (r["S"] * math.log(10)) for r in rows] PH1[name] = {"n": len(rows), "weighted_total_err": fit(x, y, tot), "weighted_formal_err": fit(x, y, formal), "unweighted": fit(x, y)} # pairwise slopes between band means means = {} for r in hi: if r["date"] == "(Combined)" or r["nu"] > 400: means[r["nu"]] = r["S"] ks = sorted(means) PH1["pairwise_slopes_band_means"] = {f"{a}-{b}": round(math.log10(means[b] / means[a]) / math.log10(b / a), 3) for a, b in itertools.combinations(ks, 2)} out["PH1_high_band_alpha"] = PH1 # PH4: P1 versus delta eps = res["A"]["epochs_Dfig"] e = [x["e_mean"] for x in eps] ph = [x["phase"] for x in eps] rows = [] for k in range(-15, 16): dl = k / 100 t = [T1(frac(p + dl)) for p in ph] r0 = pearson(e, t) ge = sum(1 for perm in itertools.permutations(range(6)) if pearson(e, [t[i] for i in perm]) >= r0 - 1e-12) rows.append({"delta": dl, "r": round(r0, 3), "p": round(ge / 720, 4)}) out["PH4_p_vs_delta"] = rows # PH6: null simulations with the observed residual scatter (A = 0) rng = random.Random(20261107) sd = res["A"]["C4_amplitude"]["residual_sd"] r_obs = res["A"]["P1"]["r"] t = [T1(p) for p in ph] N = 50000 ge = 0 for _ in range(N): ee = [0.04 + rng.gauss(0, sd) for _ in range(6)] if pearson(ee, t) >= r_obs: ge += 1 out["PH6_gaussian_null"] = {"noise_sd": sd, "p": (ge + 1) / (N + 1), "draws": N, "note": "Gaussian null with the observed residual SD; for comparison with P1's exact p."} with open(sys.argv[3], "w", encoding="utf-8", newline="\n") as f: json.dump(out, f, indent=1) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()