forked from Stream-AD/MIDAS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanom.cpp
88 lines (77 loc) · 3.26 KB
/
anom.cpp
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
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include "anom.hpp"
#include "edgehash.hpp"
#include "nodehash.hpp"
vector<double>* midas(vector<int>& src, vector<int>& dst, vector<int>& times, int num_rows, int num_buckets)
{
int m = *max_element(src.begin(), src.end());
Edgehash cur_count(num_rows, num_buckets, m);
Edgehash total_count(num_rows, num_buckets, m);
vector<double>* anom_score = new vector<double>(src.size());
int cur_t = 1, size = src.size(), cur_src, cur_dst;
double cur_mean, sqerr, cur_score;
for (int i = 0; i < size; i++) {
if (i == 0 || times[i] > cur_t) {
cur_count.clear();
cur_t = times[i];
}
cur_src = src[i];
cur_dst = dst[i];
cur_count.insert(cur_src, cur_dst, 1);
total_count.insert(cur_src, cur_dst, 1);
cur_mean = total_count.get_count(cur_src, cur_dst) / cur_t;
sqerr = pow(cur_count.get_count(cur_src, cur_dst) - cur_mean, 2);
if (cur_t == 1) cur_score = 0;
else cur_score = sqerr / cur_mean + sqerr / (cur_mean * (cur_t - 1));
(*anom_score)[i] = cur_score;
}
return anom_score;
}
double counts_to_anom(double tot, double cur, int cur_t)
{
double cur_mean = tot / cur_t;
double sqerr = pow(MAX(0, cur - cur_mean), 2);
return sqerr / cur_mean + sqerr / (cur_mean * MAX(1, cur_t - 1));
}
vector<double>* midasR(vector<int>& src, vector<int>& dst, vector<int>& times, int num_rows, int num_buckets, double factor)
{
int m = *max_element(src.begin(), src.end());
Edgehash cur_count(num_rows, num_buckets, m);
Edgehash total_count(num_rows, num_buckets, m);
Nodehash src_score(num_rows, num_buckets);
Nodehash dst_score(num_rows, num_buckets);
Nodehash src_total(num_rows, num_buckets);
Nodehash dst_total(num_rows, num_buckets);
vector<double>* anom_score = new vector<double>(src.size());
int cur_t = 1, size = src.size(), cur_src, cur_dst;
double cur_score, cur_score_src, cur_score_dst, combined_score;
for (int i = 0; i < size; i++) {
if (i == 0 || times[i] > cur_t) {
cur_count.lower(factor);
src_score.lower(factor);
dst_score.lower(factor);
cur_t = times[i];
}
cur_src = src[i];
cur_dst = dst[i];
cur_count.insert(cur_src, cur_dst, 1);
total_count.insert(cur_src, cur_dst, 1);
src_score.insert(cur_src, 1);
dst_score.insert(cur_dst, 1);
src_total.insert(cur_src, 1);
dst_total.insert(cur_dst, 1);
cur_score = counts_to_anom(total_count.get_count(cur_src, cur_dst), cur_count.get_count(cur_src, cur_dst), cur_t);
cur_score_src = counts_to_anom(src_total.get_count(cur_src), src_score.get_count(cur_src), cur_t);
cur_score_dst = counts_to_anom(dst_total.get_count(cur_dst), dst_score.get_count(cur_dst), cur_t);
//combined_score = MAX(cur_score_src, cur_score_dst) + cur_score;
//combined_score = cur_score_src + cur_score_dst + cur_score;
combined_score = MAX(MAX(cur_score_src, cur_score_dst), cur_score);
(*anom_score)[i] = log(1 + combined_score);
}
return anom_score;
}