-
Notifications
You must be signed in to change notification settings - Fork 0
/
anomaly_detection_util.cpp
188 lines (150 loc) · 4.28 KB
/
anomaly_detection_util.cpp
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* animaly_detection_util.cpp
*
* Author: 314970054 Ariel Barmats &
* 314985474 Amiram Yasif
*/
#include <math.h>
#include <stdlib.h>
#include "anomaly_detection_util.h"
#include "minCircle.h"
#include "SimpleAnomalyDetector.h"
/***********************************************************
*
* @param x
* @param size
* @return the average of the numbers in the array
*************************************************************/
float avg(float *x, int size) {
float avg = 0;
for (int i = 0; i < size; ++i) {
avg += x[i];
};
avg /= size;
return avg;
}
/********************************************
*
* @param x
* @param size
* @return the variance of X and Y
*******************************************/
float var(float *x, int size) {
float mue = avg(x, size);
float var = 0;
for (int i = 0; i < size; ++i) {
var += pow(x[i] - mue, 2);
}
var /= size;
return var;
};
/**************************************
*
* @param x
* @param y
* @param size
* @return the covariance of X and Y
*************************************/
float cov(float *x, float *y, int size) {
//find meanX and meanY
float meanX = avg(x, size);
float meanY = avg(y, size);
float cov = 0;
//find covariance
for (int i = 0; i < size; ++i) {
cov += (x[i] - meanX) * (y[i] - meanY);
}
cov /= size;
return cov;
};
/************************************************************
*
* @param x
* @param y
* @param size
* @return the Pearson correlation coefficient of X and Y
***********************************************************/
float pearson(float *x, float *y, int size) {
float covXY = cov(x, y, size);
float varX = var(x, size);
float varY = var(y, size);
float pearson = covXY / (sqrt(varX) * sqrt(varY));
return pearson;
};
/*********************************
*
* @param points
* @param size
* @return the line equation
* performs a linear regression
***********************************/
Line linear_reg(Point **points, int size) {
float a, b, covXY, varX, meanX, meanY;
float *arrayX = new float[size];
float *arrayY = new float[size];
for (int i = 0; i < size; ++i) {
//fill the X array
arrayX[i] = points[i]->x;
//fill the Y array
arrayY[i] = points[i]->y;
}
//get means of X and Y
meanX = avg(arrayX, size);
meanY = avg(arrayY, size);
covXY = cov(arrayX, arrayY, size);
varX = var(arrayX, size);
a = covXY / varX;
b = meanY - a * meanX;
Line linear_reg = Line(a, b);
return linear_reg;
};
/*********************************************************************************
*
* @param p
* @param points
* @param size
* @return the deviation between point p and the line equation of the points
********************************************************************************/
float dev(Point p, Point **points, int size) {
return distance(p, linear_reg(points, size));
};
/************************************************************
*
* @param p - point
* @param l - line
* @return the deviation between point p and the line
***********************************************************/
float distance(Point p, Line l) {
float dev = fabsf(l.f(p.x) - p.y);
return dev;
}
/*****************************************************************
*
* @param points - the array of points
* @param numOfPoints
* @param l - line
* @return The maximum distance between one of the
* collection of points and the given line.
*****************************************************************/
float maxDev(Point **points, int numOfPoints, Line l) {
float theDev = 0;
float maxDev = 0;
for (int i = 0; i < numOfPoints; ++i) {
theDev = distance(*points[i], l);
if (theDev > maxDev) {
maxDev = theDev;
}
}
return maxDev;
}
/****************************************************************
*
* Gets 2 points and calculates the distance between them.
* @param p1 point
* @param p2 point
* @return distance [square root of |x1-x2|^2 to |y1-y2|^2]
*****************************************************************/
float distance(Point p1, Point p2) {
float dev = sqrt( pow(p1.x - p2.x,2) + pow(p1.y - p2.y,2) );
return dev;
}