+ def get_mean(self) -> float:
+ """Returns the mean (arithmetic mean) so far in O(1) time."""
+
+ count = len(self.lowers) + len(self.highers)
+ return self.aggregate / count
+
+ def get_mode(self) -> Tuple[float, int]:
+ """Returns the mode (most common member in the population)
+ in O(n) time."""
+
+ count: Dict[float, int] = collections.defaultdict(int)
+ for n in self.lowers:
+ count[-n] += 1
+ for n in self.highers:
+ count[n] += 1
+ return dict_utils.item_with_max_value(count)
+
+ def get_stdev(self) -> float:
+ """Returns the stdev so far in O(n) time."""
+
+ mean = self.get_mean()
+ variance = 0.0
+ for n in self.lowers:
+ n = -n
+ variance += (n - mean) ** 2
+ for n in self.highers:
+ variance += (n - mean) ** 2
+ 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]
+