view flys-artifacts/src/main/java/de/intevation/flys/artifacts/math/StdDevOutlier.java @ 4794:a7d080347ac3

MINFO: Allow two methods for outlier test in SQ relation. * Methods can be switched as option in conf.xml. * Methods: - Find outliers via multiples of the standard deviation. - Grubbs (used in Fix-Analysis)
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 11 Jan 2013 13:57:38 +0100
parents
children 8ee270a3ef25
line wrap: on
line source
package de.intevation.flys.artifacts.math;

import java.util.List;

import org.apache.commons.math.MathException;

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

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

import org.apache.log4j.Logger;

import de.intevation.flys.artifacts.model.sq.SQ;

public class StdDevOutlier
{
    public static final double EPSILON = 1e-5;

    public static final double DEFAULT_FACTOR = 3;

    private static Logger log = Logger.getLogger(StdDevOutlier.class);

    protected StdDevOutlier() {
    }

    public static Integer findOutlier(List<Double> values) {
        return findOutlier(values, DEFAULT_FACTOR, null);
    }

    public static Integer findOutlier(List<Double> values, double factor, double[] stdDevResult) {
        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug("factor for std dev: " + factor);
        }

        int N = values.size();

        if (debug) {
            log.debug("Values to check: " + N);
        }

        if (values.size() < 3) {
            return null;
        }

        StandardDeviation stdDev = new StandardDeviation();

        double maxValue = -Double.MAX_VALUE;
        int    maxIndex = -1;
        int    ndx = 0;
        for (int i = values.size()-1; i >= 0; --i) {
            double value = Math.abs(values.get(i));
            stdDev.increment(value);
            if (value > maxValue) {
                maxValue = value;
                maxIndex = ndx;
            }
            ++ndx;
        }

        double sd = stdDev.getResult();

        double accepted = factor * sd;

        if (debug) {
            log.debug("std dev: " + stdDev);
            log.debug("accepted: " + accepted);
            log.debug("max value: " + maxValue);
        }
        if (stdDevResult != null) {
            stdDevResult[0] = sd;
        }
        return maxValue > accepted ? maxIndex : null;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org