# Hosted copy (Fact-Checked Science, /amundsen-low-solar-cycle/): reads its inputs from the folder given as the # first argument, filled by fetch_inputs.py, and writes its outputs to /out/. Run it with Python 3.10+, # numpy and scipy, from the folder that holds this script, asl_lib.py, surr_lib.py and the RULES files. """05c_posthoc_review.py — checks prompted by the fresh review (RULES_posthoc_review.txt; post hoc, descriptive). Fact-Checked Science, 2026-11-15. Needs numpy and scipy. Usage: py -3.12 -I 05c_posthoc_review.py (reads ../downloads and ../out/chart_series.csv; writes ../out/05c_posthoc_review.txt)""" import os, sys, json, hashlib, csv here = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, here) import numpy as np import asl_lib as A root = os.path.abspath(sys.argv[1]) if len(sys.argv) > 1 else os.getcwd() dl = root outdir = os.path.join(root, "out") os.makedirs(outdir, exist_ok=True) lines = [] def say(*a): s = " ".join(str(x) for x in a) print(s) lines.append(s) P_V3 = os.path.join(dl, "asli_calculation_latest.csv") P_SN = os.path.join(dl, "SN_m_tot_V2.0.csv") P_AAO = os.path.join(dl, "monthly.aao.index.b79.current.ascii") P_SAM = os.path.join(dl, "newsam.1957.2007.txt") for p in [os.path.join(here, "RULES_posthoc_review.txt"), P_V3, P_SN, P_AAO, P_SAM]: say("input", os.path.basename(p), hashlib.sha256(open(p, "rb").read()).hexdigest()) v3, _ = A.load_v3(P_V3) sn, _ = A.load_silso(P_SN) W0, END = A.ym("1979-01"), A.ym("2026-07") asl = v3.restrict(W0, END) ssn = sn.restrict(W0, A.ym("2026-10")) def load_aao(path): d = {} for line in open(path, encoding="utf-8"): p = line.split() if len(p) == 3 and p[0].isdigit(): d[int(p[0]) * 12 + int(p[1]) - 1] = float(p[2]) return A._from_dict(d) def load_marshall(path): d = {} for line in open(path, encoding="utf-8"): p = line.split() if p and len(p[0]) == 4 and p[0].isdigit(): for k, v in enumerate(p[1:13]): d[int(p[0]) * 12 + k] = float(v) return A._from_dict(d) ac = A.cma(asl, 60) sc = A.cma(ssn, 60) x, y, t = A.pair(sc, ac, 33) # SSN at t, ASL at t+33 asl_months = t + 33 for name, ix in (("AAO (CPC)", load_aao(P_AAO)), ("SAM (Marshall)", load_marshall(P_SAM))): icm = A.cma(ix.restrict(W0, END), 60) # P1 allm = [m for m in range(ac.start, ac.end + 1) if np.isfinite(ac.get(m)) and np.isfinite(icm.get(m))] r_all = A.pearson(np.array([ac.get(m) for m in allm]), np.array([icm.get(m) for m in allm])) r_asl = A.pearson(np.array([ac.get(m) for m in asl_months]), np.array([icm.get(m) for m in asl_months])) r_ssn = A.pearson(np.array([ac.get(m) for m in t]), np.array([icm.get(m) for m in t])) say(f"\n[{name}] P1 ASL vs index, 60-month CMAs: all {len(allm)} months r = {r_all:.4f}; pressure-side months " f"{A.ym_str(int(asl_months[0]))}..{A.ym_str(int(asl_months[-1]))} ({len(asl_months)}) r = {r_asl:.4f}; sunspot-side months r = {r_ssn:.4f}") # P2 z = np.array([icm.get(m) for m in asl_months]) b, a = np.polyfit(z, y, 1) res = y - (a + b * z) r_res = A.pearson(x, res) ne = A.neff_bretherton(x, res) r_xy, r_xz, r_yz = A.pearson(x, y), A.pearson(x, z), A.pearson(y, z) partial = (r_xy - r_xz * r_yz) / np.sqrt((1 - r_xz ** 2) * (1 - r_yz ** 2)) say(f" P2 residual of ASL on index vs SSN: r = {r_res:.4f}, n = {len(x)}, n_eff = {ne:.2f}, p_eff = {A.p_two_sided(r_res, ne):.4f}; " f"partial r(ASL, SSN | index) = {partial:.4f}; index explains {100 * r_yz ** 2:.1f}% of the ASL five-year variance (r = {r_yz:.4f}); r(SSN, index) = {r_xz:.4f}") # P3 rows = {r["month"]: r for r in csv.DictReader(open(os.path.join(outdir, "chart_series.csv"), encoding="utf-8"))} slope = json.load(open(os.path.join(outdir, "03_oos.json"), encoding="utf-8"))["T1"]["fit"]["b"] say("\n[P3] five-year means (ASL hPa; SSN drawn 33 months later); recipe slope %.5f hPa per sunspot" % slope) for a_, b_ in (("2016-07", "2021-07"), ("2021-12", "2024-02")): ra, rb = rows[a_], rows[b_] da = float(rb["asl_5y_mean_hPa"]) - float(ra["asl_5y_mean_hPa"]) ds = float(rb["ssn_5y_mean_33_months_earlier"]) - float(ra["ssn_5y_mean_33_months_earlier"]) say(f" {a_} to {b_}: ASL {float(ra['asl_5y_mean_hPa']):.3f} -> {float(rb['asl_5y_mean_hPa']):.3f} ({da:+.2f} hPa); " f"SSN {float(ra['ssn_5y_mean_33_months_earlier']):.2f} -> {float(rb['ssn_5y_mean_33_months_earlier']):.2f} ({ds:+.1f}); recipe implies {slope * ds:+.2f} hPa") open(os.path.join(outdir, "05c_posthoc_review.txt"), "w", encoding="utf-8", newline="\n").write("\n".join(lines) + "\n")