Skip to content

Commit

Permalink
chore: optimised getOutliers method
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwenk committed Jun 20, 2023
1 parent 7f44e3d commit b2a5de5
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/casekit/nmr/analysis/HOSECodeShiftStatistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public static Map<String, Map<String, Double[]>> buildHOSECodeShiftStatistics(
for (final Map.Entry<String, List<Double>> solvents : hoseCodes.getValue()
.entrySet()) {
values = new ArrayList<>(solvents.getValue());
Statistics.removeOutliers(values, 1.5);
values = Statistics.removeOutliers(values, 1.5);
hoseCodeShiftStatistics.get(hoseCodes.getKey())
.put(solvents.getKey(),
new Double[]{(double) values.size(), Collections.min(values),
Expand Down
63 changes: 46 additions & 17 deletions src/casekit/nmr/utils/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,61 @@ public class Statistics {
*
* @return new array list without values outside the generated boundaries
*/
public static void removeOutliers(final List<Double> input, final double multiplierIQR) {
input.removeAll(getOutliers(input, multiplierIQR));
public static List<Double> removeOutliers(final List<Double> input, final double multiplierIQR) {
final List<Double> values = new ArrayList<>();
if (input.size()
<= 1) {
return values;
}
final double[] boundaries = getLowerAndUpperBoundaries(input, multiplierIQR);
final double lowerBound = boundaries[0];
final double upperBound = boundaries[1];

for (final Double value : input) {
if (value
>= lowerBound
&& value
<= upperBound) {
values.add(value);
}
}

return values;
}

/**
* @param input
* @param multiplierIQR
* Detects outliers in given array list of input values and returns them. <br>
* Here, outliers are those which are outside of a calculated lower and upper bound (whisker).
* The interquartile range (IQR) of the input values is therefore multiplied with a given value
* for whisker creation.
*
* @return
* @param input list of values to process
* @param multiplierIQR multiplier for IQR to use for lower and upper bound creation
*
* @return new array list with values outside the generated boundaries
*/
public static List<Double> getOutliers(final List<Double> input, final double multiplierIQR) {
final List<Double> outliers = new ArrayList<>();
if (input.size()
<= 1) {
return outliers;
}
final double[] boundaries = getLowerAndUpperBoundaries(input, multiplierIQR);
final double lowerBound = boundaries[0];
final double upperBound = boundaries[1];
for (final Double value : input) {
if (value
< lowerBound
|| value
> upperBound) {
outliers.add(value);
}
}

return outliers;
}

public static double[] getLowerAndUpperBoundaries(final List<Double> input, final double multiplierIQR) {
Collections.sort(input);
final List<Double> data1 = input.subList(0, input.size()
/ 2);
Expand All @@ -57,18 +96,8 @@ public static List<Double> getOutliers(final List<Double> input, final double mu
final double upperBound = q3
+ multiplierIQR
* iqr;
for (int i = 0; i
< input.size(); i++) {
if ((input.get(i)
< lowerBound)
|| (input.get(i)
> upperBound)) {
outliers.add(input.get(i));
}
}
// System.out.println("input size: " + input.size());
// System.out.println("output size: " + outliers.size());
return outliers;

return new double[]{lowerBound, upperBound};
}

/**
Expand Down

0 comments on commit b2a5de5

Please sign in to comment.