+ count = len(self.lowers) + len(self.highers) - 1
+ return math.sqrt(variance) / count
+
+ 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)
+ if self.sorted_copy is not None:
+ if count == len(self.sorted_copy):
+ index = round(count * (n / 100.0))
+ assert 0 <= index < count
+ return self.sorted_copy[index]
+ self.sorted_copy = [-x for x in self.lowers]
+ for x in self.highers:
+ self.sorted_copy.append(x)
+ self.sorted_copy = sorted(self.sorted_copy)
+ index = round(count * (n / 100.0))
+ assert 0 <= index < count
+ return self.sorted_copy[index]