[benchmark] Round quantile idx to nearest or even

Explicitly use round-half-to-even rounding algorithm to match the behavior of numpy's quantile(interpolation='nearest') and quantile estimate type R-3, SAS-2. See:
https://en.wikipedia.org/wiki/Quantile#Estimating_quantiles_from_a_sample
This commit is contained in:
Pavol Vaskovic
2018-09-10 10:45:00 +02:00
parent 84bf15836d
commit a56c55c8e4
3 changed files with 24 additions and 9 deletions

View File

@@ -34,6 +34,7 @@ import re
import sys
from bisect import bisect, bisect_left, bisect_right
from collections import namedtuple
from decimal import Decimal, ROUND_HALF_EVEN
from math import sqrt
@@ -141,24 +142,32 @@ class PerformanceTestSamples(object):
"""Maximum sampled value."""
return self.samples[-1].runtime
def _quantile_index(self, q, i):
"""Return index of the element nearest to the i-th q-quantile."""
return int(round((self.count - 1) / float(q) * float(i)))
def quantile(self, q):
"""Return runtime of a sample nearest to the quantile.
Explicitly uses round-half-to-even rounding algorithm to match the
behavior of numpy's quantile(interpolation='nearest') and quantile
estimate type R-3, SAS-2. See:
https://en.wikipedia.org/wiki/Quantile#Estimating_quantiles_from_a_sample
"""
index = int(Decimal((self.count - 1) * Decimal(q))
.quantize(0, ROUND_HALF_EVEN))
return self.samples[index].runtime
@property
def median(self):
"""Median sampled value."""
return self.samples[self._quantile_index(2, 1)].runtime
return self.quantile(0.5)
@property
def q1(self):
"""First Quartile (25th Percentile)."""
return self.samples[self._quantile_index(4, 1)].runtime
return self.quantile(0.25)
@property
def q3(self):
"""Third Quartile (75th Percentile)."""
return self.samples[self._quantile_index(4, 3)].runtime
return self.quantile(0.75)
@property
def iqr(self):