comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java @ 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 846b0441f905
comparison
equal deleted inserted replaced
4794:a7d080347ac3 4795:8ee270a3ef25
1 package de.intevation.flys.artifacts.math; 1 package de.intevation.flys.artifacts.math;
2 2
3 import java.util.List; 3 import java.util.List;
4 4
5 import org.apache.commons.math.MathException;
6
7 import org.apache.commons.math.distribution.TDistributionImpl;
8
9 import org.apache.commons.math.stat.descriptive.moment.Mean;
10 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; 5 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
11 6
12 import org.apache.log4j.Logger; 7 import org.apache.log4j.Logger;
13 8
14 import de.intevation.flys.artifacts.model.sq.SQ;
15
16 public class StdDevOutlier 9 public class StdDevOutlier
17 { 10 {
18 public static final double EPSILON = 1e-5;
19
20 public static final double DEFAULT_FACTOR = 3; 11 public static final double DEFAULT_FACTOR = 3;
21 12
22 private static Logger log = Logger.getLogger(StdDevOutlier.class); 13 private static Logger log = Logger.getLogger(StdDevOutlier.class);
23 14
24 protected StdDevOutlier() { 15 protected StdDevOutlier() {
26 17
27 public static Integer findOutlier(List<Double> values) { 18 public static Integer findOutlier(List<Double> values) {
28 return findOutlier(values, DEFAULT_FACTOR, null); 19 return findOutlier(values, DEFAULT_FACTOR, null);
29 } 20 }
30 21
31 public static Integer findOutlier(List<Double> values, double factor, double[] stdDevResult) { 22 public static Integer findOutlier(
23 List<Double> values,
24 double factor,
25 double [] stdDevResult
26 ) {
32 boolean debug = log.isDebugEnabled(); 27 boolean debug = log.isDebugEnabled();
33 28
34 if (debug) { 29 if (debug) {
35 log.debug("factor for std dev: " + factor); 30 log.debug("factor for std dev: " + factor);
36 } 31 }
39 34
40 if (debug) { 35 if (debug) {
41 log.debug("Values to check: " + N); 36 log.debug("Values to check: " + N);
42 } 37 }
43 38
44 if (values.size() < 3) { 39 if (N < 3) {
45 return null; 40 return null;
46 } 41 }
47 42
48 StandardDeviation stdDev = new StandardDeviation(); 43 StandardDeviation stdDev = new StandardDeviation();
49 44
50 double maxValue = -Double.MAX_VALUE; 45 double maxValue = -Double.MAX_VALUE;
51 int maxIndex = -1; 46 int maxIndex = -1;
52 int ndx = 0; 47 for (int i = N-1; i >= 0; --i) {
53 for (int i = values.size()-1; i >= 0; --i) {
54 double value = Math.abs(values.get(i)); 48 double value = Math.abs(values.get(i));
55 stdDev.increment(value); 49 stdDev.increment(value);
56 if (value > maxValue) { 50 if (value > maxValue) {
57 maxValue = value; 51 maxValue = value;
58 maxIndex = ndx; 52 maxIndex = i;
59 } 53 }
60 ++ndx;
61 } 54 }
62 55
63 double sd = stdDev.getResult(); 56 double sd = stdDev.getResult();
64 57
65 double accepted = factor * sd; 58 double accepted = factor * sd;
67 if (debug) { 60 if (debug) {
68 log.debug("std dev: " + stdDev); 61 log.debug("std dev: " + stdDev);
69 log.debug("accepted: " + accepted); 62 log.debug("accepted: " + accepted);
70 log.debug("max value: " + maxValue); 63 log.debug("max value: " + maxValue);
71 } 64 }
65
72 if (stdDevResult != null) { 66 if (stdDevResult != null) {
73 stdDevResult[0] = sd; 67 stdDevResult[0] = sd;
74 } 68 }
69
75 return maxValue > accepted ? maxIndex : null; 70 return maxValue > accepted ? maxIndex : null;
76 } 71 }
77 } 72 }
78 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 73 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org