PureOne's picture
Upload Xhorielun-Sigma research dataset release
92c528d verified
Raw History Blame Contribute Delete
1.74 kB
"""Immutable statistical gate for a frozen pair of candidate snapshots.
The caller is responsible for fresh confirmation data and for freezing the
candidate BEFORE those data are obtained. The gate cannot verify data provenance.
"""
from dataclasses import dataclass
from math import log, sqrt, isfinite
@dataclass(frozen=True)
class Verdict:
attempt: int
alpha: float
n: int
mean: float
radius: float
lower_bound: float
required_gain: float
accepted: bool
def allocation(total, attempt):
if not 0<total<1 or not isinstance(attempt,int) or isinstance(attempt,bool) or attempt<1:
raise ValueError('Bad error budget')
return total/(attempt*(attempt+1))
def confirm(differences, total_alpha, attempt, required_gain=0.0):
if not isfinite(required_gain) or not 0<=required_gain<=1:
raise ValueError('Required gain must be in [0,1]')
data=list(differences)
if not data or any(not -1<=x<=1 for x in data):
raise ValueError('Need differences in [-1,1]')
alpha=allocation(total_alpha,attempt)
mean=sum(data)/len(data)
radius=sqrt(2*log(1/alpha)/len(data))
lower=mean-radius
return Verdict(attempt,alpha,len(data),mean,radius,lower,required_gain,
lower>required_gain)
def log_evalue(differences, null_margin, betting_rates):
"""Fixed/predictable nonnegative rates; no guarantee for hindsight rates."""
ds=list(differences); ls=list(betting_rates)
if not isfinite(null_margin) or not -1<=null_margin<=1 or len(ds)!=len(ls) or any(not -1<=d<=1 for d in ds) or any(not isfinite(l) or l<0 for l in ls):
raise ValueError('Invalid e-process inputs')
return sum(l*(d-null_margin)-l*l/2 for d,l in zip(ds,ls))