-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
120 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,85 @@ | ||
/* | ||
* animaly_detection_util.cpp | ||
* | ||
* Author: write your ID and name here | ||
*/ | ||
// | ||
// Created by orpaz on 10/17/21.fdh | ||
// | ||
|
||
#include <math.h> | ||
#include "anomaly_detection_util.h" | ||
|
||
float avg(float* x, int size){ | ||
return 0; | ||
float mu = 0; | ||
for (int i = 0; i < size; ++i) { | ||
mu = mu + x[i]; | ||
} | ||
return mu / size; | ||
} | ||
|
||
// returns the variance of X and Y | ||
float var(float* x, int size){ | ||
return 0; | ||
float sum = 0, result = 0; | ||
float m = pow(avg(x, size), 2); | ||
for (int i = 0; i < size; ++i) { | ||
sum = sum + pow(x[i], 2); | ||
} | ||
result = (sum / size) - m ; | ||
return result; | ||
} | ||
|
||
// returns the covariance of X and Y | ||
float cov(float* x, float* y, int size){ | ||
return 0; | ||
float sum = 0; | ||
float muX = avg(x, size); | ||
float muY = avg(y, size); | ||
for (int i = 0; i < size; ++i) { | ||
sum = sum + ((x[i] - muX) * (y[i] - muY)); | ||
} | ||
sum = sum / size; | ||
return sum; | ||
} | ||
|
||
|
||
// returns the Pearson correlation coefficient of X and Y | ||
float pearson(float* x, float* y, int size){ | ||
return 0; | ||
float numerator = cov(x, y, size); | ||
float denominator = sqrt(var(x, size)) * sqrt(var(y, size)); | ||
return numerator / denominator; | ||
} | ||
|
||
// performs a linear regression and returns the line equation | ||
Line linear_reg(Point** points, int size){ | ||
|
||
return Line(0,0); | ||
} | ||
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){} | ||
}; | ||
|
||
// returns the deviation between point p and the line equation of the points | ||
float dev(Point p,Point** points, int size){ | ||
return 0; | ||
Line linear_reg(Point** points, int size) { | ||
float x[size]; | ||
float y[size]; | ||
for (int i = 0; i < size; ++i) { | ||
x[i] = points[i]->x; | ||
y[i] = points[i]->y; | ||
} | ||
float a, b; | ||
a = cov(x, y, size) / var(x, size); | ||
float muX = avg(x, size); | ||
float muY = avg(y, size); | ||
b = muY - (a * muX); | ||
Line line (a, b); | ||
return line; | ||
} | ||
|
||
// returns the deviation between point p and the line | ||
float dev(Point p,Line l){ | ||
return 0; | ||
float lineY = l.f(p.x); | ||
float dev = lineY - p.y; | ||
if (dev < 0) | ||
return -dev; | ||
return dev; | ||
} | ||
|
||
|
||
|
||
float dev(Point p,Point** points, int size){ | ||
Line line = linear_reg(points, size); | ||
return dev(p, line); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,64 @@ | ||
#include <fstream> | ||
#include <vector> | ||
//#include <string> | ||
#include <iostream> | ||
#include <fstream> | ||
#include <sstream> | ||
#ifndef TIMESERIES_H_ | ||
#define TIMESERIES_H_ | ||
|
||
using namespace std; | ||
|
||
class TimeSeries{ | ||
class TimeSeries { | ||
private: | ||
vector<pair<string, vector<float>>> columns; | ||
|
||
|
||
public: | ||
|
||
TimeSeries(const char* CSVfileName){ | ||
TimeSeries(const char *CSVfileName) { | ||
|
||
|
||
string line; | ||
ifstream myFile (CSVfileName); | ||
vector<pair<string, vector<float>>> columns; | ||
int flag = 1; | ||
if (myFile.is_open()) | ||
{ | ||
getline (myFile,line); | ||
|
||
std::ifstream myFile(CSVfileName); | ||
|
||
if (myFile.is_open()) { | ||
getline(myFile, line, '\r'); | ||
std::istringstream ss(line); | ||
std::string token; | ||
while (std::getline(ss, token, ',')) { | ||
vector<float> v1; | ||
columns.push_back(make_pair(token, v1)); | ||
} | ||
|
||
while (std::getline (myFile,line)){ | ||
std::if ss(line); | ||
while (std::getline(myFile, line, '\r')) { | ||
std::istringstream ss(line); | ||
std::string token; | ||
if (flag == 1) { | ||
|
||
} | ||
int index = 0; | ||
while (std::getline(ss, token, ',')) { | ||
float num = std::stof(token); | ||
columns[index].second.push_back(num); | ||
} | ||
} | ||
|
||
|
||
/*else{ | ||
for(int i = 0; i < columns.size(); i++){ | ||
v1.push_back(); | ||
|
||
} | ||
columns[j].second = v1; | ||
} | ||
}*/ | ||
flag = 0; | ||
/*else{ | ||
for(int i = 0; i < columns.size(); i++){ | ||
v1.push_back(); | ||
} | ||
columns[j].second = v1; | ||
} | ||
myFile.close(); | ||
}*/ | ||
|
||
} | ||
myFile.close(); | ||
} | ||
}; | ||
|
||
|
||
|
||
vector<pair<string, vector<float>>> getVector() { | ||
return columns; | ||
}; | ||
}; | ||
#endif /* TIMESERIES_H_ */ |