diff artifacts/src/main/java/org/dive4elements/river/artifacts/math/StdDevOutlier.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/math/StdDevOutlier.java@bd047b71ab37
children 4897a58c8746
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/math/StdDevOutlier.java	Thu Apr 25 15:23:37 2013 +0200
@@ -0,0 +1,73 @@
+package org.dive4elements.river.artifacts.math;
+
+import java.util.List;
+
+import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
+
+import org.apache.log4j.Logger;
+
+public class StdDevOutlier
+{
+    public static final double DEFAULT_FACTOR = 3;
+
+    private static Logger log = Logger.getLogger(StdDevOutlier.class);
+
+    protected StdDevOutlier() {
+    }
+
+    public static Integer findOutlier(List<Double> values) {
+        return findOutlier(values, DEFAULT_FACTOR, null);
+    }
+
+    public static Integer findOutlier(
+        List<Double> values,
+        double       factor,
+        double []    stdDevResult
+    ) {
+        boolean debug = log.isDebugEnabled();
+
+        if (debug) {
+            log.debug("factor for std dev: " + factor);
+        }
+
+        int N = values.size();
+
+        if (debug) {
+            log.debug("Values to check: " + N);
+        }
+
+        if (N < 3) {
+            return null;
+        }
+
+        StandardDeviation stdDev = new StandardDeviation();
+
+        double maxValue = -Double.MAX_VALUE;
+        int    maxIndex = -1;
+        for (int i = N-1; i >= 0; --i) {
+            double value = Math.abs(values.get(i));
+            stdDev.increment(value);
+            if (value > maxValue) {
+                maxValue = value;
+                maxIndex = i;
+            }
+        }
+
+        double sd = stdDev.getResult();
+
+        double accepted = factor * sd;
+
+        if (debug) {
+            log.debug("std dev: " + stdDev);
+            log.debug("accepted: " + accepted);
+            log.debug("max value: " + maxValue);
+        }
+
+        if (stdDevResult != null) {
+            stdDevResult[0] = sd;
+        }
+
+        return maxValue > accepted ? maxIndex : null;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org