"""Exact finite geometric ranking and filter example; not HNSW implementation.""" import json,math SLUG="ann-recall-contracts" def run(): # True distance d_i=i/100 for deterministic1D corpus, q=0. points=[(i,i/100) for i in range(1,101)] truth=[i for i,d in points[:10]] budgets=[1,2,5,10,20,40,60,80,100] # Candidate order is even identifiers followed by odd: intentionally biased. order=list(range(2,101,2))+list(range(1,100,2)) recall=[] for b in budgets: candidates=order[:b] top=sorted(candidates)[:10] recall.append(len(set(top)&set(truth))/10) assert recall[3]==.5 and recall[-1]==1 # Filter eligible iff index>=91; postfilter top20 yields none, eligible exacttop5 exist. eligible=[i for i,d in points if i>=91] assert len([i for i in range(1,21) if i>=91])==0 and eligible[:5]==[91,92,93,94,95] # Additive per-distance error bounded eps: reversal impossible when gap>2eps. epss=[0,.001,.0025,.005,.01,.02,.04] gap=.01 margins=[gap-2*e for e in epss] assert abs(margins[3])<1e-12 and margins[4]<0 checks=0 for eps in [.001,.002,.004]: for e1 in [-eps,0,eps]: for e2 in [-eps,0,eps]: assert .10+e1<.11+e2;checks+=1 return {"slug":SLUG,"checks":[{"name":"Candidate recall against exhaustive ground truth","passed":True,"cases":len(budgets),"detail":"Biased even-first candidate sequence: at budget 10, recall@10 = 0.5; at full budget, recall@10 = 1."},{"name":"Filtering after truncation counterexample","passed":True,"cases":1,"detail":"Eligible IDs 91..100: the unfiltered top 20 contains no eligible item, although 10 exist."},{"name":"Bounded-distance-error ranking margin","passed":True,"cases":checks,"detail":"All finite endpoint/zero perturbation pairs preserve the 0.01 gap when 2 * epsilon < 0.01."}],"figures":[{"id":"candidates","title":"Recall is controlled by which candidates survive","x_label":"Number of candidates inspected","y_label":"Recall@10 against exact top 10","series":[{"name":"Even-first constructed candidate ordering","x":budgets,"y":recall}],"caption":"A deterministic 100-point one-dimensional example. This candidate policy is deliberately simple and is not HNSW, DiskANN, Faiss or ScaNN.","assumptions":["Euclidean distance toquery 0; point i at i/100","Stable identifier tie-break; no equal distances"],"kind":"Constructed analytical model; not a measured benchmark"},{"id":"margin","title":"Distance error matters relative to the ranking gap","x_label":"Maximum absolute distance error epsilon","y_label":"Certified residual gap (distance units)","series":[{"name":"0.01 - 2 epsilon","x":epss,"y":margins}],"caption":"Positive values certify the order of two items under an assumed uniform error bound. Negative values mean the certificate is inconclusive, not that every ranking reverses.","assumptions":["True distance gap 0.01","Each approximate distance deviates by at most epsilon"],"kind":"Constructed analytical model; not a measured benchmark"}],"result_summary":{"recall_at_budget10":recall[3],"recall_at_full_budget":recall[-1],"postfilter_top20_eligible_count":0,"eligible_exact_top5":eligible[:5],"strict_margin_epsilon_threshold":.005}} if __name__=="__main__":print(json.dumps(run(),indent=2))