from pathlib import Path from fractions import Fraction as F from collections import Counter import json p=Path(__file__).parent def parse(s): a=s.split();h=a.index('hist');d={a[i]:int(a[i+1]) for i in range(0,h,2)};d['x']=d.get('x',d.get('tile'));d['hist']={int(k):int(v) for k,v in (x.split(':') for x in a[h+1:])};return d rows=[parse(s) for s in (p/'histograms.txt').read_text().splitlines()] refs=[parse(s) for s in (p/'actual-reference.txt').read_text().splitlines()] for ref in refs:assert next(r for r in rows if r['x']==ref['x'] and r['rep']==-1)['hist']==ref['hist'] def variance(r): h=r['hist'];W,D,H=r['W'],r['D'],r['H'];assert sum(h.values())==W and sum(k*v for k,v in h.items())==H*D return F(sum(k*k*v for k,v in h.items()),W)-F(H*D,W)**2 # Independent exact CRT pair-correlation formula from06-variance-theorem.js. # This also prevents misreading a second-moment statistic as higher-point correlation evidence. ps=[2,3,5,7,11,13,17,19,23] for ref in refs: W,D,H,x=ref['W'],ref['D'],ref['H'],ref['x'] second=H*D for d in range(1,H): c=1 for q in ps: if q>x:break c*=q-len({0,(-2)%q,(-d)%q,(-d-2)%q}) second+=2*(H-d)*c assert F(second,W)-F(H*D,W)**2==variance(ref) results=[] for x in [13,19,23,0]: rs={r['rep']:r for r in rows if r['x']==x};vs={k:variance(v) for k,v in rs.items()};null=[vs[k] for k in range(127)];med=sorted(null)[63];ratio=vs[-1]/med;rank=F(1+sum(v<=vs[-1] for v in null),128);cal=F(1+sum(v<=vs[127] for v in null),128);advance=rank<=F(1,100) and ratio<=F(1,2) if x==0:assert advance and vs[-1]==0 r={'x':x,'actual_variance':str(vs[-1]),'null_median_variance':str(med),'ratio':str(ratio),'ratio_float':float(ratio),'p':str(rank),'advance':advance,'pseudo_null_p':str(cal),'all_variances':{str(k):str(v) for k,v in vs.items()}} results.append(r);print({k:v for k,v in r.items() if k!='all_variances'}) # Independent direct sliding count on ALL integer translations of a shuffled T13 cycle. r=next(r for r in rows if r['x']==13 and r['rep']==0);g=[6*int(v) for v in (p/'fixture.txt').read_text().split()];assert sum(g)==r['W'];pos=0;live=bytearray(r['W']) for gap in g:pos+=gap;live[pos%r['W']]=1 W,H=r['W'],r['H'];n=sum(live[1:H+1]);hist=Counter() for a in range(W): hist[n]+=1;n-=live[(a+1)%W];n+=live[(a+H+1)%W] assert dict(hist)==r['hist'] (p/'analysis.json').write_text(json.dumps({'results':results,'independent_fixture_all_translates':W,'actual_reference_matches':3,'exact_CRT_variance_matches':3},indent=2)+'\n')