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),
);
};
});
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);
});
});