boostorg / boostorg/accumulators

tail_quantile off by 1 error?

Open
#34 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
26
Forks
61
PR merge metrics
No merged PRs in 30d

Description

Consider the following program:
```cpp
#include
#include
#include

using namespace boost::accumulators;

int main() {
accumulator_set>> acc(tag::tail::cache_size = 3);
acc(3);
acc(4);
acc(5);

printf("%d\n", quantile(acc, quantile_probability = 0));
printf("%d\n", quantile(acc, quantile_probability = 0.9));
}
```

On my machine it outputs
```
0
terminate called after throwing an instance of 'boost::exception_detail::clone_impl >'
what(): index n = 3 is not in valid range [0, 3)
Aborted (core dumped)
```
Notice that the result for `quantile_probability = 0` is wrong too. Digging into https://www.boost.org/doc/libs/1_70_0/boost/accumulators/statistics/tail_quantile.hpp, I found that index -1 is accessed when `quantile_probability = 0`.

The simplest fix seems to be:
Change
```
if ( n < static_cast(tail(args).size()))
```
to
```
if (1 <= n && n <= static_cast(tail(args).size()))
```

with the caveat that exception would be thrown when `quantile_probability = 0`.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with boost/accumulators/statistics/tail_quantile.hpp and build the C++ reproducer from the issue to observe both boundary probabilities. Check the index handling for quantile_probability values 0 and 0.9, then verify that the chosen boundary behavior is consistent and the reproducer no longer reports an incorrect result or invalid index.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
data
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.