"""Declared finite analytical controls; no neural training or source benchmark reproduction.""" import json,math,itertools,hashlib from pathlib import Path ROOT=Path(__file__).resolve().parents[1] checks=[] cases=0 def check(ok,label): global cases assert ok,label cases+=1 if label not in checks: checks.append(label) def fig(fid,title,xlabel,ylabel,x,series,caption): return dict(id=fid,title=title,kind='analytical_curve',x_label=xlabel,y_label=ylabel,x={'name':xlabel,'values':x},series=[{'name':n,'values':v}for n,v in series],caption=caption) def recent(m,L,h): return min(h,L)/L if m==0 else -math.expm1(-m*min(h,L))/-math.expm1(-m*L) for w in [.01,.1,.7]: for s in [1,2,4]: for i,j,t in [(0,1,7),(3,19,-4),(20,80,100)]: dot=lambda a,b:math.cos(w*a/s)*math.cos(w*b/s)+math.sin(w*a/s)*math.sin(w*b/s) check(abs(dot(i,j)-dot(i+t,j+t))<1e-12,'rotation translation invariance') sep=math.hypot(math.cos(w*(i+1)/s)-math.cos(w*i/s),math.sin(w*(i+1)/s)-math.sin(w*i/s)) check(abs(sep-2*abs(math.sin(w/(2*s))))<1e-12,'adjacent resolution formula') for m in [0,.01,.05,.2]: for L in [4,16,64,256]: for h in [1,4,16,300]: weights=[math.exp(-m*d)for d in range(L)] check(abs(sum(weights[:h])/sum(weights)-recent(m,L,h))<1e-12,'finite sum equals geometric recent mass') for m in [.01,.05,.2]: for h in [1,4,16]: limit=-math.expm1(-m*h) length=math.ceil(40/m) check(abs(recent(m,length,h)-limit)<1e-14,'finite recent mass approaches infinite-support limit') check(recent(m,16,h)>=recent(m,256,h)>=limit-1e-14,'recent mass decreases toward limit') d=list(range(0,129,4));lens=[16,32,64,128,256,512] figures=[fig('rotation-compression','Range compression changes phase resolution','Token distance d','Cosine similarity',[*d],[(f'Position scale {s}',[math.cos(.1*x/s)for x in d])for s in [1,2,4]],'One-frequency author construction, omega=0.1 rad/position. Not a full RoPE spectrum or model retrieval accuracy.'), fig('recency-horizon','Mass assigned to the latest 16 positions','Context length L (tokens)','Probability mass',lens,[(f'Slope {m}',[recent(m,L,16)for L in lens])for m in [0,.01,.05,.2]],'Content-neutral causal scores -m*d. All keys have equal content scores; learned content can offset the bias.')] out={'article_slug':'positional-extrapolation-limits','figures':figures} (ROOT/'figures').mkdir(exist_ok=True) (ROOT/'figures/positional-extrapolation-limits.json').write_text(json.dumps(out,indent=2)+'\n',encoding='utf-8') receipt={'status':'PASS','checks':checks,'cases':cases,'empirical_benchmark':False,'source_proofs_verified':False,'figure_sha256':hashlib.sha256((ROOT/'figures/positional-extrapolation-limits.json').read_bytes()).hexdigest()} (ROOT/'analysis/positional-extrapolation-limits.receipt.json').write_text(json.dumps(receipt,indent=2)+'\n',encoding='utf-8') print(json.dumps(receipt))