-
Notifications
You must be signed in to change notification settings - Fork 16
/
VennABERS.py
183 lines (133 loc) · 4.61 KB
/
VennABERS.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# Straight-forward implementation of IVAP algorithm described in:
# Large-scale probabilistic prediction with and without validity guarantees, Vovk et al.
# https://arxiv.org/pdf/1511.00213.pdf
#
# Paolo Toccaceli
#
# https://github.com/ptocca/VennABERS
#
# 2020-07-09: Fixed bug in p0 calculation
import numpy as np
# Some elementary functions to speak the same language as the paper
# (at some point we'll just replace the occurrence of the calls with the function body itself)
def push(x,stack):
stack.append(x)
def pop(stack):
return stack.pop()
def top(stack):
return stack[-1]
def nextToTop(stack):
return stack[-2]
# perhaps inefficient but clear implementation
def nonleftTurn(a,b,c):
d1 = b-a
d2 = c-b
return np.cross(d1,d2)<=0
def nonrightTurn(a,b,c):
d1 = b-a
d2 = c-b
return np.cross(d1,d2)>=0
def slope(a,b):
ax,ay = a
bx,by = b
return (by-ay)/(bx-ax)
def notBelow(t,p1,p2):
p1x,p1y = p1
p2x,p2y = p2
tx,ty = t
m = (p2y-p1y)/(p2x-p1x)
b = (p2x*p1y - p1x*p2y)/(p2x-p1x)
return (ty >= tx*m+b)
kPrime = None
# Because we cannot have negative indices in Python (they have another meaning), I use a dictionary
def algorithm1(P):
global kPrime
S = []
P[-1] = np.array((-1,-1))
push(P[-1],S)
push(P[0],S)
for i in range(1,kPrime+1):
while len(S)>1 and nonleftTurn(nextToTop(S),top(S),P[i]):
pop(S)
push(P[i],S)
return S
def algorithm2(P,S):
global kPrime
Sprime = S[::-1] # reverse the stack
F1 = np.zeros((kPrime+1,))
for i in range(1,kPrime+1):
F1[i] = slope(top(Sprime),nextToTop(Sprime))
P[i-1] = P[i-2]+P[i]-P[i-1]
if notBelow(P[i-1],top(Sprime),nextToTop(Sprime)):
continue
pop(Sprime)
while len(Sprime)>1 and nonleftTurn(P[i-1],top(Sprime),nextToTop(Sprime)):
pop(Sprime)
push(P[i-1],Sprime)
return F1
def algorithm3(P):
global kPrime
S = []
push(P[kPrime+1],S)
push(P[kPrime],S)
for i in range(kPrime-1,0-1,-1): # k'-1,k'-2,...,0
while len(S)>1 and nonrightTurn(nextToTop(S),top(S),P[i]):
pop(S)
push(P[i],S)
return S
def algorithm4(P,S):
global kPrime
Sprime = S[::-1] # reverse the stack
F0 = np.zeros((kPrime+1,))
for i in range(kPrime,1-1,-1): # k',k'-1,...,1
F0[i] = slope(top(Sprime),nextToTop(Sprime))
P[i] = P[i-1]+P[i+1]-P[i]
if notBelow(P[i],top(Sprime),nextToTop(Sprime)):
continue
pop(Sprime)
while len(Sprime)>1 and nonrightTurn(P[i],top(Sprime),nextToTop(Sprime)):
pop(Sprime)
push(P[i],Sprime)
return F0
def prepareData(calibrPoints):
global kPrime
ptsSorted = sorted(calibrPoints)
xs = np.fromiter((p[0] for p in ptsSorted),float)
ys = np.fromiter((p[1] for p in ptsSorted),float)
ptsUnique,ptsIndex,ptsInverse,ptsCounts = np.unique(xs,
return_index=True,
return_counts=True,
return_inverse=True)
a = np.zeros(ptsUnique.shape)
np.add.at(a,ptsInverse,ys)
# now a contains the sums of ys for each unique value of the objects
w = ptsCounts
yPrime = a/w
yCsd = np.cumsum(w*yPrime) # Might as well do just np.cumsum(a)
xPrime = np.cumsum(w)
kPrime = len(xPrime)
return yPrime,yCsd,xPrime,ptsUnique
def computeF(xPrime,yCsd):
global kPrime
P = {0:np.array((0,0))}
P.update({i+1:np.array((k,v)) for i,(k,v) in enumerate(zip(xPrime,yCsd))})
S = algorithm1(P)
F1 = algorithm2(P,S)
P = {0:np.array((0,0))}
P.update({i+1:np.array((k,v)) for i,(k,v) in enumerate(zip(xPrime,yCsd))})
P[kPrime+1] = P[kPrime] + np.array((1.0,0.0)) # The paper says (1,1)
S = algorithm3(P)
F0 = algorithm4(P,S)
return F0,F1
def getFVal(F0,F1,ptsUnique,testObjects):
pos0 = np.searchsorted(ptsUnique,testObjects,side='left')
pos1 = np.searchsorted(ptsUnique[:-1],testObjects,side='right')+1
return F0[pos0],F1[pos1]
def ScoresToMultiProbs(calibrPoints,testObjects):
# sort the points, transform into unique objects, with weights and updated values
yPrime,yCsd,xPrime,ptsUnique = prepareData(calibrPoints)
# compute the F0 and F1 functions from the CSD
F0,F1 = computeF(xPrime,yCsd)
# compute the values for the given test objects
p0,p1 = getFVal(F0,F1,ptsUnique,testObjects)
return p0,p1