Skip to content

INDY-1543: Fix crash in stddev, add average bounds #877

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion plenum/common/value_accumulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def stddev(self):
if self.count < 2:
return None
d = (self._sumsq - self.count * (self.avg ** 2)) / (self.count - 1)
return math.sqrt(d)
return math.sqrt(max(0.0, d))

@property
def min(self):
Expand All @@ -108,3 +108,24 @@ def min(self):
@property
def max(self):
return self._max

@property
def lo(self):
return self.std_range()[0]

@property
def hi(self):
return self.std_range()[1]

def std_range(self):
avg = self.avg
std = self.stddev
if not std or self._min == self._max:
return avg, avg

std = min(std, self._max - self._min)
skew = (self._max - avg) / (avg - self._min)
inv_skew = 1.0 / (1.0 + skew)
a = avg - std * inv_skew
b = avg + std * inv_skew * skew
return a, b
10 changes: 10 additions & 0 deletions plenum/test/metrics/test_value_accumulator.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import statistics
import struct

Expand All @@ -12,6 +13,8 @@ def test_value_accumulator_dont_return_anything_when_created():
assert acc.stddev is None
assert acc.min is None
assert acc.max is None
assert acc.lo is None
assert acc.hi is None


def test_value_accumulator_can_add_value():
Expand All @@ -24,6 +27,8 @@ def test_value_accumulator_can_add_value():
assert acc.stddev is None
assert acc.min == value
assert acc.max == value
assert acc.lo == value
assert acc.hi == value


def test_value_accumulator_handles_same_values():
Expand All @@ -39,6 +44,8 @@ def test_value_accumulator_handles_same_values():
assert acc.stddev == 0
assert acc.min == value
assert acc.max == value
assert acc.lo == value
assert acc.hi == value


def test_value_accumulator_can_add_several_values():
Expand All @@ -53,6 +60,9 @@ def test_value_accumulator_can_add_several_values():
assert acc.stddev == statistics.stdev(values)
assert acc.min == min(values)
assert acc.max == max(values)
assert acc.min < acc.lo < acc.avg
assert acc.avg < acc.hi < acc.max
assert math.isclose(acc.hi - acc.lo, acc.stddev)


def test_value_accumulator_eq_has_value_semantics():
Expand Down