view flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/Outlier.java @ 2792:fe987587ebc9

Merged revisions 4539-4540,4543,4545-4546 via svnmerge from file:///home/clients/bsh/bsh-generischer-viewer/Material/SVN/flys-artifacts/trunk ........ r4539 | teichmann | 2012-05-27 20:02:13 +0200 (So, 27 Mai 2012) | 1 line FixA: Added forgotten csv/report facets/generators to conf. ........ r4540 | teichmann | 2012-05-27 20:11:31 +0200 (So, 27 Mai 2012) | 1 line FixA: Fixed class cast bug in report facet. ........ r4543 | teichmann | 2012-05-28 20:35:01 +0200 (Mo, 28 Mai 2012) | 1 line FixA: Added facet to return delta w/t as CSV ........ r4545 | teichmann | 2012-05-28 22:59:27 +0200 (Mo, 28 Mai 2012) | 1 line FixA: Made Delta W/t calculation finally work ........ r4546 | teichmann | 2012-05-28 23:34:24 +0200 (Mo, 28 Mai 2012) | 1 line FixA: corrected fitting (Q->W instead W->Q). ........ flys-artifacts/tags/2.7@4547 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 29 May 2012 04:58:29 +0000
parents c11da3540b70
children ab81ffd1343e
line wrap: on
line source
package de.intevation.flys.artifacts.math;

import org.apache.commons.math.MathException;

import org.apache.commons.math.stat.descriptive.moment.Mean;
import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;

import org.apache.commons.math.distribution.TDistributionImpl;

import java.util.List;
import java.util.ArrayList;

import org.apache.log4j.Logger;

public class Outlier
{
    private static Logger log = Logger.getLogger(Outlier.class);

    public static class IndexedValue {
        protected int    index;
        protected double value;

        public IndexedValue() {
        }

        public IndexedValue(int index, double value) {
            this.index = index;
            this.value = value;
        }

        public int getIndex() {
            return index;
        }

        public void setIndex(int index) {
            this.index = index;
        }

        public double getValue() {
            return value;
        }

        public void setValue(double value) {
            this.value = value;
        }
    } // class IndexedValue

    public Outlier() {
    }

    public static List<IndexedValue> findOutliers(
        List<IndexedValue> inputValues,
        double             alpha
    ) {
        ArrayList<IndexedValue> outliers = new ArrayList<IndexedValue>();

        ArrayList<IndexedValue> values =
            new ArrayList<IndexedValue>(inputValues);

        for (;;) {
            int N = values.size();

            if (N < 4) {
                break;
            }

            Mean mean = new Mean();
            StandardDeviation std = new StandardDeviation();

            for (IndexedValue value: values) {
                mean.increment(value.getValue());
                std.increment(value.getValue());
            }

            double m = mean.getResult();
            double s = std.getResult();

            double maxZ = -Double.MAX_VALUE;
            int iv = -1;
            for (int i = N-1; i >= 0; --i) {
                IndexedValue v = values.get(i);
                double z = Math.abs(m - v.getValue())/s;
                if (z > maxZ) {
                    maxZ = z;
                    iv = i;
                }
            }

            double t = Math.sqrt((N*(N-2)*maxZ*maxZ)
                /((N-1)*(N-1) - N*maxZ*maxZ));

            TDistributionImpl tdist = new TDistributionImpl(N-2);

            try {
                double p = tdist.cumulativeProbability(t);

                if (p < alpha) {
                    outliers.add(values.get(iv));
                    values.remove(iv);
                }
            }
            catch (MathException me) {
                log.error(me);
            }
        }


        return outliers;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org