changeset 4795:8ee270a3ef25

Small code cleanups in S/Q outlier tests.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 11 Jan 2013 22:16:22 +0100
parents a7d080347ac3
children 729a5edb0313
files flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java
diffstat 2 files changed, 44 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java	Fri Jan 11 13:57:38 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java	Fri Jan 11 22:16:22 2013 +0100
@@ -2,21 +2,12 @@
 
 import java.util.List;
 
-import org.apache.commons.math.MathException;
-
-import org.apache.commons.math.distribution.TDistributionImpl;
-
-import org.apache.commons.math.stat.descriptive.moment.Mean;
 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
 
 import org.apache.log4j.Logger;
 
-import de.intevation.flys.artifacts.model.sq.SQ;
-
 public class StdDevOutlier
 {
-    public static final double EPSILON = 1e-5;
-
     public static final double DEFAULT_FACTOR = 3;
 
     private static Logger log = Logger.getLogger(StdDevOutlier.class);
@@ -28,7 +19,11 @@
         return findOutlier(values, DEFAULT_FACTOR, null);
     }
 
-    public static Integer findOutlier(List<Double> values, double factor, double[] stdDevResult) {
+    public static Integer findOutlier(
+        List<Double> values,
+        double       factor, 
+        double []    stdDevResult
+    ) {
         boolean debug = log.isDebugEnabled();
 
         if (debug) {
@@ -41,7 +36,7 @@
             log.debug("Values to check: " + N);
         }
 
-        if (values.size() < 3) {
+        if (N < 3) {
             return null;
         }
 
@@ -49,15 +44,13 @@
 
         double maxValue = -Double.MAX_VALUE;
         int    maxIndex = -1;
-        int    ndx = 0;
-        for (int i = values.size()-1; i >= 0; --i) {
+        for (int i = N-1; i >= 0; --i) {
             double value = Math.abs(values.get(i));
             stdDev.increment(value);
             if (value > maxValue) {
                 maxValue = value;
-                maxIndex = ndx;
+                maxIndex = i;
             }
-            ++ndx;
         }
 
         double sd = stdDev.getResult();
@@ -69,9 +62,11 @@
             log.debug("accepted: " + accepted);
             log.debug("max value: " + maxValue);
         }
+
         if (stdDevResult != null) {
             stdDevResult[0] = sd;
         }
+
         return maxValue > accepted ? maxIndex : null;
     }
 }
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java	Fri Jan 11 13:57:38 2013 +0100
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java	Fri Jan 11 22:16:22 2013 +0100
@@ -1,20 +1,17 @@
 package de.intevation.flys.artifacts.model.sq;
 
+import de.intevation.artifacts.common.utils.Config;
+
+import de.intevation.flys.artifacts.math.GrubbsOutlier;
+import de.intevation.flys.artifacts.math.StdDevOutlier;
+
 import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.commons.math.MathException;
 
-import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
-
 import org.apache.log4j.Logger;
 
-import de.intevation.artifacts.GlobalContext;
-import de.intevation.artifacts.common.utils.Config;
-import de.intevation.flys.artifacts.context.FLYSContext;
-import de.intevation.flys.artifacts.math.GrubbsOutlier;
-import de.intevation.flys.artifacts.math.StdDevOutlier;
-
 public class Outlier
 {
     private static Logger log = Logger.getLogger(Outlier.class);
@@ -24,7 +21,7 @@
 
     private static final String GRUBBS = "grubbs";
 
-    private static final String STD_DEV = "std-dev";
+    //private static final String STD_DEV = "std-dev";
 
     public interface Callback {
 
@@ -48,42 +45,54 @@
     {
         boolean debug = log.isDebugEnabled();
 
-        if (debug) {
-            log.debug("stdDevFactor: " + stdDevFactor);
-        }
         String method = Config.getStringXPath(OUTLIER_METHOD);
-        log.debug("method: " + method);
+
         if (method == null) {
             method = "std-dev";
         }
+
+        if (debug) {
+            log.debug("stdDevFactor: " + stdDevFactor);
+            log.debug("method: " + method);
+        }
+
         List<SQ> data = new ArrayList<SQ>(sqs);
 
+        double [] stdDev = new double[1];
+
+        boolean useGrubbs = method.equals(GRUBBS);
+
+        if (useGrubbs) {
+            stdDevFactor = Math.max(0d, Math.min(stdDevFactor/100d, 1d));
+        }
+
+        List<Double> values = new ArrayList<Double>(data.size());
+
         while (data.size() > 2) {
 
             callback.initialize(data);
 
-            List<Double> values = new ArrayList<Double>();
             for (SQ sq: data) {
                 values.add(callback.eval(sq));
             }
 
-            Integer ndx = null;
-            double[] stdDev = new double[1];
-            if (method.equals(GRUBBS)) {
-                ndx = GrubbsOutlier.findOutlier(values, stdDevFactor/100, stdDev);
-            }
-            else {
-                ndx = StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
-            }
+            Integer ndx = useGrubbs
+                ? GrubbsOutlier.findOutlier(values, stdDevFactor, stdDev)
+                : StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
+
             if (ndx == null) {
                 callback.iterationFinished(stdDev[0], null, data);
                 break;
             }
 
-            SQ outlier = data.remove((int)ndx);
-            log.debug("stdDev: " + stdDev[0]);
-            log.debug("removed " + ndx + "; S: " + outlier.getS() + " Q: " + outlier.getQ());
+            SQ outlier = data.remove(ndx.intValue());
+            if (debug) {
+                log.debug("stdDev: " + stdDev[0]);
+                log.debug("removed " + ndx + 
+                    "; S: " + outlier.getS() + " Q: " + outlier.getQ());
+            }
             callback.iterationFinished(stdDev[0], outlier, data);
+            values.clear();
         }
     }
 }

http://dive4elements.wald.intevation.org