summaryrefslogtreecommitdiff
path: root/math_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2022-02-10 08:13:08 -0800
committerScott Gasch <[email protected]>2022-02-10 08:13:08 -0800
commita4b50bb62e2653d3d084c6c7e0574abb9277b8d7 (patch)
treef9c8d2c33adae7b52cea42b63d47018d134e9e5f /math_utils.py
parentc974b8cde11f133df7680967b759772c624007e9 (diff)
Add some useful stats to histogram.
Diffstat (limited to 'math_utils.py')
-rw-r--r--math_utils.py19
1 files changed, 17 insertions, 2 deletions
diff --git a/math_utils.py b/math_utils.py
index 37fcec5..28b8e6b 100644
--- a/math_utils.py
+++ b/math_utils.py
@@ -25,12 +25,14 @@ class RunningMedian(object):
def __init__(self):
self.lowers, self.highers = [], []
+ self.aggregate = 0.0
- def add_number(self, number):
+ def add_number(self, number: float):
if not self.highers or number > self.highers[0]:
heappush(self.highers, number)
else:
heappush(self.lowers, -number) # for lowers we need a max heap
+ self.aggregate += number
self.rebalance()
def rebalance(self):
@@ -39,7 +41,7 @@ class RunningMedian(object):
elif len(self.highers) - len(self.lowers) > 1:
heappush(self.lowers, -heappop(self.highers))
- def get_median(self):
+ def get_median(self) -> float:
if len(self.lowers) == len(self.highers):
return (-self.lowers[0] + self.highers[0]) / 2
elif len(self.lowers) > len(self.highers):
@@ -47,6 +49,19 @@ class RunningMedian(object):
else:
return self.highers[0]
+ def get_mean(self) -> float:
+ count = len(self.lowers) + len(self.highers)
+ return self.aggregate / count
+
+ def get_stdev(self) -> float:
+ mean = self.get_mean()
+ variance = 0.0
+ for n in self.lowers:
+ variance += (n - mean) ** 2
+ for n in self.highers:
+ variance += (n - mean) ** 2
+ return math.sqrt(variance)
+
def gcd_floats(a: float, b: float) -> float:
if a < b: