-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleAnomalyDetector.cpp
116 lines (104 loc) · 4.39 KB
/
SimpleAnomalyDetector.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
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
//Orpaz Sondhelm 206492324 Yarin Tzdaka 319091278
#include "SimpleAnomalyDetector.h"
SimpleAnomalyDetector::SimpleAnomalyDetector() {
// TODO Auto-generated constructor stub
}
SimpleAnomalyDetector::~SimpleAnomalyDetector() {
// TODO Auto-generated destructor stub
}
void SimpleAnomalyDetector::HowToLearn(float m, float c, int i, vector<pair<string, vector<float>>> columns, int sizeLines) {
if (c != -1 && m >= this->thresh) {
correlatedFeatures newCF;
newCF.corrlation = m;
newCF.feature1 = columns[i].first;
newCF.feature2 = columns[c].first;
newCF.lin_reg = linear_reg(&columns[i].second[0], &columns[c].second[0], sizeLines);
newCF.threshold = max_dev(&columns[i].second[0], &columns[c].second[0], newCF.lin_reg, sizeLines);
this->cf.push_back(newCF);
}
}
void SimpleAnomalyDetector::learnNormal(const TimeSeries& ts){
// TODO Auto-generated destructor stub
vector<pair<string, vector<float>>> columns = ts.getVector();
int sizeColumns = columns.size(); //size of the columns in the table
int sizeLines = columns[0].second.size(); //size of the lines in the table
for (int i = 0; i < sizeColumns; i++) {
float m = 0, c = -1;
for (int j = i +1; j < sizeColumns; j++) {
float* a = &columns[i].second[0];
float* b = &columns[j].second[0];
float p = pearson(a, b, sizeLines);
if (p > m ) {
m = p;
c = j;
}
}
HowToLearn(m, c, i, columns, sizeLines);
}
}
string SimpleAnomalyDetector::findCorFeature (string featureName){
for(correlatedFeatures CFpair : this->cf){
if(CFpair.feature1.compare(featureName) == 0){
return CFpair.feature2;
}
return "";
}
}
int SimpleAnomalyDetector::isThereCorFeature(string featureName) {
for (int i = 0; i < this->cf.size(); i++) {
correlatedFeatures CFpair = this->cf.at(i);
if (CFpair.feature1.compare(featureName) == 0) {
return i;
}
}
return -1;
}
correlatedFeatures SimpleAnomalyDetector::getCF(int corFeatIndex) {
return this->cf.at(corFeatIndex);
}
void SimpleAnomalyDetector::HowToDetect(correlatedFeatures* currentCF, vector<float>* targetVector,
vector<float>* currentVector, string* currentFeature,
vector<AnomalyReport>* anomalyReportVector) {
Line regLine = currentCF->lin_reg;
float threshold = currentCF->threshold;
for(int k = 0; k < currentVector->size(); k++){
Point p1(currentVector->at(k), targetVector->at(k));
if(dev(p1,regLine) >= threshold * 1.1){
AnomalyReport anomalyReport(*currentFeature + "-" + currentCF->feature2, k+1);
anomalyReportVector->push_back(anomalyReport);
}
}
}
vector<AnomalyReport> SimpleAnomalyDetector::detect(const TimeSeries &ts) {
vector<AnomalyReport> anomalyReportVector;
int tsVectorSize = ts.getVector().size();
for (int i = 0; i < tsVectorSize; i++) {
string currentFeature = ts.getVector().at(i).first;
// return the correlated feature index from the cf if the pair exist, -1 otherwise.
int CorFeatIndex = isThereCorFeature(currentFeature);
if(CorFeatIndex != -1){
// return the appropriate correlated pair.
correlatedFeatures currentCF = getCF(CorFeatIndex);
// the feature who corellated to the current feature
string feature2 = currentCF.feature2;
// to search the second feature in the
int j = i + 1;
// the next pair in ts (TimeSeries)
pair<string, vector<float>> targetPair = ts.getVector().at(j);
string targetFeature = targetPair.first;
// find the correlative feature from ts
while (feature2 != targetFeature) {
j++;
targetPair = ts.getVector().at(j);
targetFeature= targetPair.first;
}
// vectors from ts to compare with the normal line - reg.
vector<float> targetVector = targetPair.second;
vector<float> currentVector = ts.getVector().at(i).second;
HowToDetect(¤tCF, &targetVector, ¤tVector,
¤tFeature, &anomalyReportVector);
}
}
// return
return anomalyReportVector;
}