stdlib: Add SparseHist to PyStats

This is inclusive of tests to ensure they have implemented correctly.

Change-Id: I5c84d5ffdb7b914936cfd86ca012a7b141eeaf42
This commit is contained in:
Bobby R. Bruce
2024-03-27 10:06:16 -07:00
parent b5e8804cd4
commit 178679cbfd
9 changed files with 251 additions and 0 deletions

View File

@@ -253,3 +253,31 @@ class Distribution(Vector):
# These check some basic conditions of a distribution.
assert self.bin_size >= 0
assert self.num_bins >= 1
class SparseHist(Vector):
"""A Sparse Histogram of values. A sparse histogram simply counts the "
frequency of each value in a sample. Ergo, it is, ineffect an disctionary
of values mapped to their count"""
def __init__(
self,
value: Dict[float, Scalar],
description: Optional[str] = None,
):
super().__init__(
value=value,
type="SparseHist",
description=description,
)
def size(self) -> int:
"""The number of unique sampled values."""
return len(self.value)
def count(self) -> int:
"""
Returns the total number of samples.
"""
assert self.value != None
return sum(self.value.values())