summaryrefslogtreecommitdiff
path: root/math_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2022-02-11 11:40:38 -0800
committerScott Gasch <[email protected]>2022-02-11 11:40:38 -0800
commit1ff5dde5b037f8da4c9d5a65e2d7ad2f6715ff60 (patch)
treea28849e405443851b2d322612f972084ccdf8dff /math_utils.py
parent699219341e92365b7aa2f84df0c141bc4f0aa238 (diff)
Add mode.
Diffstat (limited to 'math_utils.py')
-rw-r--r--math_utils.py13
1 files changed, 12 insertions, 1 deletions
diff --git a/math_utils.py b/math_utils.py
index 3a672da..156862a 100644
--- a/math_utils.py
+++ b/math_utils.py
@@ -2,10 +2,13 @@
"""Mathematical helpers."""
+import collections
import functools
import math
from heapq import heappop, heappush
-from typing import List, Optional
+from typing import Dict, List, Optional, Tuple
+
+import dict_utils
class NumericPopulation(object):
@@ -70,6 +73,14 @@ class NumericPopulation(object):
count = len(self.lowers) + len(self.highers)
return self.aggregate / count
+ def get_mode(self) -> Tuple[float, int]:
+ 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."""