171template <
typename Container>
172[[nodiscard]]
auto sum(
const Container &data) -> std::decay_t<
decltype(*std::begin(data))>
174 using T = std::decay_t<
decltype(*std::begin(data))>;
176 for (
const auto &x : data)
189template <
typename Container>
190[[nodiscard]]
auto mean(
const Container &data) -> std::decay_t<
decltype(*std::begin(data))>
192 using T = std::decay_t<
decltype(*std::begin(data))>;
195 for (
const auto &x : data)
201 return s /
static_cast<T>(n);
219template <
typename Container>
221 bool population =
false) -> std::decay_t<
decltype(*std::begin(data))>
223 using T = std::decay_t<
decltype(*std::begin(data))>;
230 for (
const auto &x : data)
234 m += delta /
static_cast<T>(n);
236 m2 += delta * delta2;
242 return m2 /
static_cast<T>(n);
245 return m2 /
static_cast<T>(n - 1);
256template <
typename Container>
258 bool population =
false) -> std::decay_t<
decltype(*std::begin(data))>
260 return std::sqrt(
variance(data, population));
271template <
typename Container>
274 auto it = std::begin(data);
275 auto end = std::end(data);
279 for (++it; it != end; ++it)
293template <
typename Container>
296 auto it = std::begin(data);
297 auto end = std::end(data);
301 for (++it; it != end; ++it)
315template <
typename Container>
317 -> std::pair<std::decay_t<
decltype(*std::begin(data))>, std::decay_t<
decltype(*std::begin(data))>>
319 using T = std::decay_t<
decltype(*std::begin(data))>;
320 auto it = std::begin(data);
321 auto end = std::end(data);
326 for (++it; it != end; ++it)
333 return {min_val, max_val};
351template <
typename Container>
353 double p) -> std::decay_t<
decltype(*std::begin(data))>
355 using T = std::decay_t<
decltype(*std::begin(data))>;
360 for (
const auto &x : data)
369 return sorted[sorted.
size() - 1];
372 const double index = (p / 100.0) * (sorted.
size() - 1);
373 auto lower =
static_cast<size_t>(std::floor(index));
374 auto upper =
static_cast<size_t>(std::ceil(index));
377 return sorted[lower];
380 double fraction = index - lower;
381 return static_cast<T>(sorted[lower] * (1 - fraction) + sorted[upper] * fraction);
392template <
typename Container>
393[[nodiscard]]
auto median(
const Container &data) -> std::decay_t<
decltype(*std::begin(data))>
405template <
typename Container>
407 -> std::tuple<std::decay_t<
decltype(*std::begin(data))>,
408 std::decay_t<
decltype(*std::begin(data))>,
409 std::decay_t<
decltype(*std::begin(data))>>
411 using T = std::decay_t<
decltype(*std::begin(data))>;
425template <
typename Container>
426[[nodiscard]]
auto iqr(
const Container &data) -> std::decay_t<
decltype(*std::begin(data))>
446template <
typename Container>
447[[nodiscard]]
auto mode(
const Container &data) -> std::decay_t<
decltype(*std::begin(data))>
449 using T = std::decay_t<
decltype(*std::begin(data))>;
450 auto it = std::begin(data);
451 auto end = std::end(data);
456 for (
const auto &x : data)
459 T mode_val = freq.
begin()->first;
460 size_t max_count = freq.
begin()->second;
462 for (
const auto &[val,
count] : freq)
463 if (
count > max_count)
479template <
typename Container>
482 using T = std::decay_t<
decltype(*std::begin(data))>;
485 for (
const auto &x : data)
491 size_t max_count = 0;
492 for (
const auto &[val,
count] : freq)
493 if (
count > max_count)
497 for (
const auto &[val,
count] : freq)
498 if (
count == max_count)
522template <
typename Container>
525 using T = std::decay_t<
decltype(*std::begin(data))>;
535 for (
const auto &x : data)
545 T factor =
static_cast<T>(n) / ((n - 1) * (n - 2));
546 return factor * sum3;
563template <
typename Container>
566 using T = std::decay_t<
decltype(*std::begin(data))>;
576 for (
const auto &x : data)
580 sum4 += diff2 * diff2;
587 T n_t =
static_cast<T>(n);
588 T factor1 = (n_t * (n_t + 1)) / ((n_t - 1) * (n_t - 2) * (n_t - 3));
589 T factor2 = (3 * (n_t - 1) * (n_t - 1)) / ((n_t - 2) * (n_t - 3));
591 return factor1 * sum4 - factor2;
604template <
typename Container>
606 -> std::decay_t<
decltype(*std::begin(data))>
608 using T = std::decay_t<
decltype(*std::begin(data))>;
611 return stddev(data) / std::abs(
m);
629template <
typename Container1,
typename Container2>
632 bool population =
false) -> std::decay_t<
decltype(*std::begin(x))>
634 using T = std::decay_t<
decltype(*std::begin(x))>;
636 auto it_x = std::begin(x);
637 auto it_y = std::begin(
y);
638 auto end_x = std::end(x);
639 auto end_y = std::end(
y);
647 while (it_x != end_x
and it_y != end_y)
650 T dx = *it_x - mean_x;
651 mean_x += dx /
static_cast<T>(n);
652 T dy = *it_y - mean_y;
653 mean_y += dy /
static_cast<T>(n);
654 c += dx * (*it_y - mean_y);
661 <<
"covariance: containers have different sizes";
666 return c /
static_cast<T>(n);
669 return c /
static_cast<T>(n - 1);
687template <
typename Container1,
typename Container2>
689 const Container2 &
y) -> std::decay_t<
decltype(*std::begin(x))>
691 using T = std::decay_t<
decltype(*std::begin(x))>;
698 <<
"correlation: one or both datasets have zero variance";
700 return cov / (sx * sy);
716template <
typename Container>
718 -> std::vector<std::pair<std::decay_t<
decltype(*std::begin(data))>,
size_t>>
720 using T = std::decay_t<
decltype(*std::begin(data))>;
724 auto [min_val, max_val] =
min_max(data);
725 T range = max_val - min_val;
730 return {{min_val, std::distance(std::begin(data), std::end(data))}};
733 T bin_width =
range /
static_cast<T>(num_bins);
735 std::vector<size_t> counts(num_bins, 0);
737 for (
const auto &x : data)
739 auto bin =
static_cast<size_t>((x - min_val) / bin_width);
745 std::vector<std::pair<T, size_t>> result;
746 result.reserve(num_bins);
748 for (
size_t i = 0; i < num_bins; ++i)
750 T center = min_val + (i + 0.5) * bin_width;
751 result.emplace_back(center, counts[i]);
768template <
typename Container>
770 ->
Stats<std::decay_t<
decltype(*std::begin(data))>>
772 using T = std::decay_t<
decltype(*std::begin(data))>;
776 for (
const auto &x : data)
785 s.mean = s.sum /
static_cast<T>(s.count);
788 auto [min_val, max_val] =
min_max(data);
796 s.stddev = std::sqrt(s.variance);
800 s.coef_variation = s.stddev / std::abs(s.mean);
851 avg = var = med = _min = _max =
T();
865 med = (data[mid - 1] + data[mid]) / 2;
872 for (
int i =
l; i <=
r; ++i)
875 T delta = data[i] -
m;
876 m += delta /
static_cast<T>(
k);
877 T delta2 = data[i] -
m;
878 m2 += delta * delta2;
882 var = (n > 1) ? m2 /
static_cast<T>(n - 1) :
T();
898template <
typename Container>
900 std::decay_t<
decltype(*std::begin(data))> &avg,
901 std::decay_t<
decltype(*std::begin(data))> &var,
902 std::decay_t<
decltype(*std::begin(data))> &med,
903 std::decay_t<
decltype(*std::begin(data))> &_min,
904 std::decay_t<
decltype(*std::begin(data))> &_max)
906 using T = std::decay_t<
decltype(*std::begin(data))>;
909 for (
const auto &x : data)
911 if (sorted.is_empty())
913 avg = var = med = _min = _max =
T();
920 _max = sorted[sorted.size() - 1];
922 const size_t n = sorted.size();
923 const size_t mid = n / 2;
925 med = (sorted[mid - 1] + sorted[mid]) / 2;
933 for (
size_t si = 0; si < n; ++si)
935 const auto &x = sorted[si];
938 m += delta /
static_cast<T>(
k);
940 m2 += delta * delta2;
944 var = (n > 1) ? m2 /
static_cast<T>(n - 1) :
T();
Exception handling system with formatted messages for Aleph-w.
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
High-level sorting functions for Aleph containers.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
T & append(const T &data)
Append a copy of data
Generic key-value map implemented on top of a binary search tree.
iterator begin() noexcept
Return an STL-compatible iterator to the first element.
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.
std::decay_t< typename HeadC::Item_Type > T
DynArray< T > & in_place_sort(DynArray< T > &c, Cmp cmp=Cmp())
Sorts a DynArray in place.
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.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
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.
Container< T > range(const T start, const T end, const T step=1)
Generate a range of values [start, end] with a given step.
void introsort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using introsort (introspective sort).
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.
Container for comprehensive statistical results.
T q1
First quartile (25th percentile)
size_t count
Number of elements.
T skewness
Skewness (asymmetry)
T q3
Third quartile (75th percentile)
T variance
Sample variance.
T coef_variation
Coefficient of variation (stddev/mean)
T median
Median (50th percentile)
bool is_valid() const noexcept
Check if statistics are valid.
T iqr
Interquartile range (Q3 - Q1)
T range() const noexcept
Get the range (max - min).
T kurtosis
Excess kurtosis (tailedness)
T stddev
Standard deviation.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Lazy and scalable dynamic array implementation.
Dynamic key-value map based on balanced binary search trees.
Comprehensive sorting algorithms and search utilities for Aleph-w.