X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=histogram.py;h=dd47319cba845687deefe0c7f72de0e24f8e1500;hb=f2600f30801c849fc1d139386e3ddc3c9eb43e30;hp=6f9c0f1e41750f0562293ffedfc4d8383a3f0aa8;hpb=94f43043157f00fd2d2483cbbad63f9a680c0085;p=python_utils.git diff --git a/histogram.py b/histogram.py index 6f9c0f1..dd47319 100644 --- a/histogram.py +++ b/histogram.py @@ -5,7 +5,7 @@ import math from dataclasses import dataclass -from typing import Dict, Generic, Iterable, List, NamedTuple, Optional, Tuple, TypeVar +from typing import Dict, Generic, Iterable, List, Optional, Tuple, TypeVar T = TypeVar("T", int, float) Bound = int @@ -32,7 +32,7 @@ class SimpleHistogram(Generic[T]): NEGATIVE_INFINITY = -math.inf def __init__(self, buckets: List[Tuple[Bound, Bound]]): - from math_utils import RunningMedian + from math_utils import NumericPopulation self.buckets: Dict[Tuple[Bound, Bound], Count] = {} for start_end in buckets: @@ -40,7 +40,7 @@ class SimpleHistogram(Generic[T]): raise Exception("Buckets overlap?!") self.buckets[start_end] = 0 self.sigma: float = 0.0 - self.median: RunningMedian = RunningMedian() + self.stats: NumericPopulation = NumericPopulation() self.maximum: Optional[T] = None self.minimum: Optional[T] = None self.count: Count = 0 @@ -74,7 +74,7 @@ class SimpleHistogram(Generic[T]): self.count += 1 self.buckets[bucket] += 1 self.sigma += item - self.median.add_number(item) + self.stats.add_number(item) if self.maximum is None or item > self.maximum: self.maximum = item if self.minimum is None or item < self.minimum: @@ -142,4 +142,11 @@ class SimpleHistogram(Generic[T]): txt += sigma_label.rjust(details.max_label_width) txt += ' ' * (bar_width - 2) txt += f'Σ=(100.00% n={self.count})\n' + txt += ' ' * (bar_width + details.max_label_width - 2) + txt += f'mean(μ)={self.stats.get_mean():.3f}\n' + txt += ' ' * (bar_width + details.max_label_width - 2) + txt += f'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 += '\n' return txt