Add fake percentile computation in util::statistics_lite based on normal distribution

This commit is contained in:
Nikita Lisitsa 2024-06-02 15:46:56 +03:00
parent 100e2d6af8
commit 44d30a6f8d

View file

@ -1,5 +1,7 @@
#pragma once
#include <boost/math/special_functions/erf.hpp>
#include <iostream>
#include <cmath>
#include <limits>
@ -52,6 +54,8 @@ namespace psemek::util
T min() const { return min_; }
T max() const { return max_; }
T percentile(double p) const;
template <typename H>
friend statistics_lite<H> merge(statistics_lite<H> const & s1, statistics_lite<H> const & s2);
@ -93,6 +97,15 @@ namespace psemek::util
return os;
}
template <typename T>
T statistics_lite<T>::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 <typename T>
statistics_lite<T> merge(statistics_lite<T> const & s1, statistics_lite<T> const & s2)
{