-
Notifications
You must be signed in to change notification settings - Fork 15
/
em.py
37 lines (33 loc) · 1.18 KB
/
em.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
import numpy as np
from sklearn.metrics import auc
def em(t, t_max, volume_support, s_unif, s_X, n_generated):
EM_t = np.zeros(t.shape[0])
n_samples = s_X.shape[0]
s_X_unique = np.unique(s_X)
EM_t[0] = 1.
for u in s_X_unique:
# if (s_unif >= u).sum() > n_generated / 1000:
EM_t = np.maximum(EM_t, 1. / n_samples * (s_X > u).sum() -
t * (s_unif > u).sum() / n_generated
* volume_support)
amax = np.argmax(EM_t <= t_max) + 1
if amax == 1:
print '\n failed to achieve t_max \n'
amax = -1
AUC = auc(t[:amax], EM_t[:amax])
return AUC, EM_t, amax
def mv(axis_alpha, volume_support, s_unif, s_X, n_generated):
n_samples = s_X.shape[0]
s_X_argsort = s_X.argsort()
mass = 0
cpt = 0
u = s_X[s_X_argsort[-1]]
mv = np.zeros(axis_alpha.shape[0])
for i in range(axis_alpha.shape[0]):
# pdb.set_trace()
while mass < axis_alpha[i]:
cpt += 1
u = s_X[s_X_argsort[-cpt]]
mass = 1. / n_samples * cpt # sum(s_X > u)
mv[i] = float((s_unif >= u).sum()) / n_generated * volume_support
return auc(axis_alpha, mv), mv