Example usage for org.jfree.data.statistics Statistics calculateMedian

List of usage examples for org.jfree.data.statistics Statistics calculateMedian

Introduction

In this page you can find the example usage for org.jfree.data.statistics Statistics calculateMedian.

Prototype

public static double calculateMedian(List values, boolean copyAndSort) 

Source Link

Document

Calculates the median for a list of values ( Number objects).

Usage

From source file:org.jfree.data.statistics.BoxAndWhiskerCalculator.java

/**
 * Calculates the statistics required for a {@link BoxAndWhiskerItem}
 * from a list of <code>Number</code> objects.  Any items in the list
 * that are <code>null</code>, not an instance of <code>Number</code>, or
 * equivalent to <code>Double.NaN</code>, will be ignored.
 *
 * @param values  a list of numbers (a <code>null</code> list is not
 *                permitted)./*from  w  w w .java 2s  .  c  om*/
 * @param stripNullAndNaNItems  a flag that controls the handling of null
 *     and NaN items.
 *
 * @return A box-and-whisker item.
 *
 * @since 1.0.3
 */
public static BoxAndWhiskerItem calculateBoxAndWhiskerStatistics(List values, boolean stripNullAndNaNItems) {

    ParamChecks.nullNotPermitted(values, "values");

    List vlist;
    if (stripNullAndNaNItems) {
        vlist = new ArrayList(values.size());
        Iterator iterator = values.listIterator();
        while (iterator.hasNext()) {
            Object obj = iterator.next();
            if (obj instanceof Number) {
                Number n = (Number) obj;
                double v = n.doubleValue();
                if (!Double.isNaN(v)) {
                    vlist.add(n);
                }
            }
        }
    } else {
        vlist = values;
    }
    Collections.sort(vlist);

    double mean = Statistics.calculateMean(vlist, false);
    double median = Statistics.calculateMedian(vlist, false);
    double q1 = calculateQ1(vlist);
    double q3 = calculateQ3(vlist);

    double interQuartileRange = q3 - q1;

    double upperOutlierThreshold = q3 + (interQuartileRange * 1.5);
    double lowerOutlierThreshold = q1 - (interQuartileRange * 1.5);

    double upperFaroutThreshold = q3 + (interQuartileRange * 2.0);
    double lowerFaroutThreshold = q1 - (interQuartileRange * 2.0);

    double minRegularValue = Double.POSITIVE_INFINITY;
    double maxRegularValue = Double.NEGATIVE_INFINITY;
    double minOutlier = Double.POSITIVE_INFINITY;
    double maxOutlier = Double.NEGATIVE_INFINITY;
    List outliers = new ArrayList();

    Iterator iterator = vlist.iterator();
    while (iterator.hasNext()) {
        Number number = (Number) iterator.next();
        double value = number.doubleValue();
        if (value > upperOutlierThreshold) {
            outliers.add(number);
            if (value > maxOutlier && value <= upperFaroutThreshold) {
                maxOutlier = value;
            }
        } else if (value < lowerOutlierThreshold) {
            outliers.add(number);
            if (value < minOutlier && value >= lowerFaroutThreshold) {
                minOutlier = value;
            }
        } else {
            minRegularValue = Math.min(minRegularValue, value);
            maxRegularValue = Math.max(maxRegularValue, value);
        }
        minOutlier = Math.min(minOutlier, minRegularValue);
        maxOutlier = Math.max(maxOutlier, maxRegularValue);
    }

    return new BoxAndWhiskerItem(new Double(mean), new Double(median), new Double(q1), new Double(q3),
            new Double(minRegularValue), new Double(maxRegularValue), new Double(minOutlier),
            new Double(maxOutlier), outliers);

}

From source file:sturesy.votinganalysis.VoteAverage.java

/**
 * Constructer for VoteAverage, which calculates the arithmetic mean and median for the votes over time
 * @param setvotes//  w ww.j  a va 2  s  . c o m
 */
public VoteAverage(Set<Vote> setvotes) {
    if (hasVotes(setvotes)) {
        ArrayList<Long> values = new ArrayList<Long>(setvotes.size());
        for (Vote v : setvotes) {
            values.add(v.getTimeDiff());
        }

        Collections.sort(values);

        double mean = Statistics.calculateMean(values);
        double median = Statistics.calculateMedian(values, false);

        _timearithmeticMean = round(mean);
        _timeMedian = round(median);
    }
}