comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Outlier.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/model/sq/Outlier.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.model.sq;
2
3 import org.dive4elements.river.artifacts.math.GrubbsOutlier;
4 import org.dive4elements.river.artifacts.math.StdDevOutlier;
5
6 import java.util.ArrayList;
7 import java.util.List;
8
9 import org.apache.commons.math.MathException;
10
11 import org.apache.log4j.Logger;
12
13 public class Outlier
14 {
15 private static Logger log = Logger.getLogger(Outlier.class);
16
17 private static final String GRUBBS = "outlier.method.grubbs";
18
19 //private static final String STD_DEV = "std-dev";
20
21 public interface Callback {
22
23 void initialize(List<SQ> sqs) throws MathException;
24
25 double eval(SQ sq);
26
27 void iterationFinished(
28 double stdDev,
29 SQ outlier,
30 List<SQ> remaining);
31
32 } // interface Callback
33
34 public static void detectOutliers(
35 Callback callback,
36 List<SQ> sqs,
37 double stdDevFactor,
38 String method
39 )
40 throws MathException
41 {
42 boolean debug = log.isDebugEnabled();
43
44 if (method == null) {
45 method = "std-dev";
46 }
47
48 if (debug) {
49 log.debug("stdDevFactor: " + stdDevFactor);
50 log.debug("method: " + method);
51 }
52
53 List<SQ> data = new ArrayList<SQ>(sqs);
54
55 double [] stdDev = new double[1];
56
57 boolean useGrubbs = method.equals(GRUBBS);
58
59 if (useGrubbs) {
60 stdDevFactor = Math.max(0d, Math.min(stdDevFactor/100d, 1d));
61 }
62
63 List<Double> values = new ArrayList<Double>(data.size());
64
65 while (data.size() > 2) {
66
67 callback.initialize(data);
68
69 for (SQ sq: data) {
70 values.add(callback.eval(sq));
71 }
72
73 Integer ndx = useGrubbs
74 ? GrubbsOutlier.findOutlier(values, stdDevFactor, stdDev)
75 : StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);
76
77 if (ndx == null) {
78 callback.iterationFinished(stdDev[0], null, data);
79 break;
80 }
81
82 SQ outlier = data.remove(ndx.intValue());
83 if (debug) {
84 log.debug("stdDev: " + stdDev[0]);
85 log.debug("removed " + ndx +
86 "; S: " + outlier.getS() + " Q: " + outlier.getQ());
87 }
88 callback.iterationFinished(stdDev[0], outlier, data);
89 values.clear();
90 }
91 }
92 }
93 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org