-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleAnomalyDetector.cpp
102 lines (84 loc) · 3.18 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
//
// Created by rivka and vered on 09/11/2021.
//
#include <iostream>
#include "SimpleAnomalyDetector.h"
#include "TimeSeries.h"
#include "anomaly_detection_util.h"
#include <cmath>
using namespace std;
SimpleAnomalyDetector::SimpleAnomalyDetector(){
// TODO Auto-generated constructor stub
}
SimpleAnomalyDetector::~SimpleAnomalyDetector(){
// TODO Auto-generated destructor stub
}
void SimpleAnomalyDetector::learnNormal(const TimeSeries& ts){
for (int i = 0; i < ts.featureS() ; ++i) {
// ts.getDataT();
float maxValue = 0;
float pears = 0;
int index = 0;
for (int j = i+1; j <ts.featureS() ; ++j) {
float feature1x[ts.getAFeature(i).size()];
float feature2y[ts.getAFeature(j).size()];
for (int k = 1; k <ts.getAFeature(i).size(); ++k) {
feature1x[k] = ts.getAFeature(i).at(k);
feature2y[k] = ts.getAFeature(i).at(k);
}
pears = fabs(pearson(feature1x,feature2y,ts.featureS()));
if(pears > maxValue){
maxValue = pears;
index = j;
}
}
cout<<"hi";
if(maxValue >= 0.9){
cout<<"rivk";
cout<<"i"<<i<<"j"<<index<<maxValue;
correlatedFeatures cr;
cr.corrlation = maxValue;
cr.feature1 = ts.getTheFeaturesName().at(i);
cr.feature2 = ts.getTheFeaturesName().at(index) ;
cr.threshold = 0;
// float feature1xPoint[cr->feature1.size()];
// float feature2yPoint[cr->feature2.size()];
Point **pointArray = new Point*[cr.feature1.size()];
for (int j = 0; j < cr.feature1.size(); ++j) {
pointArray[j] = new Point(ts.getAFeature(i)[j],ts.getAFeature(index)[j]);
//feature1xPoint[j] = ts.getAFeature(i)[j];
// feature2yPoint[j] = ts.getAFeature(index)[j];
//pointArray[j]->x = ts.getAFeature(i)[j];
// pointArray[j]->y = ts.getAFeature(index)[j];
}
cr.lin_reg = linear_reg(pointArray,cr.feature1.size());
for (int j = 0; j < cr.feature1.size(); ++j) {
float deviation = dev(*pointArray[j], cr.lin_reg);
if(deviation > cr.threshold) {
cr.threshold = deviation*1.1;
}
}
this->cf.push_back(cr);
for (int j = 0; j < cr.feature1.size(); ++j) {
delete pointArray[j];
}
delete[] pointArray;
}
}
}
vector<AnomalyReport> SimpleAnomalyDetector::detect(const TimeSeries& ts) {
vector<float> vec;
vector<AnomalyReport> ar;
for (int i = 1; i <= ts.featureS(); ++i) {
vec = ts.getAFeature(i);
for (int j = 0; j < cf.size(); ++j) {
Point point(vec.at(ts.getFeatureN(cf.at(j).feature1)),
vec.at(ts.getFeatureN(cf.at(j).feature2)));
if (dev(point, cf.at(j).lin_reg) > cf.at(j).threshold) {
string anomalyR = cf.at(j).feature1 + "-" + cf.at(j).feature2;
ar.push_back(AnomalyReport(anomalyR, i));
}
}
}
return ar;
}