From 3a79273d692f1b6298102dd82491da168b3f1c74 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 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lnst/Common/Utils.py b/lnst/Common/Utils.py index 5c3a80ab6..51b847cc6 100644 --- a/lnst/Common/Utils.py +++ b/lnst/Common/Utils.py @@ -22,6 +22,7 @@ import ast import collections import math +import statistics import itertools from collections.abc import Iterable, Callable from contextlib import AbstractContextManager @@ -287,10 +288,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): """