X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Futils%2FStatisticUtils.ts;h=9ca4398ca76590250de3b1c5a935f42ca373cd6f;hb=1d41bc6b533ef7361954f472811263c24cd6f64b;hp=a2d906eb06c5e06af6b44ac9b8fb607b13a7b69c;hpb=9bf0ef23c51160abc6866ad8d07eea85e308edb8;p=e-mobility-charging-stations-simulator.git diff --git a/src/utils/StatisticUtils.ts b/src/utils/StatisticUtils.ts index a2d906eb..9ca4398c 100644 --- a/src/utils/StatisticUtils.ts +++ b/src/utils/StatisticUtils.ts @@ -1,55 +1,84 @@ -import { isEmptyArray, isNullOrUndefined } from './Utils'; +/** + * Computes the average of the given data set. + * + * @param dataSet - Data set. + * @returns The average of the given data set. + * @internal + */ +export const average = (dataSet: number[]): number => { + if (Array.isArray(dataSet) && dataSet.length === 0) { + return 0 + } else if (Array.isArray(dataSet) && dataSet.length === 1) { + return dataSet[0] + } + return dataSet.reduce((accumulator, nb) => accumulator + nb, 0) / dataSet.length +} +/** + * Computes the median of the given data set. + * + * @param dataSet - Data set. + * @returns The median of the given data set. + * @internal + */ export const median = (dataSet: number[]): number => { - if (isEmptyArray(dataSet)) { - return 0; - } - if (Array.isArray(dataSet) === true && dataSet.length === 1) { - return dataSet[0]; + if (Array.isArray(dataSet) && dataSet.length === 0) { + return 0 + } else if (Array.isArray(dataSet) && dataSet.length === 1) { + return dataSet[0] } - const sortedDataSet = dataSet.slice().sort((a, b) => a - b); + const sortedDataSet = dataSet.slice().sort((a, b) => a - b) return ( (sortedDataSet[(sortedDataSet.length - 1) >> 1] + sortedDataSet[sortedDataSet.length >> 1]) / 2 - ); -}; + ) +} // 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 (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 (!isNullOrUndefined(sortedDataSet[percentileIndexInteger + 1])) { + const percentileIndexBase = (percentile / 100) * (sortedDataSet.length - 1) + const percentileIndexInteger = Math.floor(percentileIndexBase) + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition + if (sortedDataSet[percentileIndexInteger + 1] != null) { return ( sortedDataSet[percentileIndexInteger] + (percentileIndexBase - percentileIndexInteger) * (sortedDataSet[percentileIndexInteger + 1] - sortedDataSet[percentileIndexInteger]) - ); + ) } - return sortedDataSet[percentileIndexInteger]; -}; + return sortedDataSet[percentileIndexInteger] +} -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 = average(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) + ) +}