"""Post hoc (not pre-registered): which reading of the thesis's crossing rule reproduces its counts? The pre-registered readings (RULES_hcs_list.txt, rules R-A and R-B) bracket the thesis's counts but match neither. This script tries a grid of readings of the same three steps and ranks them by distance to the thesis's numbers. Labelled post hoc in the report. usage: py -3.12 -I hcs_variants.py Needs numpy. """ import datetime as dt import itertools import json import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import numpy as np # noqa: E402 import common as C # noqa: E402 D = dt.date dl, outdir = sys.argv[1], sys.argv[2] dates, gse, gsm = C.read_omni_daily(os.path.join(dl, 'omni', 'omni_01_av.dat'), 1999, 2024) P1 = (D(2000, 8, 31), D(2007, 4, 30)) P1yrs = (D(2000, 1, 1), D(2007, 12, 31)) P2 = (D(2010, 1, 1), D(2023, 12, 31)) def rm(b, kind): n = len(b) m = np.full(n, np.nan) for t in range(n): if kind == 'centred': lo, hi = t - 2, t + 3 elif kind == 'trailing': lo, hi = t - 4, t + 1 else: # leading lo, hi = t, t + 5 if lo < 0 or hi > n: continue seg = b[lo:hi] seg = seg[~np.isnan(seg)] if len(seg) >= 3: m[t] = seg.mean() return m def stat(b, lo, hi, kind): if lo < 0 or hi >= len(b): return np.nan seg = b[lo:hi + 1] seg = seg[~np.isnan(seg)] if len(seg) < 3: return np.nan return np.abs(seg).mean() if kind == 'meanabs' else abs(seg.mean()) def detect(dd, b, thr, rmkind, tol, statkind, win): m = rm(b, rmkind) (b0, b1), (a0, a1) = C.WINDOWS[win] out = [] n = len(b) for t in range(1, n): p, q = b[t - 1], b[t] if np.isnan(p) or np.isnan(q) or p == 0 or q == 0 or np.sign(p) == np.sign(q): continue want = np.sign(q) ok = False for s in range(max(1, t - tol), min(n, t + tol + 1)): u, v = m[s - 1], m[s] if not (np.isnan(u) or np.isnan(v)) and np.sign(u) == -want and np.sign(v) == want: ok = True break if not ok: continue bf = stat(b, t + b0, t + b1, statkind) af = stat(b, t + a0, t + a1, statkind) if np.isnan(bf) or np.isnan(af) or bf <= thr or af <= thr: continue out.append((t, 'T-A' if p < 0 else 'A-T', bf, af)) return out def thr_of(period): _, b = C.subset(dates, gse, *period) return 0.5 * np.nanmean(np.abs(b)) thrP2, thrP1 = thr_of(P2), thr_of(P1yrs) series = {k: C.subset(dates, gse, *p) for k, p in (('P1', P1), ('P1yrs', P1yrs), ('P2', P2))} target = {'P2_n': 284, 'P2_TA': 151, 'P2_AT': 133, 'P1_n': 175, 'P1_TA': 90, 'P1_AT': 85, 'P1el_n': 139, 'P1el_TA': 74, 'P1el_AT': 65, 'P2_gap_mean': 17.7, 'P2_gap_median': 15, 'P1yrs_gap_mean': 15.4, 'P1yrs_gap_median': 14} rows = [] for rmkind, tol, statkind, win in itertools.product(('centred', 'trailing', 'leading'), (0, 1, 2, 3), ('meanabs', 'absmean'), ('W1', 'W2')): r = {'rm': rmkind, 'tol': tol, 'stat': statkind, 'win': win} for key, thr in (('P2', thrP2), ('P1', thrP1), ('P1yrs', thrP1)): dd, b = series[key] cr = detect(dd, b, thr, rmkind, tol, statkind, win) r[f'{key}_n'] = len(cr) r[f'{key}_TA'] = sum(c[1] == 'T-A' for c in cr) r[f'{key}_AT'] = sum(c[1] == 'A-T' for c in cr) g = np.diff([c[0] for c in cr]) r[f'{key}_gap_mean'] = round(float(g.mean()), 2) if len(g) else None r[f'{key}_gap_median'] = float(np.median(g)) if len(g) else None r[f'{key}_gap_min'] = int(g.min()) if len(g) else None if key == 'P1': el = [c for c in cr if 40 <= c[0] <= len(dd) - 41] r['P1el_n'] = len(el) r['P1el_TA'] = sum(c[1] == 'T-A' for c in el) r['P1el_AT'] = sum(c[1] == 'A-T' for c in el) score = 0.0 for k, v in target.items(): if r.get(k) is not None: score += ((r[k] - v) / v) ** 2 r['score'] = round(score, 4) rows.append(r) rows.sort(key=lambda r: r['score']) with open(os.path.join(outdir, 'hcs_variants.json'), 'w', encoding='utf-8', newline='\n') as f: json.dump({'target_thesis': target, 'variants_ranked': rows}, f, indent=1) for r in rows[:12]: print(r)