-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleAnomalyDetector.cpp
More file actions
99 lines (83 loc) · 2.35 KB
/
SimpleAnomalyDetector.cpp
File metadata and controls
99 lines (83 loc) · 2.35 KB
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
#include "SimpleAnomalyDetector.h"
SimpleAnomalyDetector::SimpleAnomalyDetector() {
threshold = 0.9;
}
SimpleAnomalyDetector::SimpleAnomalyDetector(float t) {
threshold = t;
}
SimpleAnomalyDetector::~SimpleAnomalyDetector() {
}
Point** SimpleAnomalyDetector::toPoints(vector<float> x, vector<float> y){
Point** ps=new Point*[x.size()];
for(size_t i=0;i<x.size();i++){
ps[i]=new Point(x[i],y[i]);
}
return ps;
}
float SimpleAnomalyDetector::findThreshold(Point** ps,size_t len,Line rl){
float max=0;
for(size_t i=0;i<len;i++){
float d=abs(ps[i]->y - rl.f(ps[i]->x));
if(d>max)
max=d;
}
return max;
}
void SimpleAnomalyDetector::learnNormal(const TimeSeries& ts){
vector<string> atts=ts.gettAttributes();
size_t len=ts.getRowSize();
float vals[atts.size()][len];
for(size_t i=0;i<atts.size();i++){
for(size_t j=0;j<ts.getRowSize();j++){
vals[i][j]=ts.getAttributeData(atts[i])[j];
}
}
for(size_t i=0;i<atts.size();i++){
string f1=atts[i];
float max=0;
size_t jmax=0;
for(size_t j=i+1;j<atts.size();j++){
float p=abs(pearson(vals[i],vals[j],len));
if(p>max){
max=p;
jmax=j;
}
}
string f2=atts[jmax];
Point** ps=toPoints(ts.getAttributeData(f1),ts.getAttributeData(f2));
learnHelper(ts,max,f1,f2,ps);
// delete points
for(size_t k=0;k<len;k++)
delete ps[k];
delete[] ps;
}
}
void SimpleAnomalyDetector::learnHelper(const TimeSeries& ts,float p/*pearson*/,string f1, string f2,Point** ps){
if(p>threshold){
size_t len=ts.getRowSize();
correlatedFeatures c;
c.feature1=f1;
c.feature2=f2;
c.corrlation=p;
c.lin_reg=linear_reg(ps,len);
c.threshold=findThreshold(ps,len,c.lin_reg)*1.1; // 10% increase
cf.push_back(c);
}
}
vector<AnomalyReport> SimpleAnomalyDetector::detect(const TimeSeries& ts){
vector<AnomalyReport> v;
for_each(cf.begin(),cf.end(),[&v,&ts,this](correlatedFeatures c){
vector<float> x=ts.getAttributeData(c.feature1);
vector<float> y=ts.getAttributeData(c.feature2);
for(size_t i=0;i<x.size();i++){
if(isAnomalous(x[i],y[i],c)){
string d=c.feature1 + "-" + c.feature2;
v.push_back(AnomalyReport(d,(i+1)));
}
}
});
return v;
}
bool SimpleAnomalyDetector::isAnomalous(float x, float y,correlatedFeatures c){
return (abs(y - c.lin_reg.f(x))>c.threshold);
}