"""Outcome-free consistency test: if every '±err' were an honest 1-sigma and forecasts independent (or positively
correlated), at most 4.6% of pairs would differ by more than 2*sqrt(sa^2+sb^2). Reads scored.csv and ../c24/c24_table2.csv."""
import csv, itertools, os, numpy as np
HERE = os.path.dirname(os.path.abspath(__file__))
S = [s for s in csv.DictReader(open(os.path.join(HERE, 'scored.csv'))) if s['err_plus'] not in ('', 'nan')]
c25 = [(float(s['value_v2']), (float(s['err_plus']) + float(s['err_minus'])) / 2, int(s['year'])) for s in S]
c24p = os.path.join(HERE, '..', 'c24', 'c24_table2.csv')
if not os.path.exists(c24p): c24p = os.path.join(HERE, 'c24_table2.csv')
c24 = [(float(r['value']), float(r['err']), 0) for r in csv.DictReader(open(c24p)) if r['err']]
for name, vals in [('Cycle 25, published by 2019', [v for v in c25 if v[2] <= 2019]), ('Cycle 25, all', c25), ('Cycle 24', c24)]:
    P = list(itertools.combinations(vals, 2))
    d = np.array([abs(a[0] - b[0]) / np.hypot(a[1], b[1]) for a, b in P])
    disj = np.mean([abs(a[0] - b[0]) > a[1] + b[1] for a, b in P])
    print(f'{name}: {len(vals)} forecasts, {len(P)} pairs; >2 combined sigma {np.mean(d > 2):.0%} (<=4.6% if honest), '
          f'>3 {np.mean(d > 3):.0%} (<=0.3%), disjoint ±1 bars {disj:.0%}')
