#!/usr/bin/env python3 """Independent re-computation of the core T1/T2/T4/X1 statistics with numpy/scipy (needs Python with numpy and scipy; run with py -3.12 -I). Compares with out/trend_test.json. Reads the NASA seasonal files as data.""" import json import sys from pathlib import Path import numpy as np from scipy import stats V = Path(sys.argv[1]).resolve() T = json.loads((V / "out" / "trend_test.json").read_text(encoding="utf-8")) def seasonal(sub): f = next((V / "downloads" / sub).glob("*.txt")) rows = {} for line in f.read_text(encoding="utf-8").splitlines(): p = line.split() if len(p) >= 2 and p[0].isdigit() and len(p[0]) == 4 and float(p[1]) != -9999.0: rows[int(p[0])] = float(p[1]) return rows for sub, key in (("nasa_area_0907_1013", "T1_area_0907_1013"), ("nasa_omd_0921_1013", "T2a_mass_deficit_0921_1013"), ("nasa_min_0921_1016", "T2b_min_ozone_0921_1016")): s = seasonal(sub) yrs = np.array([y for y in range(2000, 2026) if y in s]) x = yrs - 2000 y = np.array([s[v] for v in yrs]) lr = stats.linregress(x, y) n = len(x) res = y - (lr.intercept + lr.slope * x) r1 = np.sum(res[1:] * res[:-1]) / np.sum(res ** 2) tq = stats.t.ppf(0.975, n - 2) ci = (lr.slope - tq * lr.stderr, lr.slope + tq * lr.stderr) # r1 < 0 here, so no inflation (rule T1.2) tau = stats.kendalltau(x, y) s_res = np.sqrt(np.sum(res ** 2) / (n - 2)) x0 = 26 se_pred = s_res * np.sqrt(1 + 1 / n + (x0 - x.mean()) ** 2 / np.sum((x - x.mean()) ** 2)) yhat = lr.intercept + lr.slope * x0 pi = (yhat - tq * se_pred, yhat + tq * se_pred) mine = T[key] print(f"{key}: slope/decade scipy {lr.slope * 10:.4f} mine {mine['slope_per_decade']:.4f}; " f"CI scipy [{ci[0] * 10:.3f},{ci[1] * 10:.3f}] mine {[round(v, 3) for v in mine['ci95_per_decade']]}; " f"r1 {r1:.3f} (mine {mine['r1_residuals']:.3f}); kendall tau {tau.statistic:.4f} p {tau.pvalue:.4f} " f"(mine {mine['kendall_tau_b']:.4f}, p {mine['kendall_p']:.4f}); PI scipy [{pi[0]:.3f},{pi[1]:.3f}] " f"mine {[round(v, 3) for v in mine['pi95_2026']]}")