"""EXPLORATORY (added after the first run): which interval formula gives the note's printed 95% intervals? Not pre-registered; it changes no verdict. Usage: py -3.12 -I interval_match.py [asli_calculation_latest.csv] Compares the note's three printed intervals with M1-M5 of ppt_lib and M6: as M1, but with r1 = Pearson correlation of (e[:-1], e[1:]). """ import math import os import sys import numpy as np from scipy import stats sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import ppt_lib as L # noqa: E402 def m6(x, y): n, sxx, b, a, e, sse, _ = L._fit(x, y) r1 = float(np.corrcoef(e[:-1], e[1:])[0, 1]) r = max(r1, 0.0) neff = n * (1 - r) / (1 + r) se = math.sqrt(sse / (neff - 2) / sxx) c = stats.t.ppf(0.975, neff - 2) return r1, (b - c * se) * 10, (b + c * se) * 10 def load_csv(path, col): rows = [l.strip().split(",") for l in open(path, encoding="utf-8")] h = rows[0] j = h.index(col) return (np.array([int(r[0]) for r in rows[1:]]), np.array([float(r[j]) for r in rows[1:]])) out = sys.argv[1] cases = [ ("R1 (Gaussian) global 1979-2025", "annual_bands_r1g.csv", "global", 1979, 2025, (-0.0576, -0.0014)), ("R1 (Gaussian) dry air 1979-2025", "annual_global_r1g_budget.csv", "dry_hPa", 1979, 2025, (-0.0759, -0.0313)), ("20CRv3 global 1979-2015", "annual_bands_c20v3.csv", "global", 1979, 2015, (0.0271, 0.0444)), ("JRA-55 global 1979-2023", "annual_bands_jra55.csv", "global", 1979, 2023, (0.0503, 0.0972)), ] def asl_sector(path): import csv rows = [r for r in csv.reader(open(path, encoding="utf-8")) if r and r[0][:1].isdigit()] yy = np.array([int(r[0][:4]) for r in rows]) mm = np.array([int(r[0][5:7]) for r in rows]) vv = np.array([float(r[4]) for r in rows]) return L.annual_means(yy, mm, vv) if len(sys.argv) > 2: cases.append(("BAS ASL sector pressure 1959-2025", "ASL", "sector", 1959, 2025, (-0.856, -0.372))) for name, f, col, s, e, note in cases: if f == "ASL": y, v = asl_sector(sys.argv[2]) else: y, v = load_csv(os.path.join(out, f), col) sel = (y >= s) & (y <= e) print(name, "note", note) for m in ("M1", "M2", "M3", "M4", "M5"): t = L.trend(y[sel], v[sel], m) print(f" {m}: [{t['lo']:.4f}, {t['hi']:.4f}] r1={t['r1']:.3f} neff={t['neff']:.1f}") r1, lo, hi = m6(y[sel], v[sel]) print(f" M6: [{lo:.4f}, {hi:.4f}] r1(Pearson)={r1:.3f}")