diff options
| author | Scott Gasch <[email protected]> | 2022-02-11 11:40:38 -0800 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2022-02-11 11:40:38 -0800 |
| commit | 1ff5dde5b037f8da4c9d5a65e2d7ad2f6715ff60 (patch) | |
| tree | a28849e405443851b2d322612f972084ccdf8dff | |
| parent | 699219341e92365b7aa2f84df0c141bc4f0aa238 (diff) | |
Add mode.
| -rw-r--r-- | histogram.py | 14 | ||||
| -rw-r--r-- | math_utils.py | 13 |
2 files changed, 21 insertions, 6 deletions
diff --git a/histogram.py b/histogram.py index dd47319..3796470 100644 --- a/histogram.py +++ b/histogram.py @@ -122,9 +122,11 @@ class SimpleHistogram(Generic[T]): ) if len(sigma_label) > details.max_label_width: details.max_label_width = len(sigma_label) - bar_width = width - (details.max_label_width + 16) + bar_width = width - (details.max_label_width + 17) for (start, end), pop in sorted(self.buckets.items(), key=lambda x: x[0]): + if start < details.lowest_start: + continue label = f'[{label_formatter}..{label_formatter}): ' % (start, end) bar = bar_graph( (pop / details.max_population), @@ -141,12 +143,14 @@ class SimpleHistogram(Generic[T]): txt += '-' * width + '\n' txt += sigma_label.rjust(details.max_label_width) txt += ' ' * (bar_width - 2) - txt += f'Σ=(100.00% n={self.count})\n' + txt += f' pop(Σn)={self.count}\n' txt += ' ' * (bar_width + details.max_label_width - 2) - txt += f'mean(μ)={self.stats.get_mean():.3f}\n' + txt += f' mean(x̄)={self.stats.get_mean():.3f}\n' txt += ' ' * (bar_width + details.max_label_width - 2) - txt += f'p50(η)={self.stats.get_median():.3f}\n' + txt += f' median(p50)={self.stats.get_median():.3f}\n' txt += ' ' * (bar_width + details.max_label_width - 2) - txt += f'stdev(σ)={self.stats.get_stdev():.3f}\n' + txt += f' mode(Mo)={self.stats.get_mode()[0]:.3f}\n' + txt += ' ' * (bar_width + details.max_label_width - 2) + txt += f' stdev(σ)={self.stats.get_stdev():.3f}\n' txt += '\n' return txt 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.""" |
