File size: 3,213 Bytes
ec65441 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #!/usr/bin/env python3
"""Sensitivity of the procedure's verdicts to its own prespecified thresholds.
Answers a reviewer question: how do the four synthetic verdicts move as the
H >= 0.05 ratio-stability guard and the 5 percent undefined-draw rule vary?
Also varies the minimum benefit and harm tolerance, which are the thresholds
that actually enter the verdict function.
All records are declared synthetic constructions, not observations.
"""
import csv, math, random, statistics
from pathlib import Path
import worked_headroom as W
CASES = W.CASES
def boot(name, counts, alpha=0.05, B=50_000):
both, p_only, b_only, neither = counts
pairs=([(1,1)]*both+[(0,1)]*p_only+[(1,0)]*b_only+[(0,0)]*neither)
n=len(pairs); rng=random.Random(f"{W.SEED}:{name}")
d=[]; r=[]; undef=0
for _ in range(B):
dr=[pairs[rng.randrange(n)] for _ in range(n)]
a0=sum(x for x,_ in dr)/n; a1=sum(y for _,y in dr)/n
d.append(a1-a0)
if a0==1: undef+=1
else: r.append((a1-a0)/(1-a0))
return (W.percentile(d,alpha/2), W.percentile(d,1-alpha/2),
undef/B, (both+b_only)/n, (both+p_only)/n)
def verdict(dl,dh,H,min_benefit,harm_tol):
if H==0: return "no-opportunity"
if min_benefit>H: return "design-infeasible"
if dl>=min_benefit: return "benefit-supported"
if dh<=-harm_tol: return "harm-supported"
if dl>=-harm_tol and dh<=min_benefit: return "practically-negligible"
return "unresolved"
print("=== cached bootstraps ===")
B={}
for nm,c in CASES.items():
B[nm]=boot(nm,c); print(f" {nm:18} dCI=[{B[nm][0]:+.3f},{B[nm][1]:+.3f}] undef={B[nm][2]:.3%} a0={B[nm][3]:.2f}")
print("\n=== (a) vary the RATIO-STABILITY GUARDS: H floor and max undefined ===")
rows=[]
for hf in (0.02,0.05,0.10):
for mu in (0.01,0.05,0.10,0.25,0.40,1.00):
line=[]
for nm,c in CASES.items():
dl,dh,undef,a0,a1=B[nm]; H=1-a0
r_rep = (H>=hf and undef<=mu and H>0)
v=verdict(dl,dh,H,W.MIN_BENEFIT,W.HARM_TOLERANCE)
line.append(f"{nm.split('_')[0]}:{'R' if r_rep else '-'}/{v[:6]}")
rows.append({"H_floor":hf,"max_undefined":mu,"case":nm,
"R_reported":r_rep,"verdict":v})
print(f" Hfloor={hf:.2f} maxundef={mu:.2f} "+" ".join(line))
vs={(r['case'],) for r in rows}
import collections
print("\n verdicts per case across ALL guard settings:")
for nm in CASES:
s={r['verdict'] for r in rows if r['case']==nm}
rr={r['R_reported'] for r in rows if r['case']==nm}
print(f" {nm:18} verdict(s)={s} R_reported varies={len(rr)>1}")
print("\n=== (b) vary MIN_BENEFIT (delta_b) and HARM_TOLERANCE (delta_h) ===")
for mb in (0.01,0.02,0.05,0.10):
for ht in (0.01,0.02,0.05):
line=[]
for nm,c in CASES.items():
dl,dh,undef,a0,a1=B[nm]; H=1-a0
line.append(f"{nm.split('_')[0]}:{verdict(dl,dh,H,mb,ht)[:9]}")
print(f" d_b={mb:.2f} d_h={ht:.2f} "+" ".join(line))
rows2=[]
out=Path("guard_sensitivity_results.csv")
with out.open("w",newline="",encoding="utf-8") as fh:
wr=csv.DictWriter(fh,fieldnames=list(rows[0])); wr.writeheader(); wr.writerows(rows)
print(f"\nwrote {out}")
|