"""Empirical pair credit and threshold ROC area on four fixed scores."""
from fractions import Fraction as F
import json
if __name__=="__main__":
    rows=[(1,F(9,10)),(1,F(6,10)),(0,F(6,10)),(0,F(2,10))]
    pos=[s for y,s in rows if y];neg=[s for y,s in rows if not y]
    credit=[F(1) if p>n else F(1,2) if p==n else F(0) for p in pos for n in neg]
    auc=sum(credit)/(len(pos)*len(neg));points=[(F(0),F(0))]
    for threshold in sorted({s for y,s in rows},reverse=True):
        tp=sum(y==1 and s>=threshold for y,s in rows);fp=sum(y==0 and s>=threshold for y,s in rows)
        points.append((F(fp,len(neg)),F(tp,len(pos))))
    area=sum((x2-x1)*(y1+y2)/2 for (x1,y1),(x2,y2) in zip(points,points[1:]))
    assert auc==area==F(7,8) and credit==[F(1),F(1),F(1,2),F(1)]
    reversed_credit=[F(1) if -p>-n else F(1,2) if p==n else F(0) for p in pos for n in neg]
    assert sum(reversed_credit)/4==F(1,8)
    print(json.dumps({"pair_credit":[str(x) for x in credit],"ROC_points_FPR_TPR":[[str(x),str(y)] for x,y in points],"pairwise_AUC":float(auc),"trapezoidal_ROC_AUC":float(area),"score_direction_reversed_AUC":.125,"actual_model_test":False},indent=2))
