Skip to content

Commit

Permalink
Y & O
Browse files Browse the repository at this point in the history
  • Loading branch information
OrpazSond committed Nov 11, 2021
1 parent 6566b90 commit d6398e1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 62 deletions.
17 changes: 15 additions & 2 deletions MainTrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#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");
Expand Down Expand Up @@ -108,4 +108,17 @@ int main(){
cout<<"done"<<endl;
return 0;
}
}*/

int main() {
TimeSeries ts = TimeSeries("/home/orpaz/CLionProjects/ass1/ex2.csv");
for (int i = 0; i < ts.getVector().size(); i++) {
std::cout << ts.getVector()[i].first << endl;
for (int j = 0; j < ts.getVector()[i].second.size(); j++) {
std::cout << ts.getVector()[i].second[j] << endl;
}
}
};



89 changes: 63 additions & 26 deletions anomaly_detection_util.cpp
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);
}

18 changes: 9 additions & 9 deletions anomaly_detection_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ 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;
}
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){}
float x,y;
Point(float x, float y):x(x),y(y){}
};

// performs a linear regression and returns the line equation
Expand All @@ -40,4 +40,4 @@ float dev(Point p,Point** points, int size);
// returns the deviation between point p and the line
float dev(Point p,Line l);

#endif
#endif
58 changes: 33 additions & 25 deletions timeseries.h
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_ */

0 comments on commit d6398e1

Please sign in to comment.