refactor: cleanup standard deviation implementation
authorJérôme Benoit <jerome.benoit@sap.com>
Fri, 15 Sep 2023 20:08:51 +0000 (22:08 +0200)
committerJérôme Benoit <jerome.benoit@sap.com>
Fri, 15 Sep 2023 20:08:51 +0000 (22:08 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/utils/StatisticUtils.ts

index ecd47a17f9af0b2d49601542ec3c8af78997d9dd..7f338ff9d08d88651a6d012b036ef73f43946ec5 100644 (file)
@@ -68,9 +68,9 @@ export const stdDeviation = (
   dataSet: number[],
   dataSetAverage: number = average(dataSet),
 ): number => {
-  const geometricDeviation = dataSet.reduce((accumulator, nb) => {
-    const deviation = nb - dataSetAverage;
-    return accumulator + deviation * deviation;
-  }, 0);
-  return Math.sqrt(geometricDeviation / dataSet.length);
+  return Math.sqrt(
+    dataSet.reduce((accumulator, num) => {
+      return accumulator + Math.pow(num - dataSetAverage, 2);
+    }, 0) / dataSet.length,
+  );
 };