-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatcpp_compute.hpp
More file actions
530 lines (425 loc) · 19.1 KB
/
statcpp_compute.hpp
File metadata and controls
530 lines (425 loc) · 19.1 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/**
* @file statcpp_compute.hpp
* @brief Pure compute functions over statcpp (database-agnostic)
*
* Ported from the `calc_*` / `wf_*` wrappers of sqlite3StatisticalLibrary. None of
* these depend on DuckDB or SQLite; they take and return `std::vector<double>`.
* The marshalling layers in statcpp_udf.hpp / statcpp_scalar.hpp call into them.
*
* Conventions
* -----------
* - Aggregates: `(const Vec&[, const Vec&][, double]) -> double`
* - Window functions: `(const Vec& values, const std::vector<bool>& nulls, int param) -> Vec`
* `values` keeps missing entries as NaN; `nulls[i]` flags a missing entry.
* - NaN / Inf results are converted to SQL NULL by the marshalling layer.
*
* For the definition, assumptions and algorithm of each statistic, see the statcpp
* documentation.
*/
#pragma once
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <limits>
#include <map>
#include <vector>
#include "statcpp/statcpp.hpp"
namespace statcpp_duckdb::compute {
/// Sample vector type passed to statcpp.
using Vec = std::vector<double>;
/// Shorthand for a NaN result (representation of invalid input).
inline double NaN() {
return std::numeric_limits<double>::quiet_NaN();
}
// ===========================================================================
// Basic aggregates: (const Vec&) -> double
// The input is already NaN-free (and sorted, where the helper requests it).
// ===========================================================================
// --- Basic statistics ---
inline double Mean(const Vec& v) { return statcpp::mean(v.begin(), v.end()); }
inline double Median(const Vec& v) {
Vec s(v);
std::sort(s.begin(), s.end());
return statcpp::median(s.begin(), s.end());
}
inline double Mode(const Vec& v) { return statcpp::mode(v.begin(), v.end()); }
inline double GeometricMean(const Vec& v) { return statcpp::geometric_mean(v.begin(), v.end()); }
inline double HarmonicMean(const Vec& v) { return statcpp::harmonic_mean(v.begin(), v.end()); }
// --- Dispersion / spread ---
inline double Range(const Vec& v) { return statcpp::range(v.begin(), v.end()); }
inline double Var(const Vec& v) {
// stat_var defaults to ddof=0 (same as the population variance).
return statcpp::var(v.begin(), v.end(), static_cast<std::size_t>(0));
}
inline double PopulationVariance(const Vec& v) { return statcpp::population_variance(v.begin(), v.end()); }
inline double SampleVariance(const Vec& v) {
if (v.size() < 2) return NaN();
return statcpp::sample_variance(v.begin(), v.end());
}
inline double Stdev(const Vec& v) {
return statcpp::stdev(v.begin(), v.end(), static_cast<std::size_t>(0));
}
inline double PopulationStddev(const Vec& v) { return statcpp::population_stddev(v.begin(), v.end()); }
inline double SampleStddev(const Vec& v) {
if (v.size() < 2) return NaN();
return statcpp::sample_stddev(v.begin(), v.end());
}
inline double Cv(const Vec& v) { return statcpp::coefficient_of_variation(v.begin(), v.end()); }
inline double Iqr(const Vec& v) {
Vec s(v);
std::sort(s.begin(), s.end());
return statcpp::iqr(s.begin(), s.end());
}
inline double MadMean(const Vec& v) { return statcpp::mean_absolute_deviation(v.begin(), v.end()); }
inline double GeometricStddev(const Vec& v) { return statcpp::geometric_stddev(v.begin(), v.end()); }
// --- Shape of distribution ---
inline double PopulationSkewness(const Vec& v) {
if (v.size() < 3) return NaN();
return statcpp::population_skewness(v.begin(), v.end());
}
inline double Skewness(const Vec& v) {
if (v.size() < 3) return NaN();
return statcpp::sample_skewness(v.begin(), v.end());
}
inline double PopulationKurtosis(const Vec& v) {
if (v.size() < 4) return NaN();
return statcpp::population_kurtosis(v.begin(), v.end());
}
inline double Kurtosis(const Vec& v) {
if (v.size() < 4) return NaN();
return statcpp::sample_kurtosis(v.begin(), v.end());
}
// --- Estimation ---
inline double Se(const Vec& v) {
if (v.size() < 2) return NaN();
return statcpp::standard_error(v.begin(), v.end());
}
// --- Robust statistics ---
inline double Mad(const Vec& v) { return statcpp::mad(v.begin(), v.end()); }
inline double MadScaled(const Vec& v) { return statcpp::mad_scaled(v.begin(), v.end()); }
inline double HodgesLehmann(const Vec& v) { return statcpp::hodges_lehmann(v.begin(), v.end()); }
// ===========================================================================
// Parameterized aggregates: (const Vec&, double) -> double
// ===========================================================================
inline double TrimmedMean(const Vec& v, double proportion) {
Vec s(v);
std::sort(s.begin(), s.end());
return statcpp::trimmed_mean(s.begin(), s.end(), proportion);
}
inline double Percentile(const Vec& v, double p) {
Vec s(v);
std::sort(s.begin(), s.end());
return statcpp::percentile(s.begin(), s.end(), p);
}
inline double MoeMean(const Vec& v, double conf) {
if (v.size() < 2) return NaN();
return statcpp::margin_of_error_mean(v.begin(), v.end(), conf);
}
inline double CohensD(const Vec& v, double mu0) {
if (v.size() < 2) return NaN();
return statcpp::cohens_d(v.begin(), v.end(), mu0);
}
inline double HedgesG(const Vec& v, double mu0) {
if (v.size() < 2) return NaN();
return statcpp::hedges_g(v.begin(), v.end(), mu0);
}
inline double AcfLag(const Vec& v, double lag_d) {
auto lag = static_cast<std::size_t>(lag_d);
if (lag >= v.size()) return NaN();
return statcpp::autocorrelation(v.begin(), v.end(), lag);
}
inline double BiweightMidvar(const Vec& v, double c) {
return statcpp::biweight_midvariance(v.begin(), v.end(), c);
}
// ===========================================================================
// Two-column aggregates: (const Vec&, const Vec&) -> double
// Inputs are already pairwise-aligned (missing pairs dropped by the marshalling layer).
// ===========================================================================
// --- Correlation / covariance ---
inline double PopulationCovariance(const Vec& x, const Vec& y) {
if (x.size() < 2) return NaN();
return statcpp::population_covariance(x.begin(), x.end(), y.begin(), y.end());
}
inline double Covariance(const Vec& x, const Vec& y) {
if (x.size() < 2) return NaN();
return statcpp::covariance(x.begin(), x.end(), y.begin(), y.end());
}
inline double PearsonR(const Vec& x, const Vec& y) {
if (x.size() < 2) return NaN();
return statcpp::pearson_correlation(x.begin(), x.end(), y.begin(), y.end());
}
inline double SpearmanR(const Vec& x, const Vec& y) {
if (x.size() < 2) return NaN();
return statcpp::spearman_correlation(x.begin(), x.end(), y.begin(), y.end());
}
inline double KendallTau(const Vec& x, const Vec& y) {
if (x.size() < 2) return NaN();
return statcpp::kendall_tau(x.begin(), x.end(), y.begin(), y.end());
}
inline double WeightedCovariance(const Vec& values, const Vec& weights) {
// With a 2-column interface this is the weighted self-covariance of the value
// column (i.e. the weighted variance).
if (values.size() < 2) return NaN();
return statcpp::weighted_covariance(values.begin(), values.end(),
values.begin(), values.end(), weights.begin());
}
// --- Weighted statistics ---
inline double WeightedMean(const Vec& values, const Vec& weights) {
return statcpp::weighted_mean(values.begin(), values.end(), weights.begin(), weights.end());
}
inline double WeightedHarmonicMean(const Vec& values, const Vec& weights) {
return statcpp::weighted_harmonic_mean(values.begin(), values.end(), weights.begin(), weights.end());
}
inline double WeightedVariance(const Vec& values, const Vec& weights) {
return statcpp::weighted_variance(values.begin(), values.end(), weights.begin(), weights.end());
}
inline double WeightedStddev(const Vec& values, const Vec& weights) {
return statcpp::weighted_stddev(values.begin(), values.end(), weights.begin(), weights.end());
}
inline double WeightedMedian(const Vec& values, const Vec& weights) {
return statcpp::weighted_median(values.begin(), values.end(), weights.begin(), weights.end());
}
inline double WeightedPercentile(const Vec& values, const Vec& weights, double p) {
return statcpp::weighted_percentile(values.begin(), values.end(),
weights.begin(), weights.end(), p);
}
// --- Regression (the double-returning ones only) ---
inline double RSquared(const Vec& actual, const Vec& predicted) {
if (actual.size() < 2) return NaN();
return statcpp::r_squared(actual.begin(), actual.end(), predicted.begin(), predicted.end());
}
inline double AdjustedRSquared(const Vec& actual, const Vec& predicted) {
if (actual.size() < 3) return NaN();
return statcpp::adjusted_r_squared(actual.begin(), actual.end(),
predicted.begin(), predicted.end(), 1);
}
// --- Error metrics ---
inline double Mae(const Vec& actual, const Vec& predicted) {
return statcpp::mae(actual.begin(), actual.end(), predicted.begin());
}
inline double Mse(const Vec& actual, const Vec& predicted) {
return statcpp::mse(actual.begin(), actual.end(), predicted.begin());
}
inline double Rmse(const Vec& actual, const Vec& predicted) {
return statcpp::rmse(actual.begin(), actual.end(), predicted.begin());
}
inline double Mape(const Vec& actual, const Vec& predicted) {
return statcpp::mape(actual.begin(), actual.end(), predicted.begin());
}
// --- Distance metrics ---
inline double EuclideanDist(const Vec& a, const Vec& b) {
return statcpp::euclidean_distance(a.begin(), a.end(), b.begin(), b.end());
}
inline double ManhattanDist(const Vec& a, const Vec& b) {
return statcpp::manhattan_distance(a.begin(), a.end(), b.begin(), b.end());
}
inline double CosineSim(const Vec& a, const Vec& b) {
return statcpp::cosine_similarity(a.begin(), a.end(), b.begin(), b.end());
}
inline double CosineDist(const Vec& a, const Vec& b) {
return statcpp::cosine_distance(a.begin(), a.end(), b.begin(), b.end());
}
inline double MinkowskiDist(const Vec& a, const Vec& b, double p) {
return statcpp::minkowski_distance(a.begin(), a.end(), b.begin(), b.end(), p);
}
inline double ChebyshevDist(const Vec& a, const Vec& b) {
return statcpp::chebyshev_distance(a.begin(), a.end(), b.begin(), b.end());
}
// --- Two-sample effect sizes (the double-returning ones) ---
inline double CohensD2(const Vec& x, const Vec& y) {
if (x.size() < 2 || y.size() < 2) return NaN();
return statcpp::cohens_d_two_sample(x.begin(), x.end(), y.begin(), y.end());
}
inline double HedgesG2(const Vec& x, const Vec& y) {
if (x.size() < 2 || y.size() < 2) return NaN();
return statcpp::hedges_g_two_sample(x.begin(), x.end(), y.begin(), y.end());
}
inline double GlassDelta(const Vec& x, const Vec& y) {
if (x.size() < 2 || y.size() < 2) return NaN();
return statcpp::glass_delta(x.begin(), x.end(), y.begin(), y.end());
}
// ===========================================================================
// Window functions: (const Vec& values, const std::vector<bool>& nulls, int param) -> Vec
// `values` keeps missing entries as NaN. The result has the same length (missing
// positions are NaN).
// ===========================================================================
/// Extract the non-missing values (for statcpp functions that do not handle NaN).
inline Vec ExtractValid(const Vec& values, const std::vector<bool>& nulls) {
Vec valid;
valid.reserve(values.size());
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) valid.push_back(values[i]);
}
return valid;
}
inline Vec WfRollingMean(const Vec& values, const std::vector<bool>& nulls, int window) {
if (window <= 0) window = 1;
auto result = statcpp::rolling_mean(values, static_cast<std::size_t>(window));
for (std::size_t i = 0; i < result.size() && i < nulls.size(); ++i) {
if (nulls[i]) result[i] = NaN();
}
return result;
}
inline Vec WfRollingStd(const Vec& values, const std::vector<bool>& nulls, int window) {
if (window <= 0) window = 1;
auto result = statcpp::rolling_std(values, static_cast<std::size_t>(window));
for (std::size_t i = 0; i < result.size() && i < nulls.size(); ++i) {
if (nulls[i]) result[i] = NaN();
}
return result;
}
inline Vec WfRollingMin(const Vec& values, const std::vector<bool>& nulls, int window) {
if (window <= 0) window = 1;
auto result = statcpp::rolling_min(values, static_cast<std::size_t>(window));
for (std::size_t i = 0; i < result.size() && i < nulls.size(); ++i) {
if (nulls[i]) result[i] = NaN();
}
return result;
}
inline Vec WfRollingMax(const Vec& values, const std::vector<bool>& nulls, int window) {
if (window <= 0) window = 1;
auto result = statcpp::rolling_max(values, static_cast<std::size_t>(window));
for (std::size_t i = 0; i < result.size() && i < nulls.size(); ++i) {
if (nulls[i]) result[i] = NaN();
}
return result;
}
inline Vec WfRollingSum(const Vec& values, const std::vector<bool>& nulls, int window) {
if (window <= 0) window = 1;
auto result = statcpp::rolling_sum(values, static_cast<std::size_t>(window));
for (std::size_t i = 0; i < result.size() && i < nulls.size(); ++i) {
if (nulls[i]) result[i] = NaN();
}
return result;
}
inline Vec WfMovingAvg(const Vec& values, const std::vector<bool>& /*nulls*/, int window) {
if (window <= 0) window = 1;
return statcpp::moving_average(values.begin(), values.end(), static_cast<std::size_t>(window));
}
inline Vec WfEma(const Vec& values, const std::vector<bool>& /*nulls*/, int span) {
if (span <= 0) span = 10;
const double alpha = 2.0 / (static_cast<double>(span) + 1.0);
return statcpp::exponential_moving_average(values.begin(), values.end(), alpha);
}
inline Vec WfRank(const Vec& values, const std::vector<bool>& nulls, int /*param*/) {
auto valid = ExtractValid(values, nulls);
if (valid.empty()) return Vec(values.size(), NaN());
auto ranks = statcpp::rank_transform(valid);
Vec result(values.size(), NaN());
std::size_t vi = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) result[i] = ranks[vi++];
}
return result;
}
inline Vec WfFillnaMean(const Vec& values, const std::vector<bool>& /*nulls*/, int /*param*/) {
return statcpp::fillna_mean(values);
}
inline Vec WfFillnaMedian(const Vec& values, const std::vector<bool>& /*nulls*/, int /*param*/) {
return statcpp::fillna_median(values);
}
inline Vec WfFillnaFfill(const Vec& values, const std::vector<bool>& /*nulls*/, int /*param*/) {
return statcpp::fillna_ffill(values);
}
inline Vec WfFillnaBfill(const Vec& values, const std::vector<bool>& /*nulls*/, int /*param*/) {
return statcpp::fillna_bfill(values);
}
inline Vec WfFillnaInterp(const Vec& values, const std::vector<bool>& /*nulls*/, int /*param*/) {
return statcpp::fillna_interpolate(values);
}
inline Vec WfLabelEncode(const Vec& values, const std::vector<bool>& nulls, int /*param*/) {
auto valid = ExtractValid(values, nulls);
if (valid.empty()) return Vec(values.size(), NaN());
auto enc = statcpp::label_encode(valid);
Vec result(values.size(), NaN());
std::size_t vi = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) result[i] = static_cast<double>(enc.encoded[vi++]);
}
return result;
}
inline Vec WfBinWidth(const Vec& values, const std::vector<bool>& nulls, int n_bins) {
if (n_bins <= 0) n_bins = 10;
auto valid = ExtractValid(values, nulls);
if (valid.empty()) return Vec(values.size(), NaN());
auto bins = statcpp::bin_equal_width(valid, static_cast<std::size_t>(n_bins));
Vec result(values.size(), NaN());
std::size_t vi = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) result[i] = static_cast<double>(bins[vi++]);
}
return result;
}
inline Vec WfBinFreq(const Vec& values, const std::vector<bool>& nulls, int n_bins) {
if (n_bins <= 0) n_bins = 10;
auto valid = ExtractValid(values, nulls);
if (valid.empty()) return Vec(values.size(), NaN());
auto bins = statcpp::bin_equal_freq(valid, static_cast<std::size_t>(n_bins));
Vec result(values.size(), NaN());
std::size_t vi = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) result[i] = static_cast<double>(bins[vi++]);
}
return result;
}
inline Vec WfLag(const Vec& values, const std::vector<bool>& /*nulls*/, int k) {
if (k <= 0) k = 1;
return statcpp::lag(values.begin(), values.end(), static_cast<std::size_t>(k));
}
inline Vec WfDiff(const Vec& values, const std::vector<bool>& /*nulls*/, int order) {
if (order <= 0) order = 1;
auto result = statcpp::diff(values.begin(), values.end(), static_cast<std::size_t>(order));
Vec padded(values.size(), NaN());
for (std::size_t i = 0; i < result.size(); ++i) {
padded[i + static_cast<std::size_t>(order)] = result[i];
}
return padded;
}
inline Vec WfSeasonalDiff(const Vec& values, const std::vector<bool>& /*nulls*/, int period) {
if (period <= 0) period = 1;
auto result = statcpp::seasonal_diff(values.begin(), values.end(), static_cast<std::size_t>(period));
Vec padded(values.size(), NaN());
for (std::size_t i = 0; i < result.size(); ++i) {
padded[i + static_cast<std::size_t>(period)] = result[i];
}
return padded;
}
/// Shared outlier helper: run a detector on the valid values and emit 1.0 (outlier) / 0.0 / NaN.
template <typename Detector>
inline Vec OutlierFlags(const Vec& values, const std::vector<bool>& nulls, Detector detect) {
auto valid = ExtractValid(values, nulls);
if (valid.empty()) return Vec(values.size(), NaN());
auto det = detect(valid.begin(), valid.end());
std::vector<bool> is_outlier(valid.size(), false);
for (auto idx : det.outlier_indices) {
if (idx < is_outlier.size()) is_outlier[idx] = true;
}
Vec result(values.size(), NaN());
std::size_t vi = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) result[i] = is_outlier[vi++] ? 1.0 : 0.0;
}
return result;
}
inline Vec WfOutliersIqr(const Vec& values, const std::vector<bool>& nulls, int /*param*/) {
return OutlierFlags(values, nulls, [](auto f, auto l) { return statcpp::detect_outliers_iqr(f, l); });
}
inline Vec WfOutliersZscore(const Vec& values, const std::vector<bool>& nulls, int /*param*/) {
return OutlierFlags(values, nulls, [](auto f, auto l) { return statcpp::detect_outliers_zscore(f, l); });
}
inline Vec WfOutliersMzscore(const Vec& values, const std::vector<bool>& nulls, int /*param*/) {
return OutlierFlags(values, nulls,
[](auto f, auto l) { return statcpp::detect_outliers_modified_zscore(f, l); });
}
inline Vec WfWinsorize(const Vec& values, const std::vector<bool>& nulls, int /*param*/) {
auto valid = ExtractValid(values, nulls);
if (valid.empty()) return Vec(values.size(), NaN());
auto wins = statcpp::winsorize(valid.begin(), valid.end());
Vec result(values.size(), NaN());
std::size_t vi = 0;
for (std::size_t i = 0; i < values.size(); ++i) {
if (!nulls[i]) result[i] = wins[vi++];
}
return result;
}
} // namespace statcpp_duckdb::compute