Skip to content

Commit f6ed4fa

Browse files
committed
Implement get rank
1 parent 4817bb1 commit f6ed4fa

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

ddsketch/include/ddsketch_impl.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#ifndef DDSKETCH_IMPL_H
2121
#define DDSKETCH_IMPL_H
2222

23+
#include <iomanip>
24+
#include <iostream>
2325
#include "ddsketch.hpp"
2426
#include "store_factory.hpp"
2527
namespace datasketches {
@@ -134,6 +136,27 @@ double DDSketch<Store, Mapping>::get_max() const {
134136
return -index_mapping.value(negative_store.get_min_index());
135137
}
136138

139+
template<store_concept Store, class Mapping>
140+
double DDSketch<Store, Mapping>::get_rank(const double &item) const {
141+
double rank = 0.0;
142+
143+
if (!negative_store.is_empty()) {
144+
for (auto it = negative_store.rbegin(); it != negative_store.rend() && index_mapping.value((*it).getIndex()) <= item; ++it) {
145+
rank += (*it).getCount();
146+
}
147+
}
148+
if (item >= 0) {
149+
rank += zero_count;
150+
}
151+
if (!positive_store.is_empty()) {
152+
for (auto it = positive_store.begin(); it != positive_store.end() && index_mapping.value((*it).getIndex()) <= item; ++it) {
153+
rank += (*it).getCount();
154+
}
155+
}
156+
return rank / get_count();
157+
}
158+
159+
137160
template<store_concept Store, class Mapping>
138161
double DDSketch<Store, Mapping>::get_quantile(const double& rank) const {
139162
return get_quantile(rank, get_count());

ddsketch/test/DDSketchTest.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,4 +515,19 @@ TEMPLATE_TEST_CASE("DDSketch merging random test", "[ddsketch][random]",
515515
test_merging<decltype(sketch)>(sketch, value_arrays, relative_accuracy);
516516
}
517517
}
518+
519+
TEST_CASE("quantile", "[ddsketch]") {
520+
std::random_device rd{};
521+
std::mt19937_64 gen{rd()};
522+
std::normal_distribution<double> d(0.0, 1.0);
523+
524+
DDSketch<SparseStore<A>, LogarithmicMapping> ddsketch(0.01);
525+
for (size_t i = 0; i < 1000000; ++i) {
526+
double val = d(gen);
527+
ddsketch.update(val);
528+
}
529+
530+
std::cout << ddsketch.get_quantile(0.5) << std::endl;
531+
std::cout << ddsketch.get_rank(4) << std::endl;
532+
}
518533
} /* namespace datasketches */

tdigest/test/tdigest_test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,20 @@ TEST_CASE("many values", "[tdigest]") {
8484
REQUIRE(cdf[1] == 1);
8585
}
8686

87+
TEST_CASE("rank - quantile", "[tdigest]") {
88+
std::random_device rd{};
89+
std::mt19937_64 gen{rd()};
90+
std::normal_distribution<double> d(0.0, 1.0);
91+
tdigest_double td;
92+
for (size_t i = 0; i < 10000; ++i) {
93+
double val = d(gen);
94+
td.update(val);
95+
}
96+
97+
std::cout << td.get_quantile(0.5) << std::endl;
98+
std::cout << td.get_rank(0) << std::endl;
99+
}
100+
87101
TEST_CASE("rank - two values", "[tdigest]") {
88102
tdigest_double td(100);
89103
td.update(1);

0 commit comments

Comments
 (0)