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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334 | /*************************************************************************
*
* Project
* _____ _____ __ __ _____
* / ____| __ \| \/ | __ \
* ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
* / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
*| (_) | |_) | __/ | | | |__| | | | | | | |
* \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
* | |
* |_|
*
* Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
*
* This software is licensed as described in the file LICENSE, which
* you should have received as part of this distribution. The terms
* among other details are referenced in the official documentation
* seen here : https://akielaries.github.io/openGPMP/ along with
* important files seen in this project.
*
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file. As this is an Open Source effort, all implementations
* must be of the same methodology.
*
*
*
* This software is distributed on an AS IS basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied.
*
************************************************************************/
#include <algorithm>
#include <cmath>
#include <openGPMP/stats/describe.hpp>
#include <vector>
double gpmp::stats::Describe::u_stat(const std::vector<double> &sample1,
const std::vector<double> &sample2) {
double U = 0;
for (double x1 : sample1) {
for (double x2 : sample2) {
if (x1 < x2) {
U++;<--- Consider using std::count_if algorithm instead of a raw loop.
}
}
}
return U;
}
// Arithmetic Mean
double gpmp::stats::Describe::mean_arith(const std::vector<double> &data) {
double sum = 0.0;
for (const auto &value : data) {
sum += value;<--- Consider using std::accumulate algorithm instead of a raw loop.
}
return sum / static_cast<double>(data.size());
}
// Geometric Mean
double gpmp::stats::Describe::mean_geo(const std::vector<double> &data) {
double product = 1.0;
for (const auto &value : data) {
product *= value;<--- Consider using std::accumulate algorithm instead of a raw loop.
}
return std::pow(product, 1.0 / static_cast<double>(data.size()));
}
// Cubic Generalized Mean
double gpmp::stats::Describe::mean_cubic(const std::vector<double> &data,
double p) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value, p);
}
return std::pow(sum / static_cast<double>(data.size()), 1.0 / p);
}
// Power Geometric Mean
double gpmp::stats::Describe::mean_geo_pow(const std::vector<double> &data,
double p) {
double product = 1.0;
for (const auto &value : data) {
product *= std::pow(value, p);
}
return std::pow(product, 1.0 / static_cast<double>(data.size()));
}
// Harmonic Mean
double gpmp::stats::Describe::mean_harmonic(const std::vector<double> &data) {
double sum = 0.0;
for (const auto &value : data) {
sum += 1.0 / value;<--- Consider using std::accumulate algorithm instead of a raw loop.
}
return static_cast<double>(data.size()) / sum;
}
// Heronian Mean
double gpmp::stats::Describe::mean_heronian(const std::vector<double> &data) {
double product = 1.0;
for (const auto &value : data) {
product *= std::sqrt(value);
}
return std::pow(product, 2.0 / static_cast<double>(data.size()));
}
// Heinz Mean
double gpmp::stats::Describe::mean_heinz(const std::vector<double> &data) {
double sum = 0.0;
for (const auto &value : data) {
sum += value * std::log(value);
}
return std::exp(sum / static_cast<double>(data.size()));
}
// Lehmer Mean
double gpmp::stats::Describe::mean_lehmer(const std::vector<double> &data,
double p) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value, p);
}
return sum / static_cast<double>(data.size());
}
// Median
double gpmp::stats::Describe::Median(std::vector<double> data) {
std::sort(data.begin(), data.end());
size_t size = data.size();
if (size % 2 == 0) {
return (data[size / 2 - 1] + data[size / 2]) / 2.0;
} else {
return data[size / 2];
}
}
// Average Absolute Deviation
double gpmp::stats::Describe::avg_abs_dev(const std::vector<double> &data) {
double mean = mean_arith(data);
double sum = 0.0;
for (const auto &value : data) {
sum += std::abs(value - mean);
}
return sum / static_cast<double>(data.size());
}
// Coefficient of Variation
double gpmp::stats::Describe::var_coeff(const std::vector<double> &data) {
double mean = mean_arith(data);
double stddev = stdev(data, mean);
return (stddev / mean) * 100.0; // Multiply by 100 for percentage
}
// Interquartile Range
double gpmp::stats::Describe::iq_range(const std::vector<double> &data) {
std::vector<double> sortedData = data;
std::sort(sortedData.begin(), sortedData.end());
size_t size = sortedData.size();
size_t lowerIndex = size / 4;
size_t upperIndex = 3 * size / 4;
return sortedData[upperIndex] - sortedData[lowerIndex];
}
// percentile
double gpmp::stats::Describe::percentile(const std::vector<double> &data,
double percentile) {
std::vector<double> sortedData = data;
std::sort(sortedData.begin(), sortedData.end());
size_t size = sortedData.size();
size_t index = static_cast<size_t>(percentile * (size - 1));
return sortedData[index];
}
// Range
double gpmp::stats::Describe::range(const std::vector<double> &data) {
auto result = std::minmax_element(data.begin(), data.end());
return *result.second - *result.first;
}
// Standard Deviation
double gpmp::stats::Describe::stdev(const std::vector<double> &data,
double mean) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value - mean, 2.0);
}
return std::sqrt(sum / static_cast<double>(data.size()));
}
// variance
double gpmp::stats::Describe::variance(const std::vector<double> &data,
double mean) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value - mean, 2.0);
}
return sum / static_cast<double>(data.size());
}
// central limit theorem
double gpmp::stats::Describe::clt(const std::vector<double> &data,
int numSamples) {
double mean = mean_arith(data);
double stddev = stdev(data, mean);
return stddev / std::sqrt(static_cast<double>(numSamples));
}
// Kurtosis
double gpmp::stats::Describe::kurtosis(const std::vector<double> &data,
double mean) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value - mean, 4.0);
}
double var = variance(data, mean);
return sum / (data.size() * std::pow(var, 2.0)) - 3.0;
}
// l-moments (first two)
double gpmp::stats::Describe::lmoment1(const std::vector<double> &data,
double mean) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value - mean, 3.0);
}
return sum / data.size();
}
double gpmp::stats::Describe::lmoment2(const std::vector<double> &data,
double mean) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow(value - mean, 4.0);
}
return sum / data.size();
}
// skewness
double gpmp::stats::Describe::skewness(const std::vector<double> &data,
double mean,
double stddev) {
double sum = 0.0;
for (const auto &value : data) {
sum += std::pow((value - mean) / stddev, 3.0);
}
return sum / static_cast<double>(data.size());
}
std::vector<size_t>
gpmp::stats::Describe::rank_data(const std::vector<double> &data) {
std::vector<size_t> ranks(data.size());
for (size_t i = 0; i < data.size(); ++i) {
size_t rank = 1;
for (size_t j = 0; j < data.size(); ++j) {
if (j != i && data[j] < data[i]) {
rank++;
}
}
ranks[i] = rank;
}
return ranks;
}
double gpmp::stats::Describe::partial_corr(const std::vector<double> &x,
const std::vector<double> &y,
const std::vector<double> &z) {
double r_xy = ppmc(x, y);
double r_xz = ppmc(x, z);
double r_yz = ppmc(y, z);
return (r_xy - (r_xz * r_yz)) /
std::sqrt((1.0 - std::pow(r_xz, 2.0)) * (1.0 - std::pow(r_yz, 2.0)));
}
// Pearson Product-Moment Correlation
double gpmp::stats::Describe::ppmc(const std::vector<double> &x,
const std::vector<double> &y) {
double mean_x = mean_arith(x);
double mean_y = mean_arith(y);
double numerator = 0.0;
double denominator_x = 0.0;
double denominator_y = 0.0;
for (size_t i = 0; i < x.size(); ++i) {
numerator += (x[i] - mean_x) * (y[i] - mean_y);
denominator_x += std::pow(x[i] - mean_x, 2.0);
denominator_y += std::pow(y[i] - mean_y, 2.0);
}
return numerator / std::sqrt(denominator_x * denominator_y);
}
// Kendall's Tau Rank Correlation
double gpmp::stats::Describe::kendalls_tau(const std::vector<double> &x,
const std::vector<double> &y) {
size_t concordant = 0;
size_t discordant = 0;
for (size_t i = 0; i < x.size() - 1; ++i) {
for (size_t j = i + 1; j < x.size(); ++j) {
if ((x[i] < x[j] && y[i] < y[j]) || (x[i] > x[j] && y[i] > y[j])) {
concordant++;
} else if ((x[i] < x[j] && y[i] > y[j]) ||
(x[i] > x[j] && y[i] < y[j])) {
discordant++;
}
}
}
return static_cast<double>(concordant - discordant) /
std::sqrt(static_cast<double>((concordant + discordant) *
(x.size() * (x.size() - 1)) / 2));
}
// Spearman's Rank Correlation
double gpmp::stats::Describe::spearmans_rho(const std::vector<double> &x,
const std::vector<double> &y) {
std::vector<size_t> ranks_x = rank_data(x);
std::vector<size_t> ranks_y = rank_data(y);
double d_squared = 0.0;
for (size_t i = 0; i < x.size(); ++i) {
d_squared += std::pow(ranks_x[i] - ranks_y[i], 2.0);
}
return 1.0 -
(6.0 * d_squared) / (x.size() * (std::pow(x.size(), 2.0) - 1.0));
}
|