"""Checks ADDED AFTER THE FIRST RUN of refit.py (not pre-registered). They describe the data and the sensitivity of the result; none is used as a test of the paper's claim on its own. P1 Tipping point: the relative bias between the historical triangulations and the 2020s network (modern heights reading low) that would bring the paper's simple difference (R2) below p = 0.05 and below p = 0.005 (two-sided, normal), and the same for the season-level Welch test (R3c). P2 Spread: variance ratio of twilight means (recent / historical), F test, and Brown-Forsythe (Levene on medians) test, on H and on Hadj. P3 Between-epoch spread: Q-profile 95% interval for tau (SD of epoch effects) from the four historical epochs (Hadj), and the z of the historical-minus-recent difference (R4b formula) if tau sits at its upper bound or at 0.25, 0.5 km. P4 Same-night pairs: twilights of different camera groups on the same date (recent data): their height differences. P5 Expected model change between the two samples: mean T(t) of each sample and K needed to explain the observed difference. P6 The seasons added after the authors' earlier paper (2025 and 2026) on their own, against the historical twilights (simple difference, normal z; Welch t); and 2020-2024 on its own. Usage: python3 -I posthoc.py """ import json import math import os import sys from collections import defaultdict sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import refit as rf # noqa: E402 (our own module: numerics only) def f_sf(F, d1, d2): """upper tail of the F distribution""" x = d2 / (d2 + d1 * F) return rf.betai(d2 / 2.0, d1 / 2.0, x) def brown_forsythe(a, b): ma, mb = sorted(a)[len(a) // 2] if len(a) % 2 else rf.quantile(a, 0.5), rf.quantile(b, 0.5) za = [abs(v - rf.quantile(a, 0.5)) for v in a] zb = [abs(v - rf.quantile(b, 0.5)) for v in b] allz = za + zb N, k = len(allz), 2 gm = rf.mean(allz) ssb = len(za) * (rf.mean(za) - gm) ** 2 + len(zb) * (rf.mean(zb) - gm) ** 2 ssw = sum((v - rf.mean(za)) ** 2 for v in za) + sum((v - rf.mean(zb)) ** 2 for v in zb) F = (ssb / (k - 1)) / (ssw / (N - k)) return {"F": F, "df1": k - 1, "df2": N - k, "p": f_sf(F, k - 1, N - k)} def main(): sys.stdout.reconfigure(encoding="utf-8") R = json.load(open(sys.argv[1], encoding="utf-8")) rows = R["rows"] hist = [r for r in rows if r["cls"] == "historical"] rec = [r for r in rows if r["cls"] == "recent_triangulation"] out = {} # P1 d, se = R["R2"]["diff"], R["R2"]["diff_se"] w3 = R["R3c"] out["P1"] = { "twilight_diff": d, "twilight_se": se, "bias_for_p05": d - 1.959964 * se, "bias_for_p005": d - 2.807034 * se, "season_diff": w3["diff"], "season_se": w3["se"], "season_df": w3["df"], } # season-level: t quantiles by bisection def tq(p, df): lo, hi = 0.0, 50.0 for _ in range(200): mid = (lo + hi) / 2 if rf.p_t(mid, df) > p: lo = mid else: hi = mid return (lo + hi) / 2 out["P1"]["season_bias_for_p05"] = w3["diff"] - tq(0.05, w3["df"]) * w3["se"] out["P1"]["season_bias_for_p005"] = w3["diff"] - tq(0.005, w3["df"]) * w3["se"] # P2 def varratio(a, b): va, vb = rf.sd(a) ** 2, rf.sd(b) ** 2 F = vb / va p_one = f_sf(F, len(b) - 1, len(a) - 1) return {"sd_hist": math.sqrt(va), "sd_rec": math.sqrt(vb), "F": F, "p_two_sided": min(1.0, 2 * p_one), "brown_forsythe": brown_forsythe(a, b)} out["P2"] = {"H": varratio([r["H"] for r in hist], [r["H"] for r in rec]), "Hadj": varratio([r["Hadj"] for r in hist], [r["Hadj"] for r in rec])} # P3 ep = [e for e in R["R4_epochs"] if e["cls"] == "historical"] m = [e["Hadj_mean"] for e in ep] s2 = [e["Hadj_se"] ** 2 for e in ep] rec_ep = [e for e in R["R4_epochs"] if e["cls"] == "recent_triangulation"][0] def genQ(t2): w = [1 / (v + t2) for v in s2] mu = sum(a * b for a, b in zip(w, m)) / sum(w) return sum(a * (b - mu) ** 2 for a, b in zip(w, m)) # chi2(3) quantiles: 2.5% = 0.2158, 97.5% = 9.3484 def solve(target): if genQ(0.0) < target: return 0.0 lo, hi = 0.0, 25.0 for _ in range(200): mid = (lo + hi) / 2 if genQ(mid) > target: lo = mid else: hi = mid return (lo + hi) / 2 t2_hi = solve(0.2158) t2_lo = solve(9.3484) out["P3"] = {"Q_at_0": genQ(0.0), "tau_lower": math.sqrt(t2_lo), "tau_upper": math.sqrt(t2_hi)} def z_at(tau): t2 = tau * tau w = [1 / (v + t2) for v in s2] mu = sum(a * b for a, b in zip(w, m)) / sum(w) seh = 1 / math.sqrt(sum(w)) dd = mu - rec_ep["Hadj_mean"] sed = math.sqrt(seh ** 2 + rec_ep["Hadj_se"] ** 2 + t2) return {"tau": tau, "d": dd, "se": sed, "z": dd / sed, "p": rf.p_norm(dd / sed)} out["P3"]["z_by_tau"] = [z_at(t) for t in (0.0, 0.25, 0.5, out["P3"]["tau_upper"])] # tau at which z falls to 1.96 lo, hi = 0.0, 3.0 for _ in range(200): mid = (lo + hi) / 2 if z_at(mid)["z"] > 1.959964: lo = mid else: hi = mid out["P3"]["tau_for_p05"] = (lo + hi) / 2 # P4 by_date = defaultdict(list) for r in rec: by_date[r["date"]].append(r["H"]) pairs = [] for dte, hs in sorted(by_date.items()): if len(hs) > 1: pairs.append({"date": dte, "heights": hs, "range": max(hs) - min(hs)}) out["P4"] = {"same_date_groups": pairs, "median_range": rf.quantile([p["range"] for p in pairs], 0.5) if pairs else None} # P5 Th = rf.mean([rf.T(r["t"]) for r in hist]) Tr = rf.mean([rf.T(r["t"]) for r in rec]) out["P5"] = {"mean_T_hist": Th, "mean_T_rec": Tr, "model_change_km": Tr - Th, "K_from_simple_difference": d / (Tr - Th)} # P6 hH = [r["H"] for r in hist] out["P6"] = {} for name, sel in (("2025-2026", lambda r: r["season"] >= 2025), ("2020-2024", lambda r: r["season"] <= 2024)): sub = [r["H"] for r in rec if sel(r)] dd = rf.mean(hH) - rf.mean(sub) se_ = math.hypot(rf.sd(hH) / math.sqrt(len(hH)), rf.sd(sub) / math.sqrt(len(sub))) out["P6"][name] = {"n": len(sub), "mean": rf.mean(sub), "sd": rf.sd(sub), "diff": dd, "se": se_, "z": dd / se_, "p_normal": rf.p_norm(dd / se_), "welch_p": rf.welch(hH, sub)["p"]} with open(sys.argv[2], "w", encoding="utf-8", newline="\n") as f: json.dump(out, f, indent=1) print(json.dumps(out, indent=1)) if __name__ == "__main__": main()