-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculate_falarm_rate.py
42 lines (39 loc) · 1.25 KB
/
calculate_falarm_rate.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
import numpy as np
def false_alarm_rate(target, predicted):
"""
calculate AUC and false alarm auc
Input:
target.shape = [n,1]
predicted.shape = [n,1]
Output:
PD_PF_auc
PF_tau_auc
"""
target = ((target-target.min()) /
(target.max()-target.min()))
predicted = ((predicted-predicted.min()) /
(predicted.max()-predicted.min()))
anomaly_map = target
normal_map = 1-target
num = 30000
taus = np.linspace(0,predicted.max(),num=num)
PF = np.zeros([num,1])
PD = np.zeros([num,1])
for index in range(num):
tau = taus[index]
anomaly_map_1 = np.double(predicted>=tau)
PF[index] = np.sum(anomaly_map_1*normal_map)/np.sum(normal_map)
PD[index] = np.sum(anomaly_map_1*anomaly_map)/np.sum(anomaly_map)
"""
plt.figure()
plt.plot(PF,PD)
plt.figure()
plt.plot(taus,PD)
plt.figure()
plt.plot(taus,PF)
plt.show()
"""
PD_PF_auc = np.sum((PF[0:num-1,:]-PF[1:num,:])*(PD[1:num]+PD[0:num-1])/2)
#area0 = np.trapz(PD.squeeze(),PF.squeeze())
PF_tau_auc = np.trapz(PF.squeeze(),taus.squeeze())
return PD_PF_auc, PF_tau_auc