Skip to content

Commit

Permalink
start
Browse files Browse the repository at this point in the history
  • Loading branch information
OrpazSond committed Nov 10, 2021
1 parent d75ad0c commit ddb6b7b
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions AnomalyDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


#ifndef ANOMALYDETECTOR_H_
#define ANOMALYDETECTOR_H_

#include <string>
#include <vector>
#include "timeseries.h"
#include "math.h"
using namespace std;


class AnomalyReport{
public:
const string description;
const long timeStep;
AnomalyReport(string description, long timeStep):description(description),timeStep(timeStep){}
};

class TimeSeriesAnomalyDetector {
public:
virtual void learnNormal(const TimeSeries& ts)=0;
virtual vector<AnomalyReport> detect(const TimeSeries& ts)=0;
virtual ~TimeSeriesAnomalyDetector(){}
};

#endif /* ANOMALYDETECTOR_H_ */
111 changes: 111 additions & 0 deletions MainTrain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@


#include <iostream>
#include <vector>
#include "AnomalyDetector.h"
#include "SimpleAnomalyDetector.h"
#include <fstream>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <math.h>

using namespace std;

// this is a simple test to put you on the right track
void generateTrainCSV(float a1,float b1, float a2, float b2){
ofstream out("trainFile1.csv");
out<<"A,B,C,D"<<endl;
Line ac(a1,b1);
Line bd(a2,b2);
for(int i=1;i<=100;i++){
float a=i;
float b=rand()%40;
out<<a<<","<<b<<","<<ac.f(a)-0.02+(rand()%40)/100.0f<<","<<bd.f(b)-0.02+(rand()%40)/100.0f<<endl;
}
out.close();
}

void generateTestCSV(float a1,float b1, float a2, float b2, int anomaly){
ofstream out("testFile1.csv");
out<<"A,B,C,D"<<endl;
Line ac(a1,b1);
Line bd(a2,b2);
for(int i=1;i<=100;i++){
float a=i;
float b=rand()%40;
if(i!=anomaly)
out<<a<<","<<b<<","<<ac.f(a)-0.02+(rand()%40)/100.0f<<","<<bd.f(b)-0.02+(rand()%40)/100.0f<<endl;
else
out<<a<<","<<b<<","<<ac.f(a)+1<<","<<bd.f(b)-0.02+(rand()%40)/100.0f<<endl;
}
out.close();
}

void checkCorrelationTrain(correlatedFeatures c,string f1, string f2, float a, float b){
if(c.feature1==f1){
if(c.feature2!=f2)
cout<<"wrong correlated feature of "<<f1<<" (-20)"<<endl;
else{
if(c.corrlation<0.99)
cout<<f1<<"-"<<f2<<" wrong correlation detected (-5)"<<endl;
if(c.lin_reg.a<a-0.5f || c.lin_reg.a>a+0.5f)
cout<<f1<<"-"<<f2<<" wrong value of line_reg.a (-5)"<<endl;
if(c.lin_reg.b<b-0.5f || c.lin_reg.b>b+0.5f)
cout<<f1<<"-"<<f2<<" wrong value of line_reg.b (-5)"<<endl;
if(c.threshold>0.3)
cout<<f1<<"-"<<f2<<" wrong threshold detected (-5)"<<endl;
}
}

}

int main(){
srand (time(NULL));
float a1=1+rand()%10, b1=-50+rand()%100;
float a2=1+rand()%20 , b2=-50+rand()%100;


// test the learned model: (40 points)
// expected correlations:
// A-C: y=a1*x+b1
// B-D: y=a2*x+b2

generateTrainCSV(a1,b1,a2,b2);
TimeSeries ts("trainFile1.csv");
SimpleAnomalyDetector ad;
ad.learnNormal(ts);
vector<correlatedFeatures> cf=ad.getNormalModel();

if(cf.size()!=2)
cout<<"wrong size of correlated features (-40)"<<endl;
else
for_each(cf.begin(),cf.end(),[&a1,&b1,&a2,&b2](correlatedFeatures c){
checkCorrelationTrain(c,"A","C",a1,b1); // 20 points
checkCorrelationTrain(c,"B","D",a2,b2); // 20 points
});

// test the anomaly detector: (60 points)
// one simply anomaly is injected to the data
int anomaly=5+rand()%90; // one anomaly injected in a random time step
generateTestCSV(a1,b1,a2,b2,anomaly);
TimeSeries ts2("testFile1.csv");
vector<AnomalyReport> r = ad.detect(ts2);

bool anomlyDetected=false;
int falseAlarms=0;
for_each(r.begin(),r.end(),[&anomaly,&anomlyDetected,&falseAlarms](AnomalyReport ar){
if(ar.description=="A-C" && ar.timeStep == anomaly)
anomlyDetected=true;
else
falseAlarms++;
});

if(!anomlyDetected)
cout<<"the anomaly was not detected (-30)"<<endl;

if(falseAlarms>0)
cout<<"you have "<<falseAlarms<<" false alarms (-"<<min(30,falseAlarms*3)<<")"<<endl;

cout<<"done"<<endl;
return 0;
}
21 changes: 21 additions & 0 deletions SimpleAnomalyDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

#include "SimpleAnomalyDetector.h"

SimpleAnomalyDetector::SimpleAnomalyDetector() {
// TODO Auto-generated constructor stub

}

SimpleAnomalyDetector::~SimpleAnomalyDetector() {
// TODO Auto-generated destructor stub
}


void SimpleAnomalyDetector::learnNormal(const TimeSeries& ts){
// TODO Auto-generated destructor stub
}

vector<AnomalyReport> SimpleAnomalyDetector::detect(const TimeSeries& ts){
// TODO Auto-generated destructor stub
}

38 changes: 38 additions & 0 deletions SimpleAnomalyDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@


#ifndef SIMPLEANOMALYDETECTOR_H_
#define SIMPLEANOMALYDETECTOR_H_

#include "anomaly_detection_util.h"
#include "AnomalyDetector.h"
#include <vector>
#include <algorithm>
#include <string.h>
#include <math.h>

struct correlatedFeatures{
string feature1,feature2; // names of the correlated features
float corrlation;
Line lin_reg;
float threshold;
};


class SimpleAnomalyDetector:public TimeSeriesAnomalyDetector{
vector<correlatedFeatures> cf;
public:
SimpleAnomalyDetector();
virtual ~SimpleAnomalyDetector();

virtual void learnNormal(const TimeSeries& ts);
virtual vector<AnomalyReport> detect(const TimeSeries& ts);

vector<correlatedFeatures> getNormalModel(){
return cf;
}

};



#endif /* SIMPLEANOMALYDETECTOR_H_ */
48 changes: 48 additions & 0 deletions anomaly_detection_util.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* animaly_detection_util.cpp
*
* Author: write your ID and name here
*/

#include <math.h>
#include "anomaly_detection_util.h"

float avg(float* x, int size){
return 0;
}

// returns the variance of X and Y
float var(float* x, int size){
return 0;
}

// returns the covariance of X and Y
float cov(float* x, float* y, int size){
return 0;
}


// returns the Pearson correlation coefficient of X and Y
float pearson(float* x, float* y, int size){
return 0;
}

// performs a linear regression and returns the line equation
Line linear_reg(Point** points, int size){

return Line(0,0);
}

// returns the deviation between point p and the line equation of the points
float dev(Point p,Point** points, int size){
return 0;
}

// returns the deviation between point p and the line
float dev(Point p,Line l){
return 0;
}




43 changes: 43 additions & 0 deletions anomaly_detection_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

#ifndef ANOMALYDETECTORUTIL_H_
#define ANOMALYDETECTORUTIL_H_


float avg(float* x, int size);

// returns the variance of X and Y
float var(float* x, int size);

// returns the covariance of X and Y
float cov(float* x, float* y, int size);


// returns the Pearson correlation coefficient of X and Y
float pearson(float* x, float* y, int size);

class Line{
public:
float a,b;
Line():a(0),b(0){};
Line(float a, float b):a(a),b(b){}
float f(float x){
return a*x+b;
}
};

class Point{
public:
float x,y;
Point(float x, float y):x(x),y(y){}
};

// performs a linear regression and returns the line equation
Line linear_reg(Point** points, int size);

// returns the deviation between point p and the line equation of the points
float dev(Point p,Point** points, int size);

// returns the deviation between point p and the line
float dev(Point p,Line l);

#endif
6 changes: 0 additions & 6 deletions main.cpp

This file was deleted.

3 changes: 3 additions & 0 deletions timeseries.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#include "timeseries.h"


47 changes: 47 additions & 0 deletions timeseries.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <fstream>
#include <vector>
//#include <string>

#ifndef TIMESERIES_H_
#define TIMESERIES_H_

using namespace std;

class TimeSeries{

public:

TimeSeries(const char* CSVfileName){
string line;
ifstream myFile (CSVfileName);
vector<(pair<(string), (vector<float)>)> columns;
int flag = 1;
if (myFile.is_open())
{
while ( getline (myFile,line) )
{
std::istringstream ss(line);
std::string token
while(std::getline(ss, token, ',')) {
if (flag == 1) {
vector<float> v1
columns.push_bac(make_pair(token, v1));

}
else{
for(int i = 0, i < columns.size(), i++){
columns[i].second = token;
}
}
}
flag = 0;

}
myFile.close();
}
}
};



#endif /* TIMESERIES_H_ */

0 comments on commit ddb6b7b

Please sign in to comment.