-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevaluator.py
88 lines (80 loc) · 2.8 KB
/
evaluator.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
import numpy as np
def evaluate(prediction, ground_truth, mask, report=False):
assert ground_truth.shape == prediction.shape, 'shape mis-match'
performance = {}
# mse
performance['mse'] = np.linalg.norm((prediction - ground_truth) * mask) ** 2 / np.sum(mask)
mrr_top = 0.0
all_miss_days_top = 0
bt_long = 1.0
bt_long5 = 1.0
bt_long10 = 1.0
for i in range(prediction.shape[1]):
rank_gt = np.argsort(ground_truth[:, i])
gt_top1 = set()
gt_top5 = set()
gt_top10 = set()
for j in range(1, prediction.shape[0] + 1):
cur_rank = rank_gt[-1 * j]
if mask[cur_rank][i] < 0.5:
continue
if len(gt_top1) < 1:
gt_top1.add(cur_rank)
if len(gt_top5) < 5:
gt_top5.add(cur_rank)
if len(gt_top10) < 10:
gt_top10.add(cur_rank)
rank_pre = np.argsort(prediction[:, i])
pre_top1 = set()
pre_top5 = set()
pre_top10 = set()
for j in range(1, prediction.shape[0] + 1):
cur_rank = rank_pre[-1 * j]
if mask[cur_rank][i] < 0.5:
continue
if len(pre_top1) < 1:
pre_top1.add(cur_rank)
if len(pre_top5) < 5:
pre_top5.add(cur_rank)
if len(pre_top10) < 10:
pre_top10.add(cur_rank)
# calculate mrr of top1
top1_pos_in_gt = 0
# for each stock rank 1 to 1026
# got the real rank of prediction top 1
for j in range(1, prediction.shape[0] + 1):
cur_rank = rank_gt[-1 * j]
if mask[cur_rank][i] < 0.5:
continue
else:
top1_pos_in_gt += 1
if cur_rank in pre_top1:
break
if top1_pos_in_gt == 0:
all_miss_days_top += 1
else:
mrr_top += 1.0 / top1_pos_in_gt
# back testing on top 1
real_ret_rat_top = ground_truth[list(pre_top1)[0]][i]
bt_long += real_ret_rat_top
# back testing on top 5
real_ret_rat_top5 = 0
for pre in pre_top5:
real_ret_rat_top5 += ground_truth[pre][i]
real_ret_rat_top5 /= 5
bt_long5 += real_ret_rat_top5
# back testing on top 10
real_ret_rat_top10 = 0
for pre in pre_top10:
real_ret_rat_top10 += ground_truth[pre][i]
real_ret_rat_top10 /= 10
bt_long10 += real_ret_rat_top10
# 1/real position average
performance['mrrt'] = mrr_top / (prediction.shape[1] - all_miss_days_top)
# prediction best return ratio
performance['btl'] = bt_long
# prediction top 5 average ratio
# performance['btl5'] = bt_long5
# top 10 average
# performance['btl10'] = bt_long10
return performance