ClickHouse/src/Common/HyperLogLogBiasEstimator.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.6 KiB
C++
Raw Normal View History

2015-10-05 01:11:12 +00:00
#pragma once
#include <Common/Exception.h>
2015-10-05 01:11:12 +00:00
#include <algorithm>
#include <limits>
#include <tuple>
#include <type_traits>
2017-05-07 20:25:26 +00:00
/** This class provides a way to evaluate the error in the result of applying the HyperLogLog algorithm.
* Empirical observations show that large errors occur at E < 5 * 2^precision, where
* E is the return value of the HyperLogLog algorithm, and `precision` is the HyperLogLog precision parameter.
* See "HyperLogLog in Practice: Algorithmic Engineering of a State of the Art Cardinality Estimation Algorithm".
2015-10-05 01:11:12 +00:00
* (S. Heule et al., Proceedings of the EDBT 2013 Conference).
*/
template <typename BiasData>
class HyperLogLogBiasEstimator
{
public:
static constexpr bool isTrivial()
{
return false;
}
/// Maximum number of unique values to which the correction should apply
2017-05-07 20:25:26 +00:00
/// from the LinearCounting algorithm.
2015-10-05 01:11:12 +00:00
static double getThreshold()
{
return BiasData::getThreshold();
}
2017-05-07 20:25:26 +00:00
/// Return the error estimate.
2015-10-05 01:11:12 +00:00
static double getBias(double raw_estimate)
{
const auto & estimates = BiasData::getRawEstimates();
const auto & biases = BiasData::getBiases();
2015-10-05 01:11:12 +00:00
auto it = std::lower_bound(estimates.begin(), estimates.end(), raw_estimate);
2015-10-05 01:11:12 +00:00
if (it == estimates.end())
{
return biases[estimates.size() - 1];
}
else if (*it == raw_estimate)
{
size_t index = std::distance(estimates.begin(), it);
return biases[index];
}
else if (it == estimates.begin())
{
return biases[0];
}
else
{
2017-05-07 20:25:26 +00:00
/// We get the error estimate by linear interpolation.
2015-10-05 01:11:12 +00:00
size_t index = std::distance(estimates.begin(), it);
2015-10-05 01:11:12 +00:00
double estimate1 = estimates[index - 1];
double estimate2 = estimates[index];
2015-10-05 01:11:12 +00:00
double bias1 = biases[index - 1];
double bias2 = biases[index];
2017-05-07 20:25:26 +00:00
/// It is assumed that the estimate1 < estimate2 condition is always satisfied.
2015-10-05 01:11:12 +00:00
double slope = (bias2 - bias1) / (estimate2 - estimate1);
2015-10-05 01:11:12 +00:00
return bias1 + slope * (raw_estimate - estimate1);
}
}
private:
2017-05-07 20:25:26 +00:00
/// Static checks.
2015-10-05 01:11:12 +00:00
using TRawEstimatesRef = decltype(BiasData::getRawEstimates());
2017-12-25 04:01:46 +00:00
using TRawEstimates = std::remove_reference_t<TRawEstimatesRef>;
2015-10-05 01:11:12 +00:00
using TBiasDataRef = decltype(BiasData::getBiases());
2017-12-25 04:01:46 +00:00
using TBiasData = std::remove_reference_t<TBiasDataRef>;
2017-12-25 04:01:46 +00:00
static_assert(std::is_same_v<TRawEstimates, TBiasData>, "Bias estimator data have inconsistent types");
2015-10-05 01:11:12 +00:00
static_assert(std::tuple_size<TRawEstimates>::value > 0, "Bias estimator has no raw estimate data");
static_assert(std::tuple_size<TBiasData>::value > 0, "Bias estimator has no bias data");
static_assert(std::tuple_size<TRawEstimates>::value == std::tuple_size<TBiasData>::value,
"Bias estimator has inconsistent data");
};
2017-05-07 20:25:26 +00:00
/** Trivial case of HyperLogLogBiasEstimator: used if we do not want to fix
* error. This has meaning for small values of the accuracy parameter, for example 5 or 12.
2017-05-07 20:25:26 +00:00
* Then the corrections from the original version of the HyperLogLog algorithm are applied.
* See "HyperLogLog: The analysis of a near-optimal cardinality estimation algorithm"
2015-10-05 01:11:12 +00:00
* (P. Flajolet et al., AOFA '07: Proceedings of the 2007 International Conference on Analysis
* of Algorithms)
*/
struct TrivialBiasEstimator
{
static constexpr bool isTrivial()
{
return true;
}
2015-10-05 01:11:12 +00:00
static double getThreshold()
{
return 0.0;
}
2017-12-02 02:47:12 +00:00
static double getBias(double /*raw_estimate*/)
2015-10-05 01:11:12 +00:00
{
return 0.0;
}
};