Skip to content

Commit

Permalink
gh-761 used double for quantile
Browse files Browse the repository at this point in the history
  • Loading branch information
novertia committed Jul 20, 2024
1 parent 90653c5 commit 9ba8167
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
27 changes: 16 additions & 11 deletions src/internal_modules/roc_core/mov_quantile.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ namespace core {
//! @tparam T defines a sample type.
template <typename T> class MovQuantile {
public:
MovQuantile(IArena& arena, const size_t win_len, const size_t percentile)
MovQuantile(IArena& arena, const size_t win_len, const double quantile)
//! Initialize
: win_len_(win_len)
, percentile_(percentile)
, quantile_(quantile)
, old_heap_root_index_(0)
, heap_root_((percentile * (win_len - 1)) / 100)
, heap_root_(0)
, heap_size_(0)
, max_heap_index_(heap_root_)
, min_heap_index_(heap_root_)
, max_heap_index_(0)
, min_heap_index_(0)
, elem_index_(0)
, win_filled_(false)
, valid_(false)
Expand All @@ -45,8 +45,8 @@ template <typename T> class MovQuantile {
if (win_len == 0) {
roc_panic("mov quantile: window length must be greater than 0");
}
if (percentile > 100) {
roc_panic("mov quantile: percentile should be between 0 and 100");
if (quantile < 0 && quantile > 1) {
roc_panic("mov quantile: quantile should be between 0 and 1");
}
if (!heap_.resize(win_len)) {
return;
Expand All @@ -57,6 +57,10 @@ template <typename T> class MovQuantile {
if (!heap_index_elem_index_.resize(win_len)) {
return;
}
double index = (quantile * static_cast<double>(win_len - 1));
heap_root_ = static_cast<size_t>(index);
max_heap_index_ = heap_root_;
min_heap_index_ = heap_root_;
valid_ = true;
}

Expand Down Expand Up @@ -192,7 +196,8 @@ template <typename T> class MovQuantile {
heap_[heap_index] = x;
heapify(heap_index);
} else {
size_t k = (percentile_ * (heap_size_ - 1)) / 100;
double index = quantile_ * static_cast<double>(heap_size_ - 1);
size_t k = static_cast<size_t>(index);
size_t heap_index;
if (elem_index_ == 0) {
heap_index = heap_root_;
Expand Down Expand Up @@ -225,13 +230,13 @@ template <typename T> class MovQuantile {

//! Length of the sliding window
const size_t win_len_;
//! Percentile of the window elements
const size_t percentile_;
//! Quantile of the window elements
const double quantile_;

//! Used to check the window filling logic
size_t old_heap_root_index_;
//! Index which seperates max and min heap and also act as their root
const size_t heap_root_;
size_t heap_root_;

//! Maintains current heap size
size_t heap_size_;
Expand Down
10 changes: 5 additions & 5 deletions src/tests/roc_core/test_mov_quantile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ TEST_GROUP(movquantile) {

TEST(movquantile, testing_minimum) {
const size_t n = 9;
MovQuantile<int64_t> quant(arena, n, 0);
MovQuantile<int64_t> quant(arena, n, 0.0);
CHECK(quant.is_valid());
quant.add(14);
quant.add(28);
Expand All @@ -40,7 +40,7 @@ TEST(movquantile, testing_minimum) {

TEST(movquantile, testing_lower_side) {
const size_t n = 12;
MovQuantile<int64_t> quant(arena, n, 34);
MovQuantile<int64_t> quant(arena, n, 0.34);
CHECK(quant.is_valid());
quant.add(10);
quant.add(12);
Expand All @@ -66,7 +66,7 @@ TEST(movquantile, testing_lower_side) {

TEST(movquantile, testing_median) {
const size_t n = 10;
MovQuantile<int64_t> quant(arena, n, 50);
MovQuantile<int64_t> quant(arena, n, 0.50);
CHECK(quant.is_valid());
quant.add(18);
quant.add(12);
Expand All @@ -91,7 +91,7 @@ TEST(movquantile, testing_median) {

TEST(movquantile, testing_upper_side) {
const size_t n = 11;
MovQuantile<int64_t> quant(arena, n, 78);
MovQuantile<int64_t> quant(arena, n, 0.78);
CHECK(quant.is_valid());
quant.add(18);
quant.add(18);
Expand All @@ -112,7 +112,7 @@ TEST(movquantile, testing_upper_side) {

TEST(movquantile, test_maximum) {
const size_t n = 7;
MovQuantile<int64_t> quant(arena, n, 100);
MovQuantile<int64_t> quant(arena, n, 1);
CHECK(quant.is_valid());
quant.add(21);
quant.add(14);
Expand Down

0 comments on commit 9ba8167

Please sign in to comment.