#!/usr/bin/env python3 """Analysis 3 of PROTOCOL.md: how often SSGEOS's "convergence" happens anyway, and whether large earthquakes favour it, in SSGEOS's own years and in new ones. python3 -I convergence_test.py Standard library only. A day D is a "convergence" day if two or more sets of three bodies come within 2° of a line (our rebuilt rule, conjunctions.py) on some day from D-2 to D+2 ("within 2 days"); the stricter version uses D-1 to D+1. SSGEOS's other statistic, "one or more planetary conjunctions ≤ 1°", is computed the same way. In-sample: SSGEOS's own 182 earthquakes (times from its listing) against all days from 1 January 2011 to 28 February 2023. Out of sample: ComCat earthquakes of magnitude 7.0 or more (rounded half up) from 1 March 2023 to 31 October 2026 against all days in that period. The p-value is the exact one-sided binomial chance of at least as many earthquakes on such days if they fell on days at random. """ import csv import datetime as dt import glob import json import math import os import sys from decimal import Decimal, ROUND_HALF_UP CRITERIA = { # name: (minimum number of sets of three, deviation limit in degrees, days either side) "convergence_2d": (2, 2.0, 2), # SSGEOS: 135 of 182 (74.2%) "convergence_1d": (2, 2.0, 1), # SSGEOS: 134 of 182 (73.6%) "one_within_1deg_2d": (1, 1.0, 2), # SSGEOS: 153 of 182 (84.1%) "one_within_1deg_1d": (1, 1.0, 1), # SSGEOS: 148 of 182 (81.3%) } SSGEOS = {"convergence_2d": (135, 182), "convergence_1d": (134, 182), "one_within_1deg_2d": (153, 182), "one_within_1deg_1d": (148, 182)} def day_flags(days, d, n_min, limit, span): keys = [(d + dt.timedelta(days=k)).isoformat() for k in range(-span, span + 1)] n = len(days[keys[0]]) count = sum(1 for i in range(n) if min(days[k][i] for k in keys) < limit) return count >= n_min def binom_tail(k, n, p): """P(X >= k) for X ~ Binomial(n, p), exactly (log space).""" if k <= 0: return 1.0 total = 0.0 for j in range(k, n + 1): total += math.exp(math.lgamma(n + 1) - math.lgamma(j + 1) - math.lgamma(n - j + 1) + j * math.log(p) + (n - j) * math.log1p(-p)) return min(1.0, total) def comcat_m7(folder, start, end): seen, out = set(), [] for path in sorted(glob.glob(os.path.join(folder, "*.csv"))): with open(path, encoding="utf-8", newline="") as fh: for row in csv.DictReader(fh): if row.get("type") != "earthquake" or not row.get("mag") or row["id"] in seen: continue seen.add(row["id"]) m = float(Decimal(row["mag"]).quantize(Decimal("0.1"), rounding=ROUND_HALF_UP)) t = dt.datetime.fromisoformat(row["time"].replace("Z", "+00:00")) if m >= 7.0 and start <= t.date() <= end: out.append({"time": row["time"], "mag": m, "place": row.get("place", ""), "id": row["id"]}) out.sort(key=lambda e: e["time"]) return out def block(days, quake_days, first, last, name): n_min, limit, span = CRITERIA[name] all_days = [first + dt.timedelta(days=k) for k in range((last - first).days + 1)] share_days = sum(day_flags(days, d, n_min, limit, span) for d in all_days) / len(all_days) hits = sum(day_flags(days, d, n_min, limit, span) for d in quake_days) n = len(quake_days) return {"days": len(all_days), "share_of_days": round(share_days, 4), "earthquakes": n, "on_such_days": hits, "share_of_earthquakes": round(hits / n, 4) if n else None, "expected_by_chance": round(share_days * n, 1), "p_one_sided": round(binom_tail(hits, n, share_days), 4) if n else None} def main(): days = json.load(open(sys.argv[1], encoding="utf-8"))["days"] val = json.load(open(sys.argv[2], encoding="utf-8")) their = [dt.date.fromisoformat(r["date"]) for r in val["rows"]] m7 = comcat_m7(sys.argv[3], dt.date(2023, 3, 1), dt.date(2026, 10, 31)) new = [dt.date.fromisoformat(e["time"][:10]) for e in m7] res = {"in_sample": {}, "out_of_sample": {}, "ssgeos_reported": SSGEOS, "out_of_sample_events": m7} for name in CRITERIA: res["in_sample"][name] = block(days, their, dt.date(2011, 1, 1), dt.date(2023, 2, 28), name) res["out_of_sample"][name] = block(days, new, dt.date(2023, 3, 1), dt.date(2026, 10, 31), name) with open(sys.argv[4], "w", encoding="utf-8", newline="\n") as fh: json.dump(res, fh, ensure_ascii=False, indent=1) sys.stdout.reconfigure(encoding="utf-8") for part in ("in_sample", "out_of_sample"): for name, r in res[part].items(): print(part, name, r) if __name__ == "__main__": main()