Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/math/StdDevOutlier.java @ 5863:4897a58c8746
River artifacts: Added new copyright headers.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Sun, 28 Apr 2013 14:40:59 +0200 |
parents | 5aa05a7a34b7 |
children | af13ceeba52a |
rev | line source |
---|---|
5863
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
2 * Software engineering by Intevation GmbH |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
3 * |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
4 * This file is Free Software under the GNU AGPL (>=v3) |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
6 * documentation coming with Dive4Elements River for details. |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
7 */ |
4897a58c8746
River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
5838
diff
changeset
|
8 |
5831
bd047b71ab37
Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4816
diff
changeset
|
9 package org.dive4elements.river.artifacts.math; |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
10 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
11 import java.util.List; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
12 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
13 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
14 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
15 import org.apache.log4j.Logger; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
16 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
17 public class StdDevOutlier |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
18 { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
19 public static final double DEFAULT_FACTOR = 3; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
20 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
21 private static Logger log = Logger.getLogger(StdDevOutlier.class); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
22 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
23 protected StdDevOutlier() { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
24 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
25 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
26 public static Integer findOutlier(List<Double> values) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
27 return findOutlier(values, DEFAULT_FACTOR, null); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
28 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
29 |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
30 public static Integer findOutlier( |
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
31 List<Double> values, |
4816
846b0441f905
Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4795
diff
changeset
|
32 double factor, |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
33 double [] stdDevResult |
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
34 ) { |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
35 boolean debug = log.isDebugEnabled(); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
36 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
37 if (debug) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
38 log.debug("factor for std dev: " + factor); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
39 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
40 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
41 int N = values.size(); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
42 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
43 if (debug) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
44 log.debug("Values to check: " + N); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
45 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
46 |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
47 if (N < 3) { |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
48 return null; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
49 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
50 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
51 StandardDeviation stdDev = new StandardDeviation(); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
52 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
53 double maxValue = -Double.MAX_VALUE; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
54 int maxIndex = -1; |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
55 for (int i = N-1; i >= 0; --i) { |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
56 double value = Math.abs(values.get(i)); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
57 stdDev.increment(value); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
58 if (value > maxValue) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
59 maxValue = value; |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
60 maxIndex = i; |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
61 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
62 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
63 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
64 double sd = stdDev.getResult(); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
65 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
66 double accepted = factor * sd; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
67 |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
68 if (debug) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
69 log.debug("std dev: " + stdDev); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
70 log.debug("accepted: " + accepted); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
71 log.debug("max value: " + maxValue); |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
72 } |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
73 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
74 if (stdDevResult != null) { |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
75 stdDevResult[0] = sd; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
76 } |
4795
8ee270a3ef25
Small code cleanups in S/Q outlier tests.
Sascha L. Teichmann <teichmann@intevation.de>
parents:
4794
diff
changeset
|
77 |
4794
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
78 return maxValue > accepted ? maxIndex : null; |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
79 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
80 } |
a7d080347ac3
MINFO: Allow two methods for outlier test in SQ relation.
Raimund Renkert <rrenkert@intevation.de>
parents:
diff
changeset
|
81 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |