X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FStatisticUtils.ts;h=9f50b8eaa12b08037ef9c49b042ac96a62463bef;hb=452a4864d4a8d0286ddd351958d8cc02574b4ba9;hp=bb9a847091decf2ca488f83ad8c3d9d579d6e7fd;hpb=d58b442097da31f8b974d51aef63c64470d9ab48;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/StatisticUtils.ts b/src/utils/StatisticUtils.ts index bb9a8470..9f50b8ea 100644 --- a/src/utils/StatisticUtils.ts +++ b/src/utils/StatisticUtils.ts @@ -1,55 +1,53 @@ -import { Utils } from './Utils'; +import { mean } from 'rambda' -export const median = (dataSet: number[]): number => { - if (Utils.isEmptyArray(dataSet)) { - return 0; - } - if (Array.isArray(dataSet) === true && dataSet.length === 1) { - return dataSet[0]; - } - const sortedDataSet = dataSet.slice().sort((a, b) => a - b); - return ( - (sortedDataSet[(sortedDataSet.length - 1) >> 1] + sortedDataSet[sortedDataSet.length >> 1]) / 2 - ); -}; +export const min = (...args: number[]): number => + args.reduce((minimum, num) => (minimum < num ? minimum : num), Infinity) + +export const max = (...args: number[]): number => + args.reduce((maximum, num) => (maximum > num ? maximum : num), -Infinity) // TODO: use order statistics tree https://en.wikipedia.org/wiki/Order_statistic_tree export const nthPercentile = (dataSet: number[], percentile: number): number => { if (percentile < 0 && percentile > 100) { - throw new RangeError('Percentile is not between 0 and 100'); + throw new RangeError('Percentile is not between 0 and 100') } - if (Utils.isEmptyArray(dataSet)) { - return 0; + if (Array.isArray(dataSet) && dataSet.length === 0) { + return 0 } - const sortedDataSet = dataSet.slice().sort((a, b) => a - b); + const sortedDataSet = dataSet.slice().sort((a, b) => a - b) if (percentile === 0 || sortedDataSet.length === 1) { - return sortedDataSet[0]; + return sortedDataSet[0] } if (percentile === 100) { - return sortedDataSet[sortedDataSet.length - 1]; + return sortedDataSet[sortedDataSet.length - 1] } - const percentileIndexBase = (percentile / 100) * (sortedDataSet.length - 1); - const percentileIndexInteger = Math.floor(percentileIndexBase); - if (!Utils.isNullOrUndefined(sortedDataSet[percentileIndexInteger + 1])) { + const base = (percentile / 100) * (sortedDataSet.length - 1) + const baseIndex = Math.floor(base) + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (sortedDataSet[baseIndex + 1] != null) { return ( - sortedDataSet[percentileIndexInteger] + - (percentileIndexBase - percentileIndexInteger) * - (sortedDataSet[percentileIndexInteger + 1] - sortedDataSet[percentileIndexInteger]) - ); + sortedDataSet[baseIndex] + + (base - baseIndex) * (sortedDataSet[baseIndex + 1] - sortedDataSet[baseIndex]) + ) } - return sortedDataSet[percentileIndexInteger]; -}; + return sortedDataSet[baseIndex] +} -export const stdDeviation = (dataSet: number[]): number => { - let totalDataSet = 0; - for (const data of dataSet) { - totalDataSet += data; - } - const dataSetMean = totalDataSet / dataSet.length; - let totalGeometricDeviation = 0; - for (const data of dataSet) { - const deviation = data - dataSetMean; - totalGeometricDeviation += deviation * deviation; - } - return Math.sqrt(totalGeometricDeviation / dataSet.length); -}; +/** + * 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 = mean(dataSet)): number => { + if (Array.isArray(dataSet) && (dataSet.length === 0 || dataSet.length === 1)) { + return 0 + } + return Math.sqrt( + dataSet.reduce((accumulator, num) => accumulator + Math.pow(num - dataSetAverage, 2), 0) / + (dataSet.length - 1) + ) +}