Example usage for org.apache.commons.math.stat.descriptive DescriptiveStatistics getPercentile

List of usage examples for org.apache.commons.math.stat.descriptive DescriptiveStatistics getPercentile

Introduction

In this page you can find the example usage for org.apache.commons.math.stat.descriptive DescriptiveStatistics getPercentile.

Prototype

public double getPercentile(double p) 

Source Link

Document

Returns an estimate for the pth percentile of the stored values.

Usage

From source file:org.matsim.contrib.common.stats.StatsWriter.java

/**
 * Writes a table with columns map-key and statistical indicators mean, median, min, max and number of samples. Rows
 * are sorted according to the natural order of the map keys.
 *
 * @param statsMap a map with {@code DescriptiveStatistics} objects
 * @param keyLabel the header for the first column (containing the map keys)
 * @param file     the filename//w w w . ja v  a2  s.  c  om
 * @throws IOException
 */
public static void writeStatistics(TDoubleObjectHashMap<DescriptiveStatistics> statsMap, String keyLabel,
        String file) throws IOException {
    double[] keys = statsMap.keys();
    Arrays.sort(keys);

    BufferedWriter writer = new BufferedWriter(new FileWriter(file));

    writer.write(keyLabel);
    writer.write(TAB);
    writer.write("mean");
    writer.write(TAB);
    writer.write("median");
    writer.write(TAB);
    writer.write("min");
    writer.write(TAB);
    writer.write("max");
    writer.write(TAB);
    writer.write("n");
    writer.newLine();

    for (double key : keys) {
        DescriptiveStatistics stats = statsMap.get(key);

        writer.write(String.valueOf(key));
        writer.write(TAB);
        writer.write(String.valueOf(stats.getMean()));
        writer.write(TAB);
        writer.write(String.valueOf(stats.getPercentile(50)));
        writer.write(TAB);
        writer.write(String.valueOf(stats.getMin()));
        writer.write(TAB);
        writer.write(String.valueOf(stats.getMax()));
        writer.write(TAB);
        writer.write(String.valueOf(stats.getN()));
        writer.newLine();
    }

    writer.close();
}

From source file:org.matsim.contrib.socnetgen.sna.graph.spatial.analysis.EdgeLengthMedian.java

private double edgeLengthMedian(SpatialVertex vertex) {
    if (calculator == null) {
        calculator = DistanceCalculatorFactory
                .createDistanceCalculator(CRSUtils.getCRS(vertex.getPoint().getSRID()));
    }//ww  w.j av a2 s .com

    DescriptiveStatistics stats = new DescriptiveStatistics();

    for (SpatialVertex neighbor : vertex.getNeighbours()) {
        if (neighbor.getPoint() != null) {
            if (vertex.getPoint().getSRID() == neighbor.getPoint().getSRID()) {
                stats.addValue(calculator.distance(vertex.getPoint(), neighbor.getPoint()));
            } else {
                throw new RuntimeException("Points do not share the same coordinate reference system.");
            }
        }
    }

    if (stats.getN() > 1)
        return stats.getPercentile(50);
    else
        return Double.NaN;
}

From source file:org.ow2.clif.jenkins.chart.QuantileDistributionChart.java

public void addData(DescriptiveStatistics stat) {
    for (int i = 5; i <= 100; i += 5) {
        this.data.addValue(stat.getPercentile(i), this.chartId.getEvent(), "" + i);
    }/*from  ww w.  j a v  a2 s  . c  o m*/
}

From source file:org.processmining.analysis.performance.fsmanalysis.FSMPerformanceAnalysisUI.java

protected double getData(DescriptiveStatistics ds) {
    String sort = (String) measureSort.getValue();
    if (sort.equals("Minimum")) {
        return ds.getMin();
    } else if (sort.equals("Average")) {
        return (ds.getMean());
    } else if (sort.equals("Median")) {
        return (ds.getPercentile(50));
    } else if (sort.equals("Maximum")) {
        return (ds.getMax());
    } else if (sort.equals("Sum")) {
        return (ds.getSum());
    } else if (sort.equals("StandDev")) {
        return (ds.getStandardDeviation());
    } else if (sort.equals("Variance")) {
        return (ds.getStandardDeviation() * ds.getStandardDeviation());
    } else if (sort.equals("Frequency")) {
        return (ds.getN());
    }/*w ww.java  2 s.co m*/
    return 0.0;
}

From source file:org.tellervo.desktop.graph.SkeletonPlot.java

private Integer getSkeletonCategoryFromPercentiles(Integer value, DescriptiveStatistics windowStats) {
    Integer skeletonCategory = 0;
    // Calculate skeleton category
    if (value == (int) windowStats.getMin()) {
        skeletonCategory = 10;/*from w  ww  . jav  a2  s .  com*/
    } else if (value < windowStats.getPercentile(10)) {
        skeletonCategory = 9;
    } else if (value < windowStats.getPercentile(15)) {
        skeletonCategory = 8;
    } else if (value < windowStats.getPercentile(20)) {
        skeletonCategory = 7;
    } else if (value < windowStats.getPercentile(25)) {
        skeletonCategory = 6;
    } else if (value < windowStats.getPercentile(30)) {
        skeletonCategory = 5;
    } else if (value < windowStats.getPercentile(35)) {
        skeletonCategory = 4;
    } else if (value < windowStats.getPercentile(40)) {
        skeletonCategory = 3;
    } else if (value < windowStats.getPercentile(45)) {
        skeletonCategory = 2;
    } else if (value < windowStats.getPercentile(50)) {
        skeletonCategory = 1;
    }

    return skeletonCategory;
}

From source file:playground.artemc.pricing.SocialCostCalculator.java

private void calcStatistics() {

    // Get a DescriptiveStatistics instance
    DescriptiveStatistics tripStats = new DescriptiveStatistics();
    DescriptiveStatistics tripStatsNormalized = new DescriptiveStatistics();

    // Add the data from the array
    for (LegTrip legTrip : performedLegs) {
        double distance = 0.0;
        double cost = 0.0;
        for (LinkTrip linkTrip : legTrip.linkTrips) {
            double socialCosts = calcSocCosts(linkTrip.link_id, linkTrip.enterTime);
            if (socialCosts > 0.0)
                cost = cost + socialCosts;
            distance = legTrip.distance + network.getLinks().get(linkTrip.link_id).getLength();
        }/*ww  w.j a  v a  2  s  .c  o  m*/

        legTrip.distance = distance;
        legTrip.cost = cost;

        tripStats.addValue(cost);

        /*
         * Normalize a legs social cost by dividing them by the leg travel time or leg distance.
         */
        //double legTravelTime = legTrip.arrivalTime - legTrip.departureTime;
        if (cost > 0.0 && legTrip.distance > 0.0)
            tripStatsNormalized.addValue(cost / legTrip.distance);
    }

    // Compute some statistics
    double sum = tripStats.getSum();
    double mean = tripStats.getMean();
    double std = tripStats.getStandardDeviation();
    double median = tripStats.getPercentile(50);
    double quantile25 = tripStats.getPercentile(25);
    double quantile75 = tripStats.getPercentile(75);

    double sumNormalized = tripStatsNormalized.getSum();
    double meanNormalized = tripStatsNormalized.getMean();
    double stdNormalized = tripStatsNormalized.getStandardDeviation();
    double medianNormalized = tripStatsNormalized.getPercentile(50);
    double quantile25Normalized = tripStatsNormalized.getPercentile(25);
    double quantile75Normalized = tripStatsNormalized.getPercentile(75);

    log.info("Sum of all leg costs: " + sum);
    log.info("Mean leg costs: " + mean);
    log.info("Standard deviation: " + std);
    log.info("Median leg costs: " + median);
    log.info("25% quantile leg costs: " + quantile25);
    log.info("75% quantile leg costs: " + quantile75);

    log.info("Normalized sum of all leg costs: " + sumNormalized);
    log.info("Normalized mean leg costs: " + meanNormalized);
    log.info("Normalized standard deviation: " + stdNormalized);
    log.info("Normalized median leg costs: " + medianNormalized);
    log.info("Normalized 25% quantile leg costs: " + quantile25Normalized);
    log.info("Normalized 75% quantile leg costs: " + quantile75Normalized);

    meanSocialCosts.add(mean);
    medianSocialCosts.add(median);
    quantil25PctSocialCosts.add(quantile25);
    quantil75PctSocialCosts.add(quantile75);

    meanNormalizedSocialCosts.add(meanNormalized);
    medianNormalizedSocialCosts.add(medianNormalized);
    quantil25PctNormalizedSocialCosts.add(quantile25Normalized);
    quantil75PctNormalizedSocialCosts.add(quantile75Normalized);
}

From source file:playground.christoph.socialcosts.SocialCostCalculator.java

private void calcStatistics() {

    // Get a DescriptiveStatistics instance
    DescriptiveStatistics stats = new DescriptiveStatistics();
    DescriptiveStatistics statsNormalized = new DescriptiveStatistics();

    // Add the data from the array
    for (LegTrip legTrip : performedLegs) {
        double costs = 0.0;
        for (LinkTrip linkTrip : legTrip.linkTrips) {
            double socialCosts = calcSocCosts(linkTrip.link_id, linkTrip.enterTime);
            if (socialCosts > 0.0)
                costs = costs + socialCosts;
        }/* www  .j a  v a2  s  . c om*/
        stats.addValue(costs);

        /*
         * Normalize a legs social cost by dividing them by the leg travel time.
         * As a result we get something like social costs per traveled second.
         * Another option would be doing this on link level instead of leg level.
         */
        double legTravelTime = legTrip.arrivalTime - legTrip.departureTime;
        if (costs > 0.0 && legTravelTime > 0.0)
            statsNormalized.addValue(costs / legTravelTime);
    }

    // Compute some statistics
    double sum = stats.getSum();
    double mean = stats.getMean();
    double std = stats.getStandardDeviation();
    double median = stats.getPercentile(50);
    double quantile25 = stats.getPercentile(25);
    double quantile75 = stats.getPercentile(75);

    double sumNormalized = statsNormalized.getSum();
    double meanNormalized = statsNormalized.getMean();
    double stdNormalized = statsNormalized.getStandardDeviation();
    double medianNormalized = statsNormalized.getPercentile(50);
    double quantile25Normalized = statsNormalized.getPercentile(25);
    double quantile75Normalized = statsNormalized.getPercentile(75);

    log.info("Sum of all leg costs: " + sum);
    log.info("Mean leg costs: " + mean);
    log.info("Standard deviation: " + std);
    log.info("Median leg costs: " + median);
    log.info("25% quantile leg costs: " + quantile25);
    log.info("75% quantile leg costs: " + quantile75);

    log.info("Normalized sum of all leg costs: " + sumNormalized);
    log.info("Normalized mean leg costs: " + meanNormalized);
    log.info("Normalized standard deviation: " + stdNormalized);
    log.info("Normalized median leg costs: " + medianNormalized);
    log.info("Normalized 25% quantile leg costs: " + quantile25Normalized);
    log.info("Normalized 75% quantile leg costs: " + quantile75Normalized);

    meanSocialCosts.add(mean);
    medianSocialCosts.add(median);
    quantil25PctSocialCosts.add(quantile25);
    quantil75PctSocialCosts.add(quantile75);

    meanNormalizedSocialCosts.add(meanNormalized);
    medianNormalizedSocialCosts.add(medianNormalized);
    quantil25PctNormalizedSocialCosts.add(quantile25Normalized);
    quantil75PctNormalizedSocialCosts.add(quantile75Normalized);
}

From source file:playground.johannes.gsv.matrices.analysis.MatrixCompare.java

/**
 * @param args//from   w w  w . j a v  a  2s .  com
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    Matrix m1 = new Matrix("1", null);
    VisumMatrixReader reader = new VisumMatrixReader(m1);
    //      reader.readFile("/home/johannes/gsv/matrices/netz2030.fma");
    reader.readFile("/home/johannes/gsv/matrices/itp.fma");

    Matrix m2 = new Matrix("2", null);
    reader = new VisumMatrixReader(m2);
    reader.readFile("/home/johannes/gsv/matrices/miv.489.fma");
    //      reader.readFile("/home/johannes/gsv/matrices/netz2030.fma");

    MatrixOperations.applyFactor(m1, 1 / 365.0);
    //      MatrixOperations.applyFactor(m2, 11);
    //      MatrixOperations.applyIntracellFactor(m2, 1.3);

    System.out.println(String.format("PSMobility - matrix sum: %s", MatrixOperations.sum(m1, false)));
    System.out.println(String.format("Matsim - matrix sum: %s", MatrixOperations.sum(m2, false)));

    System.out.println(
            String.format("PSMobility: %s cells with zero value.", MatrixOperations.countEmptyCells(m1)));
    System.out
            .println(String.format("Matsim: %s cells with zero value.", MatrixOperations.countEmptyCells(m2)));

    boolean ignoreZeros = false;
    DescriptiveStatistics stats = relErrorAll(m1, m2, false, ignoreZeros);
    System.out.println(String.format("Relative error all cells: mean=%s, med=%s, min=%s, max=%s",
            stats.getMean(), stats.getPercentile(0.5), stats.getMin(), stats.getMax()));
    stats = relErrorAll(m1, m2, true, ignoreZeros);
    System.out.println(String.format("Relative error all cells (abs): mean=%s, med=%s, min=%s, max=%s",
            stats.getMean(), stats.getPercentile(0.5), stats.getMin(), stats.getMax()));

    stats = errorStats(relErrorDestinations(m1, m2, false, ignoreZeros));
    System.out.println(String.format("Destination Error: mean=%s, med=%s, min=%s, max=%s", stats.getMean(),
            stats.getPercentile(0.5), stats.getMin(), stats.getMax()));
    stats = errorStats(relErrorDestinations(m1, m2, true, ignoreZeros));
    System.out.println(String.format("Destination Error (abs): mean=%s, med=%s, min=%s, max=%s",
            stats.getMean(), stats.getPercentile(0.5), stats.getMin(), stats.getMax()));

    stats = errorStats(relErrorOrigins(m1, m2, false, ignoreZeros));
    System.out.println(String.format("Origin Error: mean=%s, med=%s, min=%s, max=%s", stats.getMean(),
            stats.getPercentile(0.5), stats.getMin(), stats.getMax()));
    stats = errorStats(relErrorOrigins(m1, m2, true, ignoreZeros));
    System.out.println(String.format("Origin Error (abs): mean=%s, med=%s, min=%s, max=%s", stats.getMean(),
            stats.getPercentile(0.5), stats.getMin(), stats.getMax()));

    ZoneLayer<Map<String, Object>> zones = ZoneLayerSHP.read("/home/johannes/gsv/matrices/zones_zone.SHP");
    TDoubleDoubleHashMap distErrCorrelation = distErrCorrelation(m1, m2, zones, false, ignoreZeros);
    TXTWriter.writeMap(distErrCorrelation, "distance", "rel. error", "/home/johannes/gsv/matrices/distErr.txt");

    Map<String, String> ids = new HashMap<>();
    ids.put("6412", "FRA");
    ids.put("11000", "BER");
    ids.put("2000", "HAM");
    ids.put("3241", "HAN");
    ids.put("5315", "KLN");
    ids.put("9162", "MUN");
    ids.put("8111", "STG");

    zones = ZoneLayerSHP.read("/home/johannes/gsv/matrices/zones_zone.SHP");
    //      distErrCorrelation = distErrCorrelation(m1, m2, zones, ids.keySet(), false, ignoreZeros);
    //      TXTWriter.writeMap(distErrCorrelation, "distance", "rel. error", "/home/johannes/gsv/matrices/distErr.sel.txt");

    Map<String, double[]> relErrs = relError(m1, m2, ids, false);
    for (java.util.Map.Entry<String, double[]> entry : relErrs.entrySet()) {
        System.out.println(String.format("%s: %.4f; old: %.4f, new; %.4f", entry.getKey(), entry.getValue()[0],
                entry.getValue()[1], entry.getValue()[2]));
    }

    System.out.println("\nDestination errors:");
    TObjectDoubleHashMap<String> destErrors = relErrorDestinations(m1, m2, false, ignoreZeros);
    for (java.util.Map.Entry<String, String> entry : ids.entrySet()) {
        System.out.println(String.format("%s: %.4f", entry.getValue(), destErrors.get(entry.getKey())));
    }

    System.out.println("\nOrigin errors:");
    TObjectDoubleHashMap<String> origErrors = relErrorOrigins(m1, m2, false, ignoreZeros);
    for (java.util.Map.Entry<String, String> entry : ids.entrySet()) {
        System.out.println(String.format("%s: %.4f", entry.getValue(), origErrors.get(entry.getKey())));
    }
}

From source file:playground.johannes.gsv.matrices.analysis.NUTSCompare.java

private static void printStats(DescriptiveStatistics stats, String name) {
    logger.info(String.format("%s : mean = %.2f, median = %.2f, var = %.2f, min = %.2f, max = %.2f", name,
            stats.getMean(), stats.getPercentile(50), stats.getVariance(), stats.getMin(), stats.getMax()));
}

From source file:playground.johannes.gsv.misc.MatrixCompareNorm.java

/**
 * @param args//from  ww w  . java 2 s  . c  om
 */
public static void main(String[] args) {
    Matrix m1 = new Matrix("1", null);
    VisumMatrixReader reader = new VisumMatrixReader(m1);
    reader.readFile("/home/johannes/gsv/matrices/IV_gesamt.O.fma");
    normMatrix(m1);

    Matrix m2 = new Matrix("2", null);
    reader = new VisumMatrixReader(m2);
    reader.readFile("/home/johannes/gsv/matrices/miv.277.fma");
    normMatrix(m2);

    int notfound = 0;
    DescriptiveStatistics oRelErrs = new DescriptiveStatistics();
    Set<String> origs = m1.getFromLocations().keySet();
    for (String origId : origs) {
        List<Entry> entries1 = m1.getFromLocEntries(origId);
        List<Entry> entries2 = m2.getFromLocEntries(origId);

        double sum1 = 0;
        for (Entry entry : entries1) {
            sum1 += entry.getValue();
        }

        if (entries2 == null) {
            oRelErrs.addValue(-1);
        } else if (entries2 != null && sum1 > 0) {
            double sum2 = 0;
            for (Entry entry : entries2) {
                sum2 += entry.getValue();
            }

            oRelErrs.addValue((sum2 - sum1) / sum1);
        } else {
            notfound++;
        }

    }

    System.err.println(String.format("%s entries out of %s not found or with zero value.", notfound,
            notfound + oRelErrs.getN()));
    System.out.println(String.format("Rel err of origins: mean=%s, med=%s, min=%s, max=%s", oRelErrs.getMean(),
            oRelErrs.getPercentile(0.5), oRelErrs.getMin(), oRelErrs.getMax()));

    DescriptiveStatistics dRelErrs = new DescriptiveStatistics();
    Set<String> dests = m1.getToLocations().keySet();
    for (String destId : dests) {
        List<Entry> entries1 = m1.getToLocEntries(destId);
        List<Entry> entries2 = m2.getToLocEntries(destId);

        double sum1 = 0;
        for (Entry entry : entries1) {
            sum1 += entry.getValue();
        }

        if (entries2 != null && sum1 > 0) {
            double sum2 = 0;
            for (Entry entry : entries2) {
                sum2 += entry.getValue();
            }

            dRelErrs.addValue((sum2 - sum1) / sum1);
        }

    }

    System.out.println(String.format("Rel err of destinations: mean=%s, med=%s, min=%s, max=%s",
            dRelErrs.getMean(), dRelErrs.getPercentile(0.5), dRelErrs.getMin(), dRelErrs.getMax()));

    Map<String, String> ids = new HashMap<>();
    ids.put("6412", "FRA");
    ids.put("11000", "BER");
    ids.put("2000", "HAM");
    ids.put("3241", "HAN");
    ids.put("5315", "KLN");
    ids.put("9162", "MUN");
    ids.put("8111", "STG");

    Map<String, Double> errors = new HashMap<>();
    for (String id1 : ids.keySet()) {
        for (String id2 : ids.keySet()) {
            if (!id1.equalsIgnoreCase(id2)) {

                Entry e1 = m1.getEntry(id1, id2);
                double val1 = e1.getValue();

                Entry e2 = m2.getEntry(id1, id2);
                double val2 = e2.getValue();

                double err = (val2 - val1) / val1;

                System.out.print(ids.get(id1));
                System.out.print(" -> ");
                System.out.print(ids.get(id2));
                System.out.print(": ");
                System.out.println(String.valueOf(err));

            }
        }
    }

}