+ count = len(self.lowers) + len(self.highers)
+ return math.sqrt(variance) / count
+
+ def _create_sorted_copy_if_needed(self, count: int):
+ if not self.sorted_copy or count != len(self.sorted_copy):
+ self.sorted_copy = []
+ for x in self.lowers:
+ self.sorted_copy.append(-x)
+ for x in self.highers:
+ self.sorted_copy.append(x)
+ self.sorted_copy = sorted(self.sorted_copy)
+
+ def get_percentile(self, n: float) -> float:
+ """Returns the number at approximately pn% (i.e. the nth percentile)
+ of the distribution in O(n log n) time. Not thread-safe;
+ does caching across multiple calls without an invocation to
+ add_number for perf reasons.
+ """
+ if n == 50:
+ return self.get_median()
+ count = len(self.lowers) + len(self.highers)
+ self._create_sorted_copy_if_needed(count)
+ assert self.sorted_copy
+ index = round(count * (n / 100.0))
+ index = max(0, index)
+ index = min(count - 1, index)
+ return self.sorted_copy[index]