diff --git a/src/x/__tests__/xRobustDistributionStats.test.ts b/src/x/__tests__/xRobustDistributionStats.test.ts index 111fc0b6a..7e91a1e25 100644 --- a/src/x/__tests__/xRobustDistributionStats.test.ts +++ b/src/x/__tests__/xRobustDistributionStats.test.ts @@ -128,3 +128,28 @@ test('typed array', () => { xRobustDistributionStats(data), ); }); + +test('more complex case with multiple outliers', () => { + const data = [ + 1050, 1052, 1052, 1052, 1053, 1055, 1055, 1055, 1055, 1056, 1056, 1056, + 1056, 1056, 1056, 1056, 1056, 1056, 1056, 1057, 1057, 1057, 1057, 1057, + 1057, 1057, 1057, 1057, 1057, 1058, 1058, 1058, 1059, 1065, 1072, 1074, + ]; + + expect(xRobustDistributionStats(data)).toStrictEqual({ + min: 1050, + q1: 1055.75, + median: 1056, + q3: 1057, + max: 1074, + lowerWhisker: 1053.875, + upperWhisker: 1058.875, + minWhisker: 1055, + maxWhisker: 1058, + iqr: 1.25, + outliers: [1050, 1052, 1052, 1052, 1053, 1059, 1065, 1072, 1074], + mean: 1056.4444444444443, + sd: 0.8915558282417289, + nb: 27, + }); +}); diff --git a/src/x/xDistributionStats.ts b/src/x/xDistributionStats.ts index 73c1bdf5c..b789ce667 100644 --- a/src/x/xDistributionStats.ts +++ b/src/x/xDistributionStats.ts @@ -20,10 +20,11 @@ export interface XDistributionStats extends XBoxPlotWithOutliers { * @returns q1, median, q3, min, max. */ export function xDistributionStats(array: NumberArray): XDistributionStats { + const mean = xMean(array); return { ...xBoxPlotWithOutliers(array), - mean: xMean(array), - sd: xStandardDeviation(array), + mean, + sd: xStandardDeviation(array, { mean }), nb: array.length, }; } diff --git a/src/x/xRobustDistributionStats.ts b/src/x/xRobustDistributionStats.ts index 6430f6b11..d474c0468 100644 --- a/src/x/xRobustDistributionStats.ts +++ b/src/x/xRobustDistributionStats.ts @@ -41,16 +41,17 @@ export function xRobustDistributionStats( filteredArray = new Float64Array(array.length - boxPlot.outliers.length); let j = 0; for (const element of array) { - if (element >= boxPlot.min && element <= boxPlot.max) { + if (element >= boxPlot.lowerWhisker && element <= boxPlot.upperWhisker) { filteredArray[j++] = element; } } } + const mean = xMean(filteredArray); return { ...boxPlot, - mean: xMean(filteredArray), - sd: xStandardDeviation(filteredArray), + mean, + sd: xStandardDeviation(filteredArray, { mean }), nb: filteredArray.length, }; }