2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 45c499c6 authored by Carla Guillen Carias's avatar Carla Guillen Carias
Browse files

vector of quantiles is not with reading_t, but with int64_t. Some refactoring was also done.

parent 46dc8a16
......@@ -63,32 +63,27 @@ int64_t computeStd(std::vector<reading_t> &buffer) {
return acc;
}
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()) {
void computeQuantiles(const std::vector<reading_t> &data, unsigned int NUMBER_QUANTILES, std::vector<int64_t> &quantiles) {
if (data.empty() || NUMBER_QUANTILES == 0) {
return;
}
int elementNumber = cdf.size();
std::sort(data.begin(), data.end(), [ ](const reading_t& lhs, const reading_t& rhs) { return lhs.value < rhs.value; });
int elementNumber = data.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;
quantiles[0] = data[0].value; //minimum
quantiles[NUMBER_QUANTILES] = data[data.size() - 1].value; //maximum
for (unsigned int i = 1; i < NUMBER_QUANTILES; i++) {
if (elementNumber > 1) {
int idx = static_cast<int>(std::floor(i * factor));
if(idx == 0){
quantiles[i].value = cdf[0].value;
quantiles[i].timestamp = cdf[0].timestamp;
quantiles[i] = data[0].value;
} else {
float rest = (i * factor) - idx;
quantiles[i].value = cdf[idx - 1].value + rest * (cdf[idx].value - cdf[idx - 1].value);
quantiles[i].timestamp = cdf[idx].timestamp;
quantiles[i] = data[idx - 1].value + rest * (data[idx].value - data[idx - 1].value); //ToDo scaling factor??
}
} else { //optimization, we don't need to calculate all the quantiles
quantiles[i].value = cdf[0].value;
quantiles[i].value = cdf[0].timestamp;
quantiles[i] = data[0].value;
}
}
}
......
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