From: Ondrej Lichtner olichtne@redhat.com
The old algorithm works and has the advantage of a single pass throught the value array, however in case of identical small values (less than 1) it might encounter an error of calculating the square root of a negative number instead of properly returning 0. Example: [0.031, 0.031, 0.031, 0.031, 0.031]
The new algorithm is less efficient (uses 2 iterations of the value array) but shouldn't have the same issue.
Signed-off-by: Ondrej Lichtner olichtne@redhat.com --- lnst/Common/Utils.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/lnst/Common/Utils.py b/lnst/Common/Utils.py index 0a903be..f158ff2 100644 --- a/lnst/Common/Utils.py +++ b/lnst/Common/Utils.py @@ -271,12 +271,8 @@ def dict_to_dot(original_dict, prefix=""): def std_deviation(values): if len(values) <= 0: return 0.0 - s1 = 0.0 - s2 = 0.0 - for val in values: - s1 += val - s2 += val**2 - return (math.sqrt(len(values)*s2 - s1**2))/len(values) + avg = sum(values) / float(len(values)) + return math.sqrt(sum([(float(i) - avg)**2 for i in values])/len(values))
def deprecated(func): """