Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java @ 3566:8d0f06b76e09
S/Q relation: Fixed flys/issue748
flys-artifacts/trunk@5164 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 01 Aug 2012 10:19:45 +0000 |
parents | 1df6984628c3 |
children | b8b1280606c2 |
line wrap: on
line diff
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java Tue Jul 31 16:46:36 2012 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java Wed Aug 01 10:19:45 2012 +0000 @@ -14,33 +14,19 @@ { private static Logger log = Logger.getLogger(Outlier.class); - public static final int MAX_ITERATIONS = 1000; - public interface Callback { - void initialize(Iterator<SQ> good) throws MathException; + void initialize(List<SQ> sqs) throws MathException; double eval(SQ sq); - void outlier(SQ sq); - - void remaining(SQ sq); - - void standardDeviation(double stdDev); - - void iterationFinished(); + void iterationFinished( + double stdDev, + SQ outlier, + List<SQ> remaining); } // interface Callback - private static final class EvalSQ { - protected SQ sq; - protected double value; - - public EvalSQ(SQ sq) { - this.sq = sq; - } - } // class EvalSQ - public static void detectOutliers( Callback callback, List<SQ> sqs, @@ -54,79 +40,46 @@ log.debug("stdDevFactor: " + stdDevFactor); } - List<EvalSQ> data = new ArrayList<EvalSQ>(sqs.size()); + List<SQ> data = new ArrayList<SQ>(sqs); - for (SQ sq: sqs) { - data.add(new EvalSQ(sq)); - } + while (data.size() > 2) { - List<EvalSQ> good = new ArrayList<EvalSQ>(sqs.size()); - - for (int i = 0; i < MAX_ITERATIONS && data.size() > 2; ++i) { - - callback.initialize(asSQIterator(data)); + callback.initialize(data); StandardDeviation stdDev = new StandardDeviation(); - for (EvalSQ esq: data) { - stdDev.increment(esq.value = callback.eval(esq.sq)); + double maxValue = -Double.MAX_VALUE; + int maxIndex = -1; + + for (int i = data.size()-1; i >= 0; --i) { + double value = Math.abs(callback.eval(data.get(i))); + stdDev.increment(value); + if (value > maxValue) { + maxValue = value; + maxIndex = i; + } } double sd = stdDev.getResult(); - callback.standardDeviation(sd); - double accepted = stdDevFactor * sd; if (debug) { + log.debug("std dev: " + stdDev); log.debug("accepted: " + accepted); - } - - for (EvalSQ esq: data) { - if (debug) { - log.debug(" value: " + Math.abs(esq.value)); - } - - if (Math.abs(esq.value) > accepted) { - callback.outlier(esq.sq); - } - else { - callback.remaining(esq.sq); - good.add(esq); - } - } - - callback.iterationFinished(); - - if (good.size() == data.size()) { - break; + log.debug("max value: " + maxValue); } - List<EvalSQ> tmp = good; - good = data; - data = tmp; - good.clear(); - } - } + SQ outlier = maxValue > accepted + ? data.remove(maxIndex) + : null; - protected static Iterator<SQ> asSQIterator(List<EvalSQ> esqs) { - final Iterator<EvalSQ> parent = esqs.iterator(); - return new Iterator<SQ>() { - @Override - public boolean hasNext() { - return parent.hasNext(); - } + callback.iterationFinished(sd, outlier, data); - @Override - public SQ next() { - return parent.next().sq; + if (outlier == null) { + break; } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :