comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java @ 4794:a7d080347ac3

MINFO: Allow two methods for outlier test in SQ relation. * Methods can be switched as option in conf.xml. * Methods: - Find outliers via multiples of the standard deviation. - Grubbs (used in Fix-Analysis)
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 11 Jan 2013 13:57:38 +0100
parents b8b1280606c2
children 8ee270a3ef25
comparison
equal deleted inserted replaced
4793:c0d6391bec6f 4794:a7d080347ac3
7 7
8 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; 8 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
9 9
10 import org.apache.log4j.Logger; 10 import org.apache.log4j.Logger;
11 11
12 import de.intevation.artifacts.GlobalContext;
13 import de.intevation.artifacts.common.utils.Config;
14 import de.intevation.flys.artifacts.context.FLYSContext;
15 import de.intevation.flys.artifacts.math.GrubbsOutlier;
16 import de.intevation.flys.artifacts.math.StdDevOutlier;
17
12 public class Outlier 18 public class Outlier
13 { 19 {
14 private static Logger log = Logger.getLogger(Outlier.class); 20 private static Logger log = Logger.getLogger(Outlier.class);
21
22 private static final String OUTLIER_METHOD =
23 "/artifact-database/options/minfo-sq/outlier-method/@name";
24
25 private static final String GRUBBS = "grubbs";
26
27 private static final String STD_DEV = "std-dev";
15 28
16 public interface Callback { 29 public interface Callback {
17 30
18 void initialize(List<SQ> sqs) throws MathException; 31 void initialize(List<SQ> sqs) throws MathException;
19 32
36 boolean debug = log.isDebugEnabled(); 49 boolean debug = log.isDebugEnabled();
37 50
38 if (debug) { 51 if (debug) {
39 log.debug("stdDevFactor: " + stdDevFactor); 52 log.debug("stdDevFactor: " + stdDevFactor);
40 } 53 }
41 54 String method = Config.getStringXPath(OUTLIER_METHOD);
55 log.debug("method: " + method);
56 if (method == null) {
57 method = "std-dev";
58 }
42 List<SQ> data = new ArrayList<SQ>(sqs); 59 List<SQ> data = new ArrayList<SQ>(sqs);
43 60
44 while (data.size() > 2) { 61 while (data.size() > 2) {
45 62
46 callback.initialize(data); 63 callback.initialize(data);
47 64
48 StandardDeviation stdDev = new StandardDeviation(); 65 List<Double> values = new ArrayList<Double>();
49 66 for (SQ sq: data) {
50 double maxValue = -Double.MAX_VALUE; 67 values.add(callback.eval(sq));
51 int maxIndex = -1;
52
53 for (int i = data.size()-1; i >= 0; --i) {
54 double value = Math.abs(callback.eval(data.get(i)));
55 stdDev.increment(value);
56 if (value > maxValue) {
57 maxValue = value;
58 maxIndex = i;
59 }
60 } 68 }
61 69
62 double sd = stdDev.getResult(); 70 Integer ndx = null;
63 71 double[] stdDev = new double[1];
64 double accepted = stdDevFactor * sd; 72 if (method.equals(GRUBBS)) {
65 73 ndx = GrubbsOutlier.findOutlier(values, stdDevFactor/100, stdDev);
66 if (debug) { 74 }
67 log.debug("std dev: " + stdDev); 75 else {
68 log.debug("accepted: " + accepted); 76 ndx = StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
69 log.debug("max value: " + maxValue); 77 }
78 if (ndx == null) {
79 callback.iterationFinished(stdDev[0], null, data);
80 break;
70 } 81 }
71 82
72 SQ outlier = maxValue > accepted 83 SQ outlier = data.remove((int)ndx);
73 ? data.remove(maxIndex) 84 log.debug("stdDev: " + stdDev[0]);
74 : null; 85 log.debug("removed " + ndx + "; S: " + outlier.getS() + " Q: " + outlier.getQ());
75 86 callback.iterationFinished(stdDev[0], outlier, data);
76 callback.iterationFinished(sd, outlier, data);
77
78 if (outlier == null) {
79 break;
80 }
81 } 87 }
82 } 88 }
83 } 89 }
84 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 90 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org