@@ -17,22 +17,61 @@ public class Statistics {
17
17
*
18
18
* @return new array list without values outside the generated boundaries
19
19
*/
20
- public static void removeOutliers (final List <Double > input , final double multiplierIQR ) {
21
- input .removeAll (getOutliers (input , multiplierIQR ));
20
+ public static List <Double > removeOutliers (final List <Double > input , final double multiplierIQR ) {
21
+ final List <Double > values = new ArrayList <>();
22
+ if (input .size ()
23
+ <= 1 ) {
24
+ return values ;
25
+ }
26
+ final double [] boundaries = getLowerAndUpperBoundaries (input , multiplierIQR );
27
+ final double lowerBound = boundaries [0 ];
28
+ final double upperBound = boundaries [1 ];
29
+
30
+ for (final Double value : input ) {
31
+ if (value
32
+ >= lowerBound
33
+ && value
34
+ <= upperBound ) {
35
+ values .add (value );
36
+ }
37
+ }
38
+
39
+ return values ;
22
40
}
23
41
24
42
/**
25
- * @param input
26
- * @param multiplierIQR
43
+ * Detects outliers in given array list of input values and returns them. <br>
44
+ * Here, outliers are those which are outside of a calculated lower and upper bound (whisker).
45
+ * The interquartile range (IQR) of the input values is therefore multiplied with a given value
46
+ * for whisker creation.
27
47
*
28
- * @return
48
+ * @param input list of values to process
49
+ * @param multiplierIQR multiplier for IQR to use for lower and upper bound creation
50
+ *
51
+ * @return new array list with values outside the generated boundaries
29
52
*/
30
53
public static List <Double > getOutliers (final List <Double > input , final double multiplierIQR ) {
31
54
final List <Double > outliers = new ArrayList <>();
32
55
if (input .size ()
33
56
<= 1 ) {
34
57
return outliers ;
35
58
}
59
+ final double [] boundaries = getLowerAndUpperBoundaries (input , multiplierIQR );
60
+ final double lowerBound = boundaries [0 ];
61
+ final double upperBound = boundaries [1 ];
62
+ for (final Double value : input ) {
63
+ if (value
64
+ < lowerBound
65
+ || value
66
+ > upperBound ) {
67
+ outliers .add (value );
68
+ }
69
+ }
70
+
71
+ return outliers ;
72
+ }
73
+
74
+ public static double [] getLowerAndUpperBoundaries (final List <Double > input , final double multiplierIQR ) {
36
75
Collections .sort (input );
37
76
final List <Double > data1 = input .subList (0 , input .size ()
38
77
/ 2 );
@@ -57,18 +96,8 @@ public static List<Double> getOutliers(final List<Double> input, final double mu
57
96
final double upperBound = q3
58
97
+ multiplierIQR
59
98
* iqr ;
60
- for (int i = 0 ; i
61
- < input .size (); i ++) {
62
- if ((input .get (i )
63
- < lowerBound )
64
- || (input .get (i )
65
- > upperBound )) {
66
- outliers .add (input .get (i ));
67
- }
68
- }
69
- // System.out.println("input size: " + input.size());
70
- // System.out.println("output size: " + outliers.size());
71
- return outliers ;
99
+
100
+ return new double []{lowerBound , upperBound };
72
101
}
73
102
74
103
/**
0 commit comments