fix: use unbiased standard deviation formula
authorJérôme Benoit <jerome.benoit@sap.com>
Wed, 6 Dec 2023 13:24:39 +0000 (14:24 +0100)
committerJérôme Benoit <jerome.benoit@sap.com>
Wed, 6 Dec 2023 13:24:39 +0000 (14:24 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@sap.com>
src/utils/StatisticUtils.ts
tests/utils/StatisticUtils.test.ts

index f0bd1f28bbc789500e3b0ae41c966dbf9f3ac241..2ff71b0acb2ffa897a5d07f0e7bcd18719314903 100644 (file)
@@ -64,12 +64,27 @@ export const nthPercentile = (dataSet: number[], percentile: number): number =>
   return sortedDataSet[percentileIndexInteger];
 };
 
+/**
+ * Computes the sample standard deviation of the given data set.
+ *
+ * @param dataSet - Data set.
+ * @param dataSetAverage - Average of the data set.
+ * @returns The sample standard deviation of the given data set.
+ * @see https://en.wikipedia.org/wiki/Unbiased_estimation_of_standard_deviation
+ * @internal
+ */
 export const stdDeviation = (
   dataSet: number[],
   dataSetAverage: number = average(dataSet),
 ): number => {
+  if (isEmptyArray(dataSet)) {
+    return 0;
+  }
+  if (Array.isArray(dataSet) && dataSet.length === 1) {
+    return 0;
+  }
   return Math.sqrt(
     dataSet.reduce((accumulator, num) => accumulator + Math.pow(num - dataSetAverage, 2), 0) /
-      dataSet.length,
+      (dataSet.length - 1),
   );
 };
index d9aadf0c5e77426f6170104799048ba79533c426..38963dbe97d2c69b27f18766f5be335351d12280 100644 (file)
@@ -33,6 +33,6 @@ await describe('StatisticUtils test suite', async () => {
   });
 
   await it('Verify stdDeviation()', () => {
-    expect(stdDeviation([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(2.0256064851429216);
+    expect(stdDeviation([0.25, 4.75, 3.05, 6.04, 1.01, 2.02, 5.03])).toBe(2.1879050645374383);
   });
 });