comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Outlier.java @ 3011:ab81ffd1343e

FixA: Reactivated rewrite of the outlier checks. flys-artifacts/trunk@4576 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 04 Jun 2012 16:44:56 +0000
parents c11da3540b70
children e01b9d1bc941
comparison
equal deleted inserted replaced
3010:05a3fe8800b3 3011:ab81ffd1343e
5 import org.apache.commons.math.stat.descriptive.moment.Mean; 5 import org.apache.commons.math.stat.descriptive.moment.Mean;
6 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; 6 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
7 7
8 import org.apache.commons.math.distribution.TDistributionImpl; 8 import org.apache.commons.math.distribution.TDistributionImpl;
9 9
10 import java.util.Collections;
10 import java.util.List; 11 import java.util.List;
11 import java.util.ArrayList; 12 import java.util.ArrayList;
12 13
13 import org.apache.log4j.Logger; 14 import org.apache.log4j.Logger;
14 15
15 public class Outlier 16 public class Outlier
16 { 17 {
18 public static final double DEFAULT_ALPHA = 0.05;
19
17 private static Logger log = Logger.getLogger(Outlier.class); 20 private static Logger log = Logger.getLogger(Outlier.class);
18 21
19 public static class IndexedValue { 22 public static class IndexedValue
23 implements Comparable<IndexedValue>
24 {
20 protected int index; 25 protected int index;
21 protected double value; 26 protected double value;
22 27
23 public IndexedValue() { 28 public IndexedValue() {
24 } 29 }
41 } 46 }
42 47
43 public void setValue(double value) { 48 public void setValue(double value) {
44 this.value = value; 49 this.value = value;
45 } 50 }
51
52 @Override
53 public int compareTo(IndexedValue other) {
54 int diff = index - other.index;
55 if (index < 0) return -1;
56 return index > 0 ? +1 : 0;
57 }
46 } // class IndexedValue 58 } // class IndexedValue
59
60 public static class Outliers {
61
62 protected List<IndexedValue> retained;
63 protected List<IndexedValue> removed;
64
65 public Outliers() {
66 }
67
68 public Outliers(
69 List<IndexedValue> retained,
70 List<IndexedValue> removed
71 ) {
72 this.retained = retained;
73 this.removed = removed;
74 }
75
76 public boolean hasOutliers() {
77 return !removed.isEmpty();
78 }
79
80 public List<IndexedValue> getRetained() {
81 return retained;
82 }
83
84 public void setRetained(List<IndexedValue> retained) {
85 this.retained = retained;
86 }
87
88 public List<IndexedValue> getRemoved() {
89 return removed;
90 }
91
92 public void setRemoved(List<IndexedValue> removed) {
93 this.removed = removed;
94 }
95 } // class Outliers
47 96
48 public Outlier() { 97 public Outlier() {
49 } 98 }
50 99
51 public static List<IndexedValue> findOutliers( 100 public static Outliers findOutliers(List<IndexedValue> inputValues) {
101 return findOutliers(inputValues, DEFAULT_ALPHA);
102 }
103
104 public static Outliers findOutliers(
52 List<IndexedValue> inputValues, 105 List<IndexedValue> inputValues,
53 double alpha 106 double alpha
54 ) { 107 ) {
55 ArrayList<IndexedValue> outliers = new ArrayList<IndexedValue>(); 108 ArrayList<IndexedValue> outliers = new ArrayList<IndexedValue>();
56 109
67 Mean mean = new Mean(); 120 Mean mean = new Mean();
68 StandardDeviation std = new StandardDeviation(); 121 StandardDeviation std = new StandardDeviation();
69 122
70 for (IndexedValue value: values) { 123 for (IndexedValue value: values) {
71 mean.increment(value.getValue()); 124 mean.increment(value.getValue());
72 std.increment(value.getValue()); 125 std .increment(value.getValue());
73 } 126 }
74 127
75 double m = mean.getResult(); 128 double m = mean.getResult();
76 double s = std.getResult(); 129 double s = std.getResult();
77 130
96 149
97 if (p < alpha) { 150 if (p < alpha) {
98 outliers.add(values.get(iv)); 151 outliers.add(values.get(iv));
99 values.remove(iv); 152 values.remove(iv);
100 } 153 }
154 else {
155 break;
156 }
101 } 157 }
102 catch (MathException me) { 158 catch (MathException me) {
103 log.error(me); 159 log.error(me);
104 } 160 }
105 } 161 }
106 162
163 Collections.sort(outliers);
107 164
108 return outliers; 165 return new Outliers(values, outliers);
109 } 166 }
110 } 167 }
111 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 168 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org