headroom-decision-procedure / code /severity_scaled_case.py
carlostoxtli's picture
Upload Headroom Decision Procedure Examples v1.0.0
ec65441 verified
Raw History Blame Contribute Delete
3.38 kB
#!/usr/bin/env python3
"""A declared synthetic case where the accuracy gate and the loss gate disagree.
Addresses a reviewer point: the opportunity gate is stated in accuracy units,
but accuracy is often not the decision-relevant outcome. Here accuracy headroom
is negligible while severity-weighted loss headroom is material, so the two
scales return opposite verdicts on identical data.
Construction, not an observation.
"""
import math, random, statistics
from pathlib import Path
import csv
SEED=20260829; B=50_000; ALPHA=0.05
MIN_BENEFIT=0.02; HARM_TOL=0.02
# 100 items: 10 high-severity (weight 1.0), 90 low-severity (weight 0.1).
# Baseline errs on one HIGH-severity item; protocol fixes it but errs on one
# LOW-severity item. Accuracy is identical; severity-weighted loss is not.
items=[]
for i in range(10): # high severity
base_ok = (i!=0) # baseline wrong on item 0
prot_ok = True # protocol right on all high-severity items
items.append((1.0, base_ok, prot_ok))
for i in range(90): # low severity
base_ok = True
prot_ok = (i!=0) # protocol wrong on one low-severity item
items.append((0.1, base_ok, prot_ok))
def acc(sample):
n=len(sample)
return (sum(1 for _,b,_ in sample if b)/n, sum(1 for _,_,p in sample if p)/n)
def loss(sample):
tw=sum(w for w,_,_ in sample)
l0=sum(w for w,b,_ in sample if not b)/tw
l1=sum(w for w,_,p in sample if not p)/tw
return l0,l1
def pct(v,q):
o=sorted(v); pos=q*(len(o)-1); lo=math.floor(pos); hi=math.ceil(pos)
return o[lo] if lo==hi else o[lo]*(1-(pos-lo))+o[hi]*(pos-lo)
a0,a1=acc(items); l0,l1=loss(items)
H_acc=1-a0; D_acc=a1-a0
L_STAR=0.0 # declared attainable loss floor
H_loss=l0-L_STAR; D_loss=l0-l1 # loss reduction is a benefit
rng=random.Random(f"{SEED}:severity")
da=[]; dl=[]
for _ in range(B):
s=[items[rng.randrange(len(items))] for _ in range(len(items))]
x0,x1=acc(s); m0,m1=loss(s)
da.append(x1-x0); dl.append(m0-m1)
da_lo,da_hi=pct(da,ALPHA/2),pct(da,1-ALPHA/2)
dl_lo,dl_hi=pct(dl,ALPHA/2),pct(dl,1-ALPHA/2)
def verdict(d_lo,d_hi,H):
if H==0: return "no-opportunity"
if MIN_BENEFIT>H: return "design-infeasible"
if d_lo>=MIN_BENEFIT: return "benefit-supported"
if d_hi<=-HARM_TOL: return "harm-supported"
if d_lo>=-HARM_TOL and d_hi<=MIN_BENEFIT: return "practically-negligible"
return "unresolved"
print("=== identical data, two outcome scales ===")
print(f" ACCURACY : a0={a0:.3f} a1={a1:.3f} H={H_acc:.3f} D={D_acc:+.3f} "
f"CI=[{da_lo:+.3f},{da_hi:+.3f}] -> {verdict(da_lo,da_hi,H_acc)}")
print(f" LOSS : l0={l0:.4f} l1={l1:.4f} H={H_loss:.4f} (floor {L_STAR}) "
f"D={D_loss:+.4f} CI=[{dl_lo:+.4f},{dl_hi:+.4f}] -> {verdict(dl_lo,dl_hi,H_loss)}")
print(f" R_loss = D/H = {D_loss/H_loss:.3f}")
print(f"\n weights: 10 items at 1.0, 90 items at 0.1 (total {sum(w for w,_,_ in items):.1f})")
with Path("severity_scaled_results.csv").open("w",newline="",encoding="utf-8") as fh:
wr=csv.writer(fh)
wr.writerow(["scale","baseline","protocol","headroom","delta","ci_low","ci_high","verdict"])
wr.writerow(["accuracy",a0,a1,H_acc,D_acc,da_lo,da_hi,verdict(da_lo,da_hi,H_acc)])
wr.writerow(["severity_weighted_loss",l0,l1,H_loss,D_loss,dl_lo,dl_hi,verdict(dl_lo,dl_hi,H_loss)])
print("wrote severity_scaled_results.csv")