view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Outlier.java @ 4255:670e98f5a441

Fixed leak while merging facets. The ThemeList that is used by OutputHelper to sort the Facets for an Output now uses a list to store the ManagedFacets. The correct order is made up by sorting the List using Collections.sort() function of the Java JDK. Therfore, the ManagedFacet class implements the Comparable interface. The return value of its compareTo(other) method depends on the value of the 'position' field.
author Ingo Weinzierl <weinzierl.ingo@googlemail.com>
date Thu, 25 Oct 2012 14:01:46 +0200
parents b8b1280606c2
children a7d080347ac3
line wrap: on
line source
package de.intevation.flys.artifacts.model.sq;

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

import org.apache.commons.math.MathException;

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

import org.apache.log4j.Logger;

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

    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
    )
    throws MathException
    {
        boolean debug = log.isDebugEnabled();

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

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

        while (data.size() > 2) {

            callback.initialize(data);

            StandardDeviation stdDev = new StandardDeviation();

            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();

            double accepted = stdDevFactor * sd;

            if (debug) {
                log.debug("std dev: " + stdDev);
                log.debug("accepted: " + accepted);
                log.debug("max value: " + maxValue);
            }

            SQ outlier = maxValue > accepted
                ? data.remove(maxIndex)
                : null;

            callback.iterationFinished(sd, outlier, data);

            if (outlier == null) {
                break;
            }
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org