-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeviation.cpp
191 lines (148 loc) · 5 KB
/
deviation.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
187
188
189
190
191
#include "deviation.h"
float average(std::vector<float> &v) {
if (v.empty()) {
return 0;
}
auto const size = static_cast<float>(v.size());
return std::accumulate(v.begin(), v.end(), 0.0f) / size;
}
/* also known as Pearson coefficient
* used in this program only for linear approximation
* formula: r = (Σ((x_i - x_avg) * (y_i - y_avg))) / (sqrt(Σ(x_i - x_avg)^2) * sqrt(Σ(y_i - y_avg)^2))
*
* The correlation coefficient is used to answer the question:
* is there a linear relationship between the variables and how strong is it?
*
* r = +- 1 => strong relationship
* r = 0 => no relationship
* etc.
*/
float correlation_coefficient(std::vector<float> xs, std::vector<float> ys) {
float xAverage = average(xs);
float yAverage = average(ys);
float numerator = 0;
for (size_t i = 0; i < xs.size(); i++) {
numerator += (xs[i] - xAverage) * (ys[i] - yAverage);
}
float denominator;
float leftSum = 0;
for (size_t j = 0; j < xs.size(); j++) {
leftSum += std::pow(xs[j] - xAverage, 2);
}
float rightSum = 0;
for (size_t k = 0; k < ys.size(); k++) {
rightSum += std::pow(ys[k] - yAverage, 2);
}
denominator = std::sqrt(leftSum * rightSum);
if (denominator == 0) {
throw std::runtime_error("While processing Pearson's coefficient dominator become 0!");
} else {
return numerator / denominator;
}
}
float standard_deviation(float S, size_t n) { /* S = ∑[1, n](a*x_i + b - y_i)^2 */
return std::sqrt(S / n);
}
float deviation_lineal(float a, float b, std::vector<float> &xs, std::vector<float> &ys) {
/* φ(x) = ax + b
* this is approximating function
*
* we substitute the values from the vector xs into it,
* and then compare the resulting y using the least squares method
*
* least squares function: S = S(a, b) = ∑[1, n](ε_i^2) =
* ∑[1, n](φ(x_i) - y_i)^2 = ∑[1, n](a*x_i + b - y_i)^2 -> min
* */
auto phi_of_x = [a, b](float x) -> float {
return a * x + b;
};
float S = 0;
std::vector<float> y_phi; y_phi.reserve(xs.size());
for (float x : xs) {
y_phi.push_back(phi_of_x(x));
}
for (size_t i = 0; i < y_phi.size(); i++) {
S += std::pow(y_phi[i] - ys[i], 2);
}
return S;
}
float deviation_exponential(float a, float b, std::vector<float> &xs, std::vector<float> &ys) {
/* φ(x) = a * exp(b * x)
* this is approximating function
*
* we substitute the values from the vector xs into it,
* and then compare the resulting y using the least squares method
*
* least squares function: S = S(a, b) = ∑[1, n](ε_i^2) =
* ∑[1, n](φ(x_i) - y_i)^2 = ∑[1, n](a*x_i + b - y_i)^2 -> min
* */
auto phi_of_x = [a, b](float x) -> float {
return a * std::exp(b * x);
};
float S = 0;
std::vector<float> y_phi; y_phi.reserve(xs.size());
for (float x : xs) {
y_phi.push_back(phi_of_x(x));
}
for (size_t i = 0; i < y_phi.size(); i++) {
S += std::pow(y_phi[i] - ys[i], 2);
}
return S;
}
//TODO: add comments
float deviation_power(float a, float b, std::vector<float> &xs, std::vector<float> &ys) {
auto phi_of_x = [a, b](float x) -> float {
return a * std::pow(x, b);
};
float S = 0;
std::vector<float> y_phi; y_phi.reserve(xs.size());
for (float x : xs) {
y_phi.push_back(phi_of_x(x));
}
for (size_t i = 0; i < y_phi.size(); i++) {
S += std::pow(y_phi[i] - ys[i], 2);
}
return S;
}
float deviation_log(float a, float b, std::vector<float> &xs, std::vector<float> &ys) {
auto phi_of_x = [a, b](float x) -> float {
return a * std::log(x) + b;
};
float S = 0;
std::vector<float> y_phi; y_phi.reserve(xs.size());
for (float x : xs) {
y_phi.push_back(phi_of_x(x));
}
for (size_t i = 0; i < y_phi.size(); i++) {
S += std::pow(y_phi[i] - ys[i], 2);
}
return S;
}
float deviation_quadratic(float a_0, float a_1, float a_2, std::vector<float> &xs, std::vector<float> &ys) {
auto phi_of_x = [a_0, a_1, a_2](float x) -> float {
return a_2 * std::pow(x, 2) + a_1 * std::pow(x, 1) + a_0 * std::pow(x, 0);
};
float S = 0;
std::vector<float> y_phi; y_phi.reserve(xs.size());
for (float x : xs) {
y_phi.push_back(phi_of_x(x));
}
for (size_t i = 0; i < y_phi.size(); i++) {
S += std::pow(y_phi[i] - ys[i], 2);
}
return S;
}
float deviation_qube(float a_0, float a_1, float a_2, float a_3, std::vector<float> &xs, std::vector<float> &ys) {
auto phi_of_x = [a_0, a_1, a_2, a_3](float x) -> float {
return a_3 * std::pow(x, 3) + a_2 * std::pow(x, 2) + a_1 * std::pow(x, 1) + a_0 * std::pow(x, 0);
};
float S = 0;
std::vector<float> y_phi; y_phi.reserve(xs.size());
for (float x : xs) {
y_phi.push_back(phi_of_x(x));
}
for (size_t i = 0; i < y_phi.size(); i++) {
S += std::pow(y_phi[i] - ys[i], 2);
}
return S;
}