comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Outlier.java @ 2645:4f7d1ea38404

Added simple Grubb's outlier test. flys-artifacts/trunk@4300 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 25 Apr 2012 15:57:23 +0000
parents
children c11da3540b70
comparison
equal deleted inserted replaced
2644:0a84313efe60 2645:4f7d1ea38404
1 package de.intevation.flys.artifacts.math;
2
3 import org.apache.commons.math.stat.descriptive.moment.Mean;
4 import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
5
6 import org.apache.commons.math.distribution.TDistributionImpl;
7
8 import java.util.List;
9 import java.util.ArrayList;
10
11 public class Outlier
12 {
13 public static class IndexedValue {
14 protected int index;
15 protected double value;
16
17 public IndexedValue() {
18 }
19
20 public IndexedValue(int index, double value) {
21 this.index = index;
22 this.value = value;
23 }
24
25 public int getIndex() {
26 return index;
27 }
28
29 public void setIndex(int index) {
30 this.index = index;
31 }
32
33 public double getValue() {
34 return value;
35 }
36
37 public void setValue(double value) {
38 this.value = value;
39 }
40 } // class IndexedValue
41
42 public Outlier() {
43 }
44
45 public static List<IndexedValue> findOutliers(
46 List<IndexedValue> inputValues,
47 double alpha
48 ) {
49 ArrayList<IndexedValue> outliers = new ArrayList<IndexedValue>();
50
51 ArrayList<IndexedValue> values =
52 new ArrayList<IndexedValue>(inputValues);
53
54 for (;;) {
55 int N = values.size();
56
57 if (N < 4) {
58 break;
59 }
60
61 Mean mean = new Mean();
62 StandardDeviation std = new StandardDeviation();
63
64 for (IndexedValue value: values) {
65 mean.increment(value.getValue());
66 std.increment(value.getValue());
67 }
68
69 double m = mean.getResult();
70 double s = std.getResult();
71
72 double maxZ = -Double.MAX_VALUE;
73 int iv = -1;
74 for (int i >= 0; i = N-1; --i) {
75 IndexedValue v = values.get(i);
76 double z = Math.abs(m - v.getValue())/s;
77 if (z > maxZ) {
78 maxZ = z;
79 iv = i;
80 }
81 }
82
83 double t = Math.sqrt((N*(N-2)*z*z)/((N-1)*(N-1) - N*z*z))
84
85 TDistributionImpl tdist = new TDistributionImpl(N-2);
86
87 double p = tdist.cumulativeProbability(t)
88 }
89
90
91 return outliers;
92 }
93 }
94 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org