Skip to content

Commit 8b05762

Browse files
Merge pull request #9 from Valeria1235/support-0_and_1_quantile
Support 0.0 and 1.0 quantile
2 parents 896ac21 + 037c85b commit 8b05762

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

quantile_estimator/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def query(self, rank):
8181
if not current:
8282
return float("nan")
8383

84+
if rank == 0.0:
85+
return current._value
86+
87+
if rank == 1.0:
88+
while current._successor:
89+
current = current._successor
90+
return current._value
91+
8492
mid_rank = math.floor(rank * self._observations)
8593
max_rank = mid_rank + math.floor(self._invariant(mid_rank, self._observations) / 2)
8694

@@ -174,8 +182,8 @@ class _Quantile:
174182
def __init__(self, quantile, inaccuracy):
175183
self._quantile = quantile
176184
self._inaccuracy = inaccuracy
177-
self._coefficient_i = (2.0 * inaccuracy) / (1.0 - quantile)
178-
self._coefficient_ii = 2.0 * inaccuracy / quantile
185+
self._coefficient_i = (2.0 * inaccuracy) / (1.0 - quantile) if quantile != 1. else float("nan")
186+
self._coefficient_ii = 2.0 * inaccuracy / quantile if quantile != .0 else float("nan")
179187

180188
"""Computes the delta for the observation."""
181189
def _delta(self, rank, n):

tests/test_estimator.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,14 @@ def test_random_observations(num_observations):
1212
estimator.observe(random.randint(1, 1000) / 100)
1313

1414
assert 0 <= estimator.query(0.5) <= estimator.query(0.9) <= estimator.query(0.99) <= 10
15+
16+
17+
def test_border_invariants():
18+
estimator = Estimator((0.0, 0.), (1.0, 0.))
19+
20+
values = [random.randint(1, 1000) for _ in range(1000)]
21+
for x in values:
22+
estimator.observe(x)
23+
24+
assert estimator.query(0) == min(values)
25+
assert estimator.query(1) == max(values)

0 commit comments

Comments
 (0)