forecast-interval-lab / test_quantiles.py
WideChaos's picture
Publish fixed forecast interval study
747f64a verified
Raw History Blame Contribute Delete
1.86 kB
"""Known order-statistic cases and exhaustive exchangeable rank enumeration."""
import itertools
import json
import math
from quantiles import threshold,interval,covers
def run():
assert threshold([0,1,2,3],1,5)['rank']==4 and threshold([0,1,2,3],1,5)['threshold']==3
assert threshold([0,1,2,3],1,10)['unbounded'] and threshold([0,1,2,3],1,10)['threshold'] is None
assert interval(2,threshold([0,1,2,3],1,10))==dict(lower=0.,upper=None,width=None,unbounded=True)
assert threshold(list(range(49)),29,50)['rank']==21 and threshold(list(range(49)),29,50)['threshold']==20
assert math.ceil(50*(1-.58))==22 # Deliberate binary-float boundary; the rational rule above stays21.
assert interval(1,threshold([2,2,2,2],1,5))==dict(lower=0.,upper=3.,width=3.,unbounded=False)
assert covers(3,1,threshold([2,2,2,2],1,5)) and not covers(3.01,1,threshold([2,2,2,2],1,5))
covered=0;unbounded=0;trials=0
for values in itertools.permutations([1,2,3,4,5]):
q=threshold(list(values[:4]),1,5);covered+=covers(values[-1],0,q);unbounded+=covers(values[-1],0,threshold(list(values[:4]),1,10));trials+=1
assert trials==120 and covered==96 and unbounded==120
assert covers(0,0,threshold([0]*4,1,5))
for scores,a,d in [([],1,10),([True],1,10),([-1],1,10),([float('nan')],1,10),([1e10],1,10),([1],0,10),([1],10,10),([1],1.,10),([1],1,10001)]:
try:threshold(scores,a,d)
except ValueError:pass
else:raise AssertionError('Invalid rank input accepted')
return dict(status='PASS',distinct_rank_permutations=trials,alpha_one_fifth_covered=covered,alpha_one_tenth_unbounded_covered=unbounded,exact_rational_boundary_rank=21,scope='Unit rank calculation and exchangeable-position enumeration only; not historical bike coverage or a new fitted model.')
if __name__=='__main__':print(json.dumps(run(),indent=2))