Commit 06c86146 authored by Carla Guillen's avatar Carla Guillen
Browse files

Adding quantile calculation

parent e60c5ab9
......@@ -64,5 +64,35 @@ int64_t computeStd(std::vector<reading_t> &buffer) {
}
void computeQuantiles(const std::vector<reading_t> &data, std::vector<reading_t> &quantiles, unsigned int NUMBER_QUANTILES) {
std::vector<reading_t> cdf; //!< cumulative distribution function (cfd)
cdf.assign(data.begin(), data.end()); // we copy the vector to avoid changing the order to the original
std::sort(cdf.begin(), cdf.end(), [ ](const reading_t& lhs, const reading_t& rhs) { return lhs.value < rhs.value; });
if (cdf.empty()) {
return;
}
int elementNumber = cdf.size();
quantiles.resize(NUMBER_QUANTILES + 1); //+min
float factor = elementNumber/static_cast<float>(NUMBER_QUANTILES);
quantiles[0].value = cdf[0].value;
quantiles[NUMBER_QUANTILES].value = cdf[cdf.size() - 1].value;
for (unsigned int i = 1; i < NUMBER_QUANTILES; i++) {
if (elementNumber > 1) {
int idx = static_cast<int>(std::floor(i * factor));
float rest = (i * factor) - idx;
if(idx == 0){
quantiles[i].value = cdf[0].value;
quantiles[i].timestamp = cdf[0].timestamp;
} else {
quantiles[i].value = cdf[idx - 1].value + rest * (cdf[idx].value - cdf[idx - 1].value);
quantiles[i].timestamp = cdf[idx].timestamp;
}
} else { //optimization, we don't need to calculate all the quantiles
quantiles[i].value = cdf[0].value;
quantiles[i].value = cdf[0].timestamp;
}
}
}
#endif /* ANALYTICS_INCLUDES_COMMONSTATISTICS_H_ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment