#!/usr/bin/env python """Addition to the rules, in the paper's favour: each of the nine as a single test at its exact period. No window, no choice of set paid for, no family: z at exactly the theoretical period against the null distribution of z at that same period, for each set and each null. This is the fairest reading of the group's claim for P629, whose value they treat as fixed in advance. Also GLE alone and S-list alone. python -I 12_single_test.py --family N --draws 100000 Writes ../out/single_test_.json """ import argparse import importlib.util import json import os import sys import numpy as np HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) import rr_core as rr # noqa: E402 spec = importlib.util.spec_from_file_location("null05", os.path.join(HERE, "05_null.py")) null05 = importlib.util.module_from_spec(spec) spec.loader.exec_module(null05) ap = argparse.ArgumentParser() ap.add_argument("--family", default="N") ap.add_argument("--draws", type=int, default=100000) ap.add_argument("--batch", type=int, default=2000) a = ap.parse_args() days, kinds, levels = rr.load_family(a.family) masks = rr.set_masks(kinds, levels) W = 7 sets = {nm: m for nm, m in zip(rr.SET_NAMES, masks)} sets["GLE alone"] = kinds == 1 sets["S-list alone"] = (kinds == 0) & (levels >= 10) for nm, m in zip(rr.SET_NAMES, masks): sets[nm + " declustered"] = rr.decluster_mask(days, m, W) nine = np.array([rr.PERIODS[n] for n in rr.NAMES]) out = dict(family=a.family, draws=a.draws, W=W, names=rr.NAMES, sets={}) cid = rr.clusters(days, W) nc = cid.max() + 1 # resultant of each cluster at each period, per set: (sets, periods, clusters) for sname, m in sets.items(): N = int(m.sum()) zobs = rr.z_direct(days[m], nine) row = dict(N=N, z=np.round(zobs, 2).tolist(), p_naive_exp=[float(np.exp(-z)) for z in zobs], nulls={}) out["sets"][sname] = row rcs = {} for sname, m in sets.items(): d = days[m].astype(float) c = cid[m] arr = np.zeros((9, nc), dtype=complex) for k, T in enumerate(nine): ph = np.exp(2j * np.pi * d / T) arr[k] = np.bincount(c, weights=ph.real, minlength=nc) + 1j * np.bincount(c, weights=ph.imag, minlength=nc) rcs[sname] = arr for null in ("Ap", "Bp", "C"): mv = null05.Mover(days, W, null, 777000 + sum(ord(x) for x in null)) counts = {s: np.zeros(9) for s in sets} means = {s: np.zeros(9) for s in sets} done = 0 while done < a.draws: n = min(a.batch, a.draws - done) sh = mv.shifts(n) # (n, clusters) for k, T in enumerate(nine): rot = np.exp(2j * np.pi * sh / T) # (n, clusters) for sname in sets: total = (rcs[sname][k][None, :] * rot).sum(axis=1) zb = (total.real ** 2 + total.imag ** 2) / out["sets"][sname]["N"] counts[sname][k] += (zb >= out["sets"][sname]["z"][k]).sum() means[sname][k] += zb.sum() done += n for sname in sets: out["sets"][sname]["nulls"][null] = dict( p=np.round((1 + counts[sname]) / (a.draws + 1), 5).tolist(), mean_z=np.round(means[sname] / a.draws, 2).tolist(), ) print("done", null, flush=True) rr.save_json(os.path.join(rr.OUT, f"single_test_{a.family}.json"), out) for sname in sets: r = out["sets"][sname] print(sname.ljust(24), r["N"], "z", r["z"]) for null in ("Ap", "Bp", "C"): print(" ", null, r["nulls"][null]["p"], " mean z", r["nulls"][null]["mean_z"])