view artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Outlier.java @ 6152:0587819960c3

Waterlevel differences & bed height differences: Add new model LinearInterpolated intented to unify the two very similiar calculations. The focus of the current implementation is correctness and not speed! The fact that the data sets more mostly sorted by station is not exploited. Doing so would improve performance significantly.
author Sascha L. Teichmann <teichmann@intevation.de>
date Sun, 02 Jun 2013 17:52:53 +0200
parents af13ceeba52a
children 0a5239a1e46e
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.artifacts.model.sq;

import org.dive4elements.river.artifacts.math.GrubbsOutlier;
import org.dive4elements.river.artifacts.math.StdDevOutlier;

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

import org.apache.commons.math.MathException;

import org.apache.log4j.Logger;

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

    private static final String GRUBBS = "outlier.method.grubbs";

    //private static final String STD_DEV = "std-dev";

    public interface Callback {

        void initialize(List<SQ> sqs) throws MathException;

        double eval(SQ sq);

        void iterationFinished(
            double   stdDev,
            SQ       outlier,
            List<SQ> remaining);

    } // interface Callback

    public static void detectOutliers(
        Callback callback,
        List<SQ> sqs,
        double   stdDevFactor,
        String   method
    )
    throws MathException
    {
        boolean debug = log.isDebugEnabled();

        if (method == null) {
            method = "std-dev";
        }

        if (debug) {
            log.debug("stdDevFactor: " + stdDevFactor);
            log.debug("method: " + method);
        }

        List<SQ> data = new ArrayList<SQ>(sqs);

        double [] stdDev = new double[1];

        boolean useGrubbs = method.equals(GRUBBS);

        if (useGrubbs) {
            stdDevFactor = Math.max(0d, Math.min(stdDevFactor/100d, 1d));
        }

        List<Double> values = new ArrayList<Double>(data.size());

        while (data.size() > 2) {

            callback.initialize(data);

            for (SQ sq: data) {
                values.add(callback.eval(sq));
            }

            Integer ndx = useGrubbs
                ? GrubbsOutlier.findOutlier(values, stdDevFactor, stdDev)
                : StdDevOutlier.findOutlier(values, stdDevFactor, stdDev);

            if (ndx == null) {
                callback.iterationFinished(stdDev[0], null, data);
                break;
            }

            SQ outlier = data.remove(ndx.intValue());
            if (debug) {
                log.debug("stdDev: " + stdDev[0]);
                log.debug("removed " + ndx +
                    "; S: " + outlier.getS() + " Q: " + outlier.getQ());
            }
            callback.iterationFinished(stdDev[0], outlier, data);
            values.clear();
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org