#!/usr/bin/env python3 """Tests 3a, 4 and 5: the storm-for-storm test with documented records, the naked-eye test, and controls on the rate test. Rules: RULES_baseline_1986_2019.txt, RULES_naked_eye.txt, RULES_rate_controls.txt. Fact-Checked Science, verification of florida-aurora-cycle-25. Standard library only; reads the downloads as data. python3 -I baseline_tests.py Reads /our_storms_24h.csv (test 2) and code/records_storms.csv (our classification of each storm's Florida record). Writes /baseline_tests.txt. """ import csv import datetime as dt import os import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) # our own code folder; -I leaves it out import fclib as F # noqa: E402 # Florida records outside the note's inventory, before cycle 25 (cycle, date, kind), from tests 3a and 3b FLORIDA_ADDITIONS = [ (10, "1859-09-02", "E"), # the second of 1859's two storms at Key West, which the note counts with 28 August as one event (23, "2000-04-06/07", "P"), # photograph, Rosemary Hill Observatory (23, "2001-11-06", "S"), # Spaceweather.com's sentences (23, "2000-07-15", "I"), # a 2022 retelling (23, "2004-11-07/08", "I"), # "every US state but Hawaii" ] # Florida records for events the note has only in Path B (so they add to Path A's count, not to Path B's) FLORIDA_ADDITIONS_A_ONLY = [ (19, "1958-02-10/11", "S"), # UP, New York, 11 Feb 1958, in Diario Las Americas (Miami), 12 Feb: observed as far south as Florida ] # Gulf and Caribbean "equivalents" outside the note's Path B, found in passing (not a systematic search): cycle, date, what EQUIVALENTS_FOUND = [ (19, "1959-11-27", "Houston, Texas (AP, 28 Nov 1959)"), (20, "1969-03-23", "Lake Charles, Louisiana (AP, 24 Mar 1969)"), (23, "2001-03-30/31", "Mexico and Texas (Spaceweather.com, 1 Apr 2001)"), ] CYCLES = [(17, dt.datetime(1933, 9, 1)), (18, dt.datetime(1944, 2, 1)), (19, dt.datetime(1954, 4, 1)), (20, dt.datetime(1964, 10, 1)), (21, dt.datetime(1976, 3, 1)), (22, dt.datetime(1986, 9, 1)), (23, dt.datetime(1996, 8, 1)), (24, dt.datetime(2008, 12, 1)), (25, dt.datetime(2019, 12, 1))] T25 = (F.FREEZE - F.C25_START).total_seconds() / 86400 / 365.25 SHARE_NOTE = 6.9 / 10.95 def main(): dl, outdir = sys.argv[1], sys.argv[2] lines = [] out = lines.append kp, _ = F.read_kp(os.path.join(dl, "gfz-kp", "Kp_ap_Ap_SN_F107_since_1932.txt")) with open(os.path.join(outdir, "our_storms_24h.csv"), encoding="utf-8", newline="") as f: storms = [r for r in csv.DictReader(x for x in f if not x.startswith("#"))] with open(os.path.join(HERE, "records_storms.csv"), encoding="utf-8", newline="") as f: recs = [r for r in csv.DictReader(x for x in f if not x.startswith("#"))] for r in recs: r["t"] = dt.datetime.fromisoformat(r["start"]) def record(storm): t0, t1 = dt.datetime.fromisoformat(storm["start_UT"]), dt.datetime.fromisoformat(storm["end_UT"]) best = None for r in recs: if r["era"] != storm["era"]: continue gap = max((r["t"] - t1).total_seconds(), (t0 - (r["t"] + dt.timedelta(days=1))).total_seconds(), 0) / 3600 if gap <= 36 and (best is None or gap < best[0]): best = (gap, r) return best[1] if best else None def fisher_line(label, h1, n1, h0, n0): p = F.fisher_one_sided(h1, n1, h0, n0) pmin = F.fisher_min_p(n1, n0, h1 + h0) return f" {label:62s} {h0:2d}/{n0:<2d} vs {h1}/{n1}: p = {F.sig(p):>9s} (smallest p these margins allow: {F.sig(pmin)})" out("TESTS 3a, 4, 5 (rules: RULES_baseline_1986_2019.txt, RULES_naked_eye.txt, RULES_rate_controls.txt)") out("") out("A. Our record for each Kp >= 9- storm (code/records_storms.csv), with the darkness control") k9 = [s for s in storms if s["stratum"] == "Kp>=9-"] dark = {} for s in k9: t0, t1 = dt.datetime.fromisoformat(s["start_UT"]), dt.datetime.fromisoformat(s["end_UT"]) bins = [t for t, v in kp.items() if t0 <= t <= t1 and v >= 8.667 - 1e-3] dbins = [t for t in bins if F.dark_at_orlando(t)] dark[s["start_UT"]] = bool(dbins) r = record(s) out(f" {s['start_UT'][:16]} {s['era']:13s} note hit {s['hit_path_a']} kind {r['kind'] if r else '?'} naked eye {r['naked_eye'] if r else '?'} " f"Kp>=9- intervals {len(bins)}, dark in Florida {len(dbins)} {'' if dbins else '<-- none after dark'}") out("") def tally(stratum, era, test, only_dark=False): n = h = 0 for s in storms: if s["stratum"] != stratum or s["era"] != era: continue if only_dark and not dark.get(s["start_UT"], False): continue n += 1 r = record(s) h += bool(test(s, r)) return h, n def kinds(*ks): return lambda s, r: r is not None and r["kind"] in ks note_hit = lambda s, r: s["hit_path_a"] == "1" # noqa: E731 t1a = lambda s, r: s["hit_path_a"] == "1" or (r is not None and r["kind"] in ("P", "E")) # noqa: E731 eye_low = lambda s, r: r is not None and r["naked_eye"] == "Y" # noqa: E731 eye_high = lambda s, r: r is not None and r["naked_eye"] in ("Y", "U") # noqa: E731 r1_strict_base = lambda s, r: s["hit_path_a"] == "1" and not s["start_UT"].startswith("2003-11-20") # noqa: E731 out("B. Storm-for-storm test, one-sided Fisher exact (1986-November 2019 against cycle 25)") for stratum in ("Kp>=9-", "Kp>=8-", "Dst<=-200"): out(f" {stratum}") base = {"T0 the note's table": note_hit, "T1 note's hits + additions with a photograph or named report": t1a, "T1 strict: only storms with a photograph or named report": kinds("P", "E"), "T2 photograph, named report or a sentence at the time": kinds("P", "E", "S"), "T3 as T2, plus inference and later retelling": kinds("P", "E", "S", "I")} for label, test in base.items(): h0, n0 = tally(stratum, "1986-2019Nov", test) h1, n1 = tally(stratum, "C25", note_hit if not label.startswith("T1 strict") else kinds("P", "E")) out(fisher_line(label + "; cycle 25: any record", h1, n1, h0, n0)) out(" -- cycle 25 held to naked-eye records (test 4) --") for blabel, btest in (("T0 baseline", note_hit), ("R1-strict baseline (2003-11-20 dropped)", r1_strict_base), ("T2 baseline", kinds("P", "E", "S")), ("T3 baseline", kinds("P", "E", "S", "I"))): h0, n0 = tally(stratum, "1986-2019Nov", btest) for elabel, etest in (("naked eye stated (2024-05, 2025-11)", eye_low), ("with 2024-10, unclear", eye_high)): h1, n1 = tally(stratum, "C25", etest) out(fisher_line(f"{blabel}; cycle 25: {elabel}", h1, n1, h0, n0)) out(" T4. Darkness control, Kp >= 9- only: storms with at least one Kp >= 9- interval after dark in Florida") for label, test in (("T0", note_hit), ("T2", kinds("P", "E", "S")), ("T3", kinds("P", "E", "S", "I"))): h0, n0 = tally("Kp>=9-", "1986-2019Nov", test, only_dark=True) h1, n1 = tally("Kp>=9-", "C25", note_hit, only_dark=True) out(fisher_line(f"{label}, dark storms only; cycle 25: any record", h1, n1, h0, n0)) for elabel, etest in (("naked eye stated", eye_low), ("with 2024-10", eye_high)): h1, n1 = tally("Kp>=9-", "C25", etest, only_dark=True) out(fisher_line(f"{label}, dark storms only; cycle 25: {elabel}", h1, n1, h0, n0)) n_add = {k: sum(1 for r in recs if r["era"] == "1986-2019Nov" and r["note_hit"] == "0" and r["kind"] in ks) for k, ks in (("P or E", ("P", "E")), ("P, E or S", ("P", "E", "S")), ("any, with inference", ("P", "E", "S", "I")))} out(f" The note's falsifier asks for 5 or more of the 16 misses. Found: {n_add}") out("") # ------------------------------------------------------------ rates A = F.read_events(os.path.join(dl, "s0-note-004-drive", "PathA_events.csv")) B = F.read_events(os.path.join(dl, "s0-note-004-drive", "PathB_events.csv")) preA = [e for e in A if int(e["cycle"]) < 25] preB = [e for e in B if int(e["cycle"]) < 25] add_pes = [a for a in FLORIDA_ADDITIONS if a[2] in ("P", "E", "S")] add_all = FLORIDA_ADDITIONS T0_all = 2019.958 - 1855.958 # SILSO minima, cycle 10 to cycle 25 T0_mod = 2019.958 - 1986.708 # cycle 22 to cycle 25 def modern(n_list): return sum(1 for c in n_list if 22 <= c <= 24) base_sets = [ ("the note's inventory", [int(e["cycle"]) for e in preA], [int(e["cycle"]) for e in preB]), ("R1 strict (2003-11-20 'photographed' dropped)", [int(e["cycle"]) for e in preA if e["date"] != "2003-11-20"], [int(e["cycle"]) for e in preB if e["date"] != "2003-11-20"]), ("with documented Florida additions (P, E, S)", [int(e["cycle"]) for e in preA] + [a[0] for a in add_pes] + [a[0] for a in FLORIDA_ADDITIONS_A_ONLY], [int(e["cycle"]) for e in preB] + [a[0] for a in add_pes]), ("with inferred additions too", [int(e["cycle"]) for e in preA] + [a[0] for a in add_all] + [a[0] for a in FLORIDA_ADDITIONS_A_ONLY], [int(e["cycle"]) for e in preB] + [a[0] for a in add_all]), ("Path B with the Gulf equivalents found in passing (and P, E, S)", None, [int(e["cycle"]) for e in preB] + [a[0] for a in add_pes] + [a[0] for a in EQUIVALENTS_FOUND]), ] # C3 first: the share of a cycle's Kp >= 9- storms in its first T25 years out("C. Controls on the rate test") out(f" C3. Share of each cycle's Kp >= 9- storms (the note's 24 h rule) that began in its first {T25:.3f} years") tot = early = 0 for i, (n, start) in enumerate(CYCLES[:-1]): stop = CYCLES[i + 1][1] S = F.build_storms([t for t, v in kp.items() if start <= t < stop and v >= 8.667 - 1e-3], 3, 24, "A") e = sum(1 for s in S if (s[0] - start).total_seconds() / 86400 / 365.25 <= T25) tot += len(S) early += e out(f" cycle {n}: {e} of {len(S)}" + (f" = {e / len(S):.0%}" if S else "")) share_pooled = early / tot out(f" pooled, cycles 17-24: {early} of {tot} = {share_pooled:.1%} (the note's scaling assumes {SHARE_NOTE:.1%})") out("") out(" The rate test under each baseline and each count for cycle 25 (exact one-sided Poisson; C1 = the two-rate binomial test)") out(" k = 5 all five records; k = 3 naked eye with October 2024; k = 2 naked eye stated (May 2024, November 2025)") for name, a, b in base_sets: out(f" Baseline: {name}") for path, lst in (("Path A", a), ("Path B", b)): if lst is None: continue n0 = len(lst) lam = n0 / 15.0 m0 = modern(lst) out(f" {path}: {n0} events in 15 cycles (lambda {lam:.3f}); cycles 22-24: {m0} (lambda {m0 / 3:.3f})") for k in (5, 3, 2): q_all, q_mod = T25 / (T25 + T0_all), T25 / (T25 + T0_mod) out(f" k = {k}: full cycle p = {F.sig(F.poisson_sf(k, lam)):>9s}; scaled by {SHARE_NOTE:.3f}: {F.sig(F.poisson_sf(k, lam * SHARE_NOTE)):>9s}; " f"scaled by {share_pooled:.3f}: {F.sig(F.poisson_sf(k, lam * share_pooled)):>9s}; modern only: {F.sig(F.poisson_sf(k, m0 / 3)) if m0 else 'n/a':>9s}; " f"C1 all cycles: {F.sig(F.binom_sf(k, k + n0, q_all)):>9s}; C1 modern: {F.sig(F.binom_sf(k, k + m0, q_mod)) if m0 else 'n/a':>9s}") out("") out(" C4. Storm supply and the note's events by cycle") evA = {} for e in A: evA[int(e["cycle"])] = evA.get(int(e["cycle"]), 0) + 1 evB = {} for e in B: evB[int(e["cycle"])] = evB.get(int(e["cycle"]), 0) + 1 for i, (n, start) in enumerate(CYCLES): stop = CYCLES[i + 1][1] if i + 1 < len(CYCLES) else F.FREEZE S = F.build_storms([t for t, v in kp.items() if start <= t < stop and v >= 8.667 - 1e-3], 3, 24, "A") S9 = F.build_storms([t for t, v in kp.items() if start <= t < stop and v >= 9.0 - 1e-3], 3, 24, "A") a, b = evA.get(n, 0), evB.get(n, 0) out(f" cycle {n}: Kp >= 9- storms {len(S):2d}, Kp 9o storms {len(S9)}; the note's events: Path A {a}, Path B {b}; Path A events per Kp >= 9- storm: " + (f"{a / len(S):.2f}" if S else "no storms")) out("") out(" C5. How many more baseline events before the note's own full-cycle Poisson p passes 0.05 and 0.01") for path, n0 in (("Path A", len(preA)), ("Path B", len(preB)), ("modern, cycles 22-24", 3)): ncyc = 3 if path.startswith("modern") else 15 for k in (5, 3, 2): need = {} for level in (0.01, 0.05): extra = 0 while F.poisson_sf(k, (n0 + extra) / ncyc) <= level: extra += 1 need[level] = extra out(f" {path}, k = {k}: p > 0.01 with {need[0.01]} more events ({n0 + need[0.01]} in all); p > 0.05 with {need[0.05]} more ({n0 + need[0.05]} in all)") out(" the note's falsifier: about 21 more Path B events, a mean of 2 a cycle: P(X >= 5 | 2.0) = " + F.sig(F.poisson_sf(5, 2.0))) with open(os.path.join(outdir, "baseline_tests.txt"), "w", encoding="utf-8", newline="\n") as f: f.write("\n".join(lines) + "\n") sys.stdout.reconfigure(encoding="utf-8", newline="\n") print("\n".join(lines)) if __name__ == "__main__": main()