From c2cd6f62fb0f6679b5797c7486c11a183c6497ef Mon Sep 17 00:00:00 2001 From: Samuel Sloniker Date: Fri, 22 Jul 2022 14:45:43 -0700 Subject: [PATCH] Revert "Switch to `statistics.stdev`" This reverts commit 76df1dc56d478e5af054a51c4fedf3a2a1542fab. Fix major performance regression --- gptc/weighting.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/gptc/weighting.py b/gptc/weighting.py index c83d02d..5060e47 100755 --- a/gptc/weighting.py +++ b/gptc/weighting.py @@ -2,10 +2,45 @@ import math from typing import Sequence, Union, Tuple, List -import statistics + + +def _mean(numbers: Sequence[float]) -> float: + """Calculate the mean of a group of numbers + + Parameters + ---------- + numbers : list of int or float + The numbers to calculate the mean of + + Returns + ------- + float + The mean of the numbers + """ + return sum(numbers) / len(numbers) + + +def _standard_deviation(numbers: Sequence[float]) -> float: + """Calculate the standard deviation of a group of numbers + + Parameters + ---------- + numbers : list of int or float + The numbers to calculate the mean of + + Returns + ------- + float + The standard deviation of the numbers + + """ + mean = _mean(numbers) + squared_deviations = [(mean - i) ** 2 for i in numbers] + return math.sqrt(_mean(squared_deviations)) def weight(numbers: Sequence[float]) -> List[float]: - weight = statistics.stdev(numbers) * 2 + standard_deviation = _standard_deviation(numbers) + weight = standard_deviation * 2 weighted_numbers = [i * weight for i in numbers] return weighted_numbers