38#include <gtest/gtest.h>
55 EXPECT_DOUBLE_EQ(
sum(v), 0.0);
58TEST(SumTest, SingleElement)
60 vector<double> v = {5.0};
61 EXPECT_DOUBLE_EQ(
sum(v), 5.0);
64TEST(SumTest, MultipleElements)
66 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
67 EXPECT_DOUBLE_EQ(
sum(v), 15.0);
70TEST(SumTest, NegativeNumbers)
72 vector<double> v = {-1.0, -2.0, 3.0};
73 EXPECT_DOUBLE_EQ(
sum(v), 0.0);
76TEST(SumTest, IntegerVector)
78 vector<int> v = {1, 2, 3, 4, 5};
79 EXPECT_EQ(
sum(v), 15);
86TEST(MeanTest, EmptyContainerThrows)
89 EXPECT_THROW(
mean(v), invalid_argument);
92TEST(MeanTest, SingleElement)
94 vector<double> v = {5.0};
95 EXPECT_DOUBLE_EQ(
mean(v), 5.0);
98TEST(MeanTest, MultipleElements)
100 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
101 EXPECT_DOUBLE_EQ(
mean(v), 3.0);
106 vector<double> v = {-10.0, 10.0};
107 EXPECT_DOUBLE_EQ(
mean(v), 0.0);
112 list<double>
l = {1.0, 2.0, 3.0};
113 EXPECT_DOUBLE_EQ(
mean(
l), 2.0);
120TEST(VarianceTest, EmptyContainerThrows)
123 EXPECT_THROW(
variance(v), invalid_argument);
126TEST(VarianceTest, SingleElementThrows)
128 vector<double> v = {5.0};
129 EXPECT_THROW(
variance(v,
false), invalid_argument);
132TEST(VarianceTest, SingleElementPopulation)
134 vector<double> v = {5.0};
135 EXPECT_DOUBLE_EQ(
variance(v,
true), 0.0);
140 vector<double> v = {0.0, 2.0};
142 EXPECT_DOUBLE_EQ(
variance(v,
false), 2.0);
145TEST(VarianceTest, SampleVsPopulation)
147 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
148 double sample_var =
variance(v,
false);
151 EXPECT_GT(sample_var, pop_var);
154TEST(VarianceTest, ConstantValues)
156 vector<double> v = {5.0, 5.0, 5.0, 5.0};
160TEST(VarianceTest, NumericalStability)
163 vector<double> v = {1e10, 1e10 + 1, 1e10 + 2};
165 EXPECT_NEAR(var, 1.0, 1e-6);
174 vector<double> v = {2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0};
177 EXPECT_NEAR(s, 2.14, 0.1);
180TEST(StddevTest, IsSquareRootOfVariance)
182 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
190TEST(MinMaxTest, EmptyContainerThrows)
193 EXPECT_THROW(
min_value(v), invalid_argument);
194 EXPECT_THROW(
max_value(v), invalid_argument);
195 EXPECT_THROW(
min_max(v), invalid_argument);
200 vector<double> v = {5.0};
203 auto [min_v, max_v] =
min_max(v);
204 EXPECT_DOUBLE_EQ(min_v, 5.0);
205 EXPECT_DOUBLE_EQ(max_v, 5.0);
208TEST(MinMaxTest, MultipleElements)
210 vector<double> v = {3.0, 1.0, 4.0, 1.0, 5.0, 9.0, 2.0};
213 auto [min_v, max_v] =
min_max(v);
214 EXPECT_DOUBLE_EQ(min_v, 1.0);
215 EXPECT_DOUBLE_EQ(max_v, 9.0);
218TEST(MinMaxTest, NegativeNumbers)
220 vector<double> v = {-5.0, -1.0, -10.0, -3.0};
229TEST(PercentileTest, EmptyContainerThrows)
232 EXPECT_THROW(
percentile(v, 50), invalid_argument);
235TEST(PercentileTest, OutOfRangeThrows)
237 vector<double> v = {1.0, 2.0, 3.0};
238 EXPECT_THROW(
percentile(v, -1), invalid_argument);
239 EXPECT_THROW(
percentile(v, 101), invalid_argument);
242TEST(PercentileTest, Percentile0)
244 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
248TEST(PercentileTest, Percentile100)
250 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
254TEST(PercentileTest, Percentile50IsMedian)
256 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
260TEST(PercentileTest, UnsortedInput)
262 vector<double> v = {5.0, 1.0, 3.0, 2.0, 4.0};
270TEST(MedianTest, EmptyContainerThrows)
273 EXPECT_THROW(
median(v), invalid_argument);
278 vector<double> v = {5.0};
279 EXPECT_DOUBLE_EQ(
median(v), 5.0);
284 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
285 EXPECT_DOUBLE_EQ(
median(v), 3.0);
290 vector<double> v = {1.0, 2.0, 3.0, 4.0};
291 EXPECT_DOUBLE_EQ(
median(v), 2.5);
296 vector<double> v = {5.0, 1.0, 3.0};
297 EXPECT_DOUBLE_EQ(
median(v), 3.0);
306 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
308 EXPECT_NEAR(q1, 3.25, 0.01);
309 EXPECT_NEAR(q2, 5.5, 0.01);
310 EXPECT_NEAR(q3, 7.75, 0.01);
315 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
317 EXPECT_NEAR(
iqr(v), q3 - q1, 0.01);
324TEST(ModeTest, EmptyContainerThrows)
327 EXPECT_THROW(
mode(v), invalid_argument);
333 EXPECT_EQ(
mode(v), 5);
338 vector<int> v = {1, 2, 3, 4, 5};
341 EXPECT_TRUE(
m >= 1
and m <= 5);
346 vector<int> v = {1, 2, 2, 3, 3, 3, 4};
347 EXPECT_EQ(
mode(v), 3);
352 vector<int> v = {1, 1, 2, 2, 3};
358 vector<int> v = {1, 2, 2, 3};
366TEST(SkewnessTest, TooFewElementsThrows)
368 vector<double> v = {1.0, 2.0};
369 EXPECT_THROW(
skewness(v), invalid_argument);
372TEST(SkewnessTest, SymmetricDistribution)
374 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
380 vector<double> v = {1.0, 1.0, 1.0, 1.0, 10.0};
386 vector<double> v = {10.0, 10.0, 10.0, 10.0, 1.0};
390TEST(SkewnessTest, ConstantValues)
392 vector<double> v = {5.0, 5.0, 5.0, 5.0};
400TEST(KurtosisTest, TooFewElementsThrows)
402 vector<double> v = {1.0, 2.0, 3.0};
403 EXPECT_THROW(
kurtosis(v), invalid_argument);
406TEST(KurtosisTest, UniformDistribution)
409 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
414TEST(KurtosisTest, ConstantValues)
416 vector<double> v = {5.0, 5.0, 5.0, 5.0, 5.0};
424TEST(CoefficientOfVariationTest, ZeroMeanThrows)
426 vector<double> v = {-1.0, 1.0};
430TEST(CoefficientOfVariationTest, Basic)
432 vector<double> v = {10.0, 10.0, 10.0, 10.0};
436TEST(CoefficientOfVariationTest, PositiveValue)
438 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
447TEST(CovarianceTest, DifferentSizesThrows)
449 vector<double> x = {1.0, 2.0, 3.0};
450 vector<double>
y = {1.0, 2.0};
454TEST(CovarianceTest, TooFewElementsThrows)
456 vector<double> x = {1.0};
457 vector<double>
y = {1.0};
458 EXPECT_THROW(
covariance(x,
y,
false), invalid_argument);
461TEST(CovarianceTest, PerfectPositive)
463 vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0};
464 vector<double>
y = {1.0, 2.0, 3.0, 4.0, 5.0};
468TEST(CovarianceTest, PerfectNegative)
470 vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0};
471 vector<double>
y = {5.0, 4.0, 3.0, 2.0, 1.0};
475TEST(CovarianceTest, NoCorrelation)
477 vector<double> x = {1.0, 2.0, 3.0};
478 vector<double>
y = {2.0, 2.0, 2.0};
486TEST(CorrelationTest, PerfectPositive)
488 vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0};
489 vector<double>
y = {2.0, 4.0, 6.0, 8.0, 10.0};
493TEST(CorrelationTest, PerfectNegative)
495 vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0};
496 vector<double>
y = {10.0, 8.0, 6.0, 4.0, 2.0};
500TEST(CorrelationTest, ZeroVarianceThrows)
502 vector<double> x = {1.0, 1.0, 1.0};
503 vector<double>
y = {1.0, 2.0, 3.0};
507TEST(CorrelationTest, PartialCorrelation)
509 vector<double> x = {1.0, 2.0, 3.0, 4.0, 5.0};
510 vector<double>
y = {1.0, 2.0, 1.5, 3.5, 5.0};
520TEST(HistogramTest, ZeroBinsThrows)
522 vector<double> v = {1.0, 2.0, 3.0};
523 EXPECT_THROW(
histogram(v, 0), invalid_argument);
526TEST(HistogramTest, EmptyContainerThrows)
529 EXPECT_THROW(
histogram(v, 5), invalid_argument);
534 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
536 EXPECT_EQ(
h.size(), 1u);
537 EXPECT_EQ(
h[0].second, 5u);
540TEST(HistogramTest, MultipleBins)
542 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
544 EXPECT_EQ(
h.size(), 5u);
547 for (
const auto & [center,
count] :
h)
549 EXPECT_EQ(total, 5u);
552TEST(HistogramTest, ConstantValues)
554 vector<double> v = {5.0, 5.0, 5.0, 5.0};
557 EXPECT_EQ(
h.size(), 1u);
558 EXPECT_EQ(
h[0].second, 4u);
565TEST(StatsTest, DefaultConstruction)
568 EXPECT_EQ(s.
count, 0u);
577 EXPECT_DOUBLE_EQ(s.
range(), 9.0);
584TEST(ComputeAllStatsTest, EmptyContainer)
588 EXPECT_EQ(s.count, 0u);
589 EXPECT_FALSE(s.is_valid());
592TEST(ComputeAllStatsTest, SingleElement)
594 vector<double> v = {5.0};
596 EXPECT_EQ(s.count, 1u);
597 EXPECT_TRUE(s.is_valid());
598 EXPECT_DOUBLE_EQ(s.mean, 5.0);
599 EXPECT_DOUBLE_EQ(s.sum, 5.0);
600 EXPECT_DOUBLE_EQ(s.min, 5.0);
601 EXPECT_DOUBLE_EQ(s.max, 5.0);
602 EXPECT_DOUBLE_EQ(s.median, 5.0);
605TEST(ComputeAllStatsTest, MultipleElements)
607 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0};
610 EXPECT_EQ(s.count, 5u);
611 EXPECT_DOUBLE_EQ(s.sum, 15.0);
612 EXPECT_DOUBLE_EQ(s.mean, 3.0);
613 EXPECT_DOUBLE_EQ(s.min, 1.0);
614 EXPECT_DOUBLE_EQ(s.max, 5.0);
615 EXPECT_DOUBLE_EQ(s.median, 3.0);
616 EXPECT_GT(s.variance, 0.0);
617 EXPECT_GT(s.stddev, 0.0);
618 EXPECT_DOUBLE_EQ(s.range(), 4.0);
621TEST(ComputeAllStatsTest, QuartilesComputed)
623 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
626 EXPECT_LT(s.q1, s.median);
627 EXPECT_LT(s.median, s.q3);
628 EXPECT_NEAR(s.iqr, s.q3 - s.q1, 0.001);
631TEST(ComputeAllStatsTest, HigherMomentsComputed)
633 vector<double> v = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
637 EXPECT_NEAR(s.skewness, 0.0, 0.1);
640 EXPECT_LT(s.kurtosis, 0.0);
647TEST(LegacyComputeStatsTest, ArrayVersion)
649 double data[] = {5.0, 1.0, 3.0, 2.0, 4.0};
650 double avg, var, med, min_val, max_val;
654 EXPECT_DOUBLE_EQ(avg, 3.0);
655 EXPECT_DOUBLE_EQ(med, 3.0);
656 EXPECT_DOUBLE_EQ(min_val, 1.0);
657 EXPECT_DOUBLE_EQ(max_val, 5.0);
661TEST(LegacyComputeStatsTest, ArrayWithOffset)
663 double data[] = {100.0, 1.0, 2.0, 3.0, 100.0};
664 double avg, var, med, min_val, max_val;
669 EXPECT_DOUBLE_EQ(avg, 2.0);
670 EXPECT_DOUBLE_EQ(med, 2.0);
671 EXPECT_DOUBLE_EQ(min_val, 1.0);
672 EXPECT_DOUBLE_EQ(max_val, 3.0);
675TEST(LegacyComputeStatsTest, ContainerVersion)
677 vector<double> v = {5.0, 1.0, 3.0, 2.0, 4.0};
678 double avg, var, med, min_val, max_val;
682 EXPECT_DOUBLE_EQ(avg, 3.0);
683 EXPECT_DOUBLE_EQ(med, 3.0);
684 EXPECT_DOUBLE_EQ(min_val, 1.0);
685 EXPECT_DOUBLE_EQ(max_val, 5.0);
688TEST(LegacyComputeStatsTest, EvenCount)
690 double data[] = {1.0, 2.0, 3.0, 4.0};
691 double avg, var, med, min_val, max_val;
695 EXPECT_DOUBLE_EQ(med, 2.5);
698TEST(LegacyComputeStatsTest, EmptyRange)
700 double data[] = {1.0, 2.0, 3.0};
701 double avg, var, med, min_val, max_val;
706 EXPECT_DOUBLE_EQ(avg, 0.0);
707 EXPECT_DOUBLE_EQ(var, 0.0);
708 EXPECT_DOUBLE_EQ(med, 0.0);
717 vector<double> v = {1e15, 1e15 + 1, 1e15 + 2, 1e15 + 3, 1e15 + 4};
721 EXPECT_NEAR(s.mean, 1e15 + 2, 1.0);
723 EXPECT_NEAR(s.variance, 2.5, 0.1);
728 vector<double> v = {1e-15, 2e-15, 3e-15, 4e-15, 5e-15};
731 EXPECT_NEAR(s.mean, 3e-15, 1e-16);
732 EXPECT_GT(s.variance, 0.0);
737 vector<double> v = {-100.0, -50.0, 0.0, 50.0, 100.0};
740 EXPECT_DOUBLE_EQ(s.mean, 0.0);
741 EXPECT_DOUBLE_EQ(s.median, 0.0);
748TEST(ContainerTest, WorksWithArray)
750 array<double, 5> a = {1.0, 2.0, 3.0, 4.0, 5.0};
751 EXPECT_DOUBLE_EQ(
mean(a), 3.0);
752 EXPECT_DOUBLE_EQ(
median(a), 3.0);
755TEST(ContainerTest, WorksWithList)
757 list<double>
l = {1.0, 2.0, 3.0, 4.0, 5.0};
758 EXPECT_DOUBLE_EQ(
mean(
l), 3.0);
759 EXPECT_DOUBLE_EQ(
median(
l), 3.0);
762TEST(ContainerTest, WorksWithDynArray)
771 EXPECT_DOUBLE_EQ(
mean(a), 3.0);
773 EXPECT_EQ(s.count, 5u);
782 vector<int> v = {1, 2, 3, 4, 5};
783 EXPECT_EQ(
sum(v), 15);
784 EXPECT_EQ(
mode(v), 1);
789 vector<float> v = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f};
790 EXPECT_FLOAT_EQ(
mean(v), 3.0f);
795 ::testing::InitGoogleTest(&argc, argv);
796 return RUN_ALL_TESTS();
T & append()
Allocate a new entry to the end of array.
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
auto percentile(const Container &data, double p) -> std::decay_t< decltype(*std::begin(data))>
Compute a percentile value.
auto histogram(const Container &data, size_t num_bins) -> std::vector< std::pair< std::decay_t< decltype(*std::begin(data))>, size_t > >
Compute a histogram of the data.
auto variance(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute variance using Welford's numerically stable algorithm.
auto kurtosis(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute excess kurtosis (measure of tailedness).
const T * median(const T &a, const T &b, const T &c, const Compare &cmp=Compare())
Return a pointer to the median value among three elements.
auto covariance(const Container1 &x, const Container2 &y, bool population=false) -> std::decay_t< decltype(*std::begin(x))>
Compute covariance between two datasets.
auto stddev(const Container &data, bool population=false) -> std::decay_t< decltype(*std::begin(data))>
Compute standard deviation.
and
Check uniqueness with explicit hash + equality functors.
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
auto min_max(const Container &data) -> std::pair< std::decay_t< decltype(*std::begin(data))>, std::decay_t< decltype(*std::begin(data))> >
Compute minimum and maximum values in one pass.
auto min_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute minimum value.
auto skewness(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute skewness (measure of asymmetry).
auto iqr(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the interquartile range (IQR = Q3 - Q1).
auto compute_all_stats(const Container &data) -> Stats< std::decay_t< decltype(*std::begin(data))> >
Compute all statistics for a dataset.
auto correlation(const Container1 &x, const Container2 &y) -> std::decay_t< decltype(*std::begin(x))>
Compute Pearson correlation coefficient.
bool is_multimodal(const Container &data)
Check if data is multimodal.
auto coefficient_of_variation(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute coefficient of variation (CV = stddev / mean).
auto mode(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the mode (most frequent value).
auto quartiles(const Container &data) -> std::tuple< std::decay_t< decltype(*std::begin(data))>, std::decay_t< decltype(*std::begin(data))>, std::decay_t< decltype(*std::begin(data))> >
Compute quartiles (Q1, Q2, Q3).
auto max_value(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute maximum value.
void compute_stats(T *data, int l, int r, T &avg, T &var, T &med, T &_min, T &_max)
Compute basic descriptive statistics for an array range.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Comprehensive statistical utilities for numeric data.
Container for comprehensive statistical results.
size_t count
Number of elements.
bool is_valid() const noexcept
Check if statistics are valid.
T range() const noexcept
Get the range (max - min).
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)