diff options
| author | Scott Gasch <[email protected]> | 2022-06-30 22:37:26 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2022-06-30 22:37:26 -0700 |
| commit | f6ec577f04044f21076c4c24c6aa2ab784c6cfc9 (patch) | |
| tree | 25b21b538236d011f569f549fc674532e1c627ce /math_utils.py | |
| parent | fa101dcf585cc6578ed3fd9a0cea748c0114b59c (diff) | |
Minor cleanup.
Diffstat (limited to 'math_utils.py')
| -rw-r--r-- | math_utils.py | 20 |
1 files changed, 11 insertions, 9 deletions
diff --git a/math_utils.py b/math_utils.py index 270df8c..dd26cb6 100644 --- a/math_utils.py +++ b/math_utils.py @@ -99,6 +99,15 @@ class NumericPopulation(object): count = len(self.lowers) + len(self.highers) - 1 return math.sqrt(variance) / count + def _create_sorted_copy_if_needed(self, count: int): + if not self.sorted_copy or count != len(self.sorted_copy): + self.sorted_copy = [] + for x in self.lowers: + self.sorted_copy.append(-x) + for x in self.highers: + self.sorted_copy.append(x) + self.sorted_copy = sorted(self.sorted_copy) + def get_percentile(self, n: float) -> float: """Returns the number at approximately pn% (i.e. the nth percentile) of the distribution in O(n log n) time. Not thread-safe; @@ -108,15 +117,8 @@ class NumericPopulation(object): if n == 50: return self.get_median() count = len(self.lowers) + len(self.highers) - if self.sorted_copy is not None: - if count == len(self.sorted_copy): - index = round(count * (n / 100.0)) - assert 0 <= index < count - return self.sorted_copy[index] - self.sorted_copy = [-x for x in self.lowers] - for x in self.highers: - self.sorted_copy.append(x) - self.sorted_copy = sorted(self.sorted_copy) + self._create_sorted_copy_if_needed(count) + assert self.sorted_copy index = round(count * (n / 100.0)) assert 0 <= index < count return self.sorted_copy[index] |
