From 44d30a6f8db89b22773651c5d1a3e3a7638e9fbf Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 2 Jun 2024 15:46:56 +0300 Subject: [PATCH] Add fake percentile computation in util::statistics_lite based on normal distribution --- libs/util/include/psemek/util/statistics.hpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/libs/util/include/psemek/util/statistics.hpp b/libs/util/include/psemek/util/statistics.hpp index 014870e4..ab2a964e 100644 --- a/libs/util/include/psemek/util/statistics.hpp +++ b/libs/util/include/psemek/util/statistics.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include #include @@ -52,6 +54,8 @@ namespace psemek::util T min() const { return min_; } T max() const { return max_; } + T percentile(double p) const; + template friend statistics_lite merge(statistics_lite const & s1, statistics_lite const & s2); @@ -93,6 +97,15 @@ namespace psemek::util return os; } + template + T statistics_lite::percentile(double p) const + { + // Assume normal distribution + // TODO: use a better distribution, maybe maximizing entropy on [0, +inf) + // See https://en.wikipedia.org/wiki/Differential_entropy#Alternative_proof + return boost::math::erf_inv(2.0 * p - 1) * var() * std::sqrt(2.0) + mean(); + } + template statistics_lite merge(statistics_lite const & s1, statistics_lite const & s2) {