From ddb6b7b218968d11198bf0806fcb1b9a5ff6bed8 Mon Sep 17 00:00:00 2001 From: Orpaz Sondhelm Date: Wed, 10 Nov 2021 04:16:54 -0800 Subject: [PATCH] start --- .idea/vcs.xml | 6 ++ AnomalyDetector.h | 27 +++++++++ MainTrain.cpp | 111 +++++++++++++++++++++++++++++++++++++ SimpleAnomalyDetector.cpp | 21 +++++++ SimpleAnomalyDetector.h | 38 +++++++++++++ anomaly_detection_util.cpp | 48 ++++++++++++++++ anomaly_detection_util.h | 43 ++++++++++++++ main.cpp | 6 -- timeseries.cpp | 3 + timeseries.h | 47 ++++++++++++++++ 10 files changed, 344 insertions(+), 6 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 AnomalyDetector.h create mode 100644 MainTrain.cpp create mode 100644 SimpleAnomalyDetector.cpp create mode 100644 SimpleAnomalyDetector.h create mode 100644 anomaly_detection_util.cpp create mode 100644 anomaly_detection_util.h delete mode 100644 main.cpp create mode 100644 timeseries.cpp create mode 100644 timeseries.h diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/AnomalyDetector.h b/AnomalyDetector.h new file mode 100644 index 0000000..c237f2b --- /dev/null +++ b/AnomalyDetector.h @@ -0,0 +1,27 @@ + + +#ifndef ANOMALYDETECTOR_H_ +#define ANOMALYDETECTOR_H_ + +#include +#include +#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 detect(const TimeSeries& ts)=0; + virtual ~TimeSeriesAnomalyDetector(){} +}; + +#endif /* ANOMALYDETECTOR_H_ */ diff --git a/MainTrain.cpp b/MainTrain.cpp new file mode 100644 index 0000000..1572996 --- /dev/null +++ b/MainTrain.cpp @@ -0,0 +1,111 @@ + + +#include +#include +#include "AnomalyDetector.h" +#include "SimpleAnomalyDetector.h" +#include +#include /* srand, rand */ +#include /* time */ +#include + +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"<a+0.5f) + cout<b+0.5f) + cout<0.3) + cout< cf=ad.getNormalModel(); + + if(cf.size()!=2) + cout<<"wrong size of correlated features (-40)"< 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)"<0) + cout<<"you have "< SimpleAnomalyDetector::detect(const TimeSeries& ts){ + // TODO Auto-generated destructor stub +} + diff --git a/SimpleAnomalyDetector.h b/SimpleAnomalyDetector.h new file mode 100644 index 0000000..ba5879f --- /dev/null +++ b/SimpleAnomalyDetector.h @@ -0,0 +1,38 @@ + + +#ifndef SIMPLEANOMALYDETECTOR_H_ +#define SIMPLEANOMALYDETECTOR_H_ + +#include "anomaly_detection_util.h" +#include "AnomalyDetector.h" +#include +#include +#include +#include + +struct correlatedFeatures{ + string feature1,feature2; // names of the correlated features + float corrlation; + Line lin_reg; + float threshold; +}; + + +class SimpleAnomalyDetector:public TimeSeriesAnomalyDetector{ + vector cf; +public: + SimpleAnomalyDetector(); + virtual ~SimpleAnomalyDetector(); + + virtual void learnNormal(const TimeSeries& ts); + virtual vector detect(const TimeSeries& ts); + + vector getNormalModel(){ + return cf; + } + +}; + + + +#endif /* SIMPLEANOMALYDETECTOR_H_ */ diff --git a/anomaly_detection_util.cpp b/anomaly_detection_util.cpp new file mode 100644 index 0000000..ebe8c94 --- /dev/null +++ b/anomaly_detection_util.cpp @@ -0,0 +1,48 @@ +/* + * animaly_detection_util.cpp + * + * Author: write your ID and name here + */ + +#include +#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; +} + + + + diff --git a/anomaly_detection_util.h b/anomaly_detection_util.h new file mode 100644 index 0000000..aabe991 --- /dev/null +++ b/anomaly_detection_util.h @@ -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 diff --git a/main.cpp b/main.cpp deleted file mode 100644 index bc8f460..0000000 --- a/main.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() { - std::cout << "Hello, World!" << std::endl; - return 0; -} diff --git a/timeseries.cpp b/timeseries.cpp new file mode 100644 index 0000000..59275f2 --- /dev/null +++ b/timeseries.cpp @@ -0,0 +1,3 @@ +#include "timeseries.h" + + diff --git a/timeseries.h b/timeseries.h new file mode 100644 index 0000000..a70e84f --- /dev/null +++ b/timeseries.h @@ -0,0 +1,47 @@ +#include +#include +//#include + +#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)> 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 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_ */