Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/x/__tests__/xRobustDistributionStats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
5 changes: 3 additions & 2 deletions src/x/xDistributionStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
7 changes: 4 additions & 3 deletions src/x/xRobustDistributionStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Loading