diff 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
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Outlier.java	Mon Jun 04 10:13:20 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Outlier.java	Mon Jun 04 16:44:56 2012 +0000
@@ -7,6 +7,7 @@
 
 import org.apache.commons.math.distribution.TDistributionImpl;
 
+import java.util.Collections;
 import java.util.List;
 import java.util.ArrayList;
 
@@ -14,9 +15,13 @@
 
 public class Outlier
 {
+    public static final double DEFAULT_ALPHA = 0.05;
+
     private static Logger log = Logger.getLogger(Outlier.class);
 
-    public static class IndexedValue {
+    public static class IndexedValue
+    implements          Comparable<IndexedValue>
+    {
         protected int    index;
         protected double value;
 
@@ -43,12 +48,60 @@
         public void setValue(double value) {
             this.value = value;
         }
+
+        @Override
+        public int compareTo(IndexedValue other) {
+            int diff = index - other.index;
+            if (index < 0) return -1;
+            return index > 0 ? +1 : 0;
+        }
     } // class IndexedValue
 
+    public static class Outliers {
+
+        protected List<IndexedValue> retained;
+        protected List<IndexedValue> removed;
+
+        public Outliers() {
+        }
+
+        public Outliers(
+            List<IndexedValue> retained,
+            List<IndexedValue> removed
+        ) {
+            this.retained = retained;
+            this.removed  = removed;
+        }
+
+        public boolean hasOutliers() {
+            return !removed.isEmpty();
+        }
+
+        public List<IndexedValue> getRetained() {
+            return retained;
+        }
+
+        public void setRetained(List<IndexedValue> retained) {
+            this.retained = retained;
+        }
+
+        public List<IndexedValue> getRemoved() {
+            return removed;
+        }
+
+        public void setRemoved(List<IndexedValue> removed) {
+            this.removed = removed;
+        }
+    } // class Outliers
+
     public Outlier() {
     }
 
-    public static List<IndexedValue> findOutliers(
+    public static Outliers findOutliers(List<IndexedValue> inputValues) {
+        return findOutliers(inputValues, DEFAULT_ALPHA);
+    }
+
+    public static Outliers findOutliers(
         List<IndexedValue> inputValues,
         double             alpha
     ) {
@@ -69,7 +122,7 @@
 
             for (IndexedValue value: values) {
                 mean.increment(value.getValue());
-                std.increment(value.getValue());
+                std .increment(value.getValue());
             }
 
             double m = mean.getResult();
@@ -98,14 +151,18 @@
                     outliers.add(values.get(iv));
                     values.remove(iv);
                 }
+                else {
+                    break;
+                }
             }
             catch (MathException me) {
                 log.error(me);
             }
         }
 
+        Collections.sort(outliers);
 
-        return outliers;
+        return new Outliers(values, outliers);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org