From 764648043137930f9a0d40439682227151778710 Mon Sep 17 00:00:00 2001 From: Ondrej Lichtner Date: Thu, 8 Feb 2024 10:46:26 +0100 Subject: [PATCH] lnst.Common.Utils: use statistics in std_deviation This function was written back in 2015 when we couldn't use python3 yet and statistics module was added in 3.4. At the same time, the formula used is "incorrect", this calculates the POPULATION std deviation... whereas we really should be using a SAMPLE standard deviation formula. Signed-off-by: Ondrej Lichtner --- lnst/Common/Utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lnst/Common/Utils.py b/lnst/Common/Utils.py index 5c3a80ab6..044330187 100644 --- a/lnst/Common/Utils.py +++ b/lnst/Common/Utils.py @@ -21,7 +21,7 @@ import errno import ast import collections -import math +import statistics import itertools from collections.abc import Iterable, Callable from contextlib import AbstractContextManager @@ -287,10 +287,9 @@ def dict_to_dot(original_dict, prefix=""): return return_list def std_deviation(values): - if len(values) <= 0: + if len(values) <= 1: return 0.0 - avg = sum(values) / float(len(values)) - return math.sqrt(sum([(float(i) - avg)**2 for i in values])/len(values)) + return statistics.stdev(values) def deprecated(func): """