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

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

Introduction

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

Prototype

public double getMin() 

Source Link

Document

Returns the minimum of the available values

Usage

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

/**
 * @param args/*from  ww w.  j  ava2s .  co  m*/
 * @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/* w  w  w .  ja  v a2s.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));

            }
        }
    }

}

From source file:playground.johannes.gsv.sim.cadyts.ODCountsAnalyzer.java

@Override
public void notifyAfterMobsim(AfterMobsimEvent event) {
    Network network = event.getControler().getScenario().getNetwork();
    DescriptiveStatistics diff = new DescriptiveStatistics();
    DescriptiveStatistics absDiff = new DescriptiveStatistics();
    DescriptiveStatistics error = new DescriptiveStatistics();
    DescriptiveStatistics absError = new DescriptiveStatistics();

    try {/*from  ww w. j  av a 2 s. c  o  m*/
        String file = event.getControler().getControlerIO().getIterationFilename(event.getIteration(),
                "odCounts.txt");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));

        writer.write("id\tobs\tsim");

        writer.newLine();

        for (Count count : counts.getCounts().values()) {
            if (count.getLocId().toString().startsWith(ODCalibrator.VIRTUAL_ID_PREFIX)) {
                Link link = network.getLinks().get(count.getLocId());
                double refVal = count.getMaxVolume().getValue() * 24;
                double simVal = simResults.getSimValue(link, 0, 86400, TYPE.COUNT_VEH);

                double err = (simVal - refVal) / refVal;
                error.addValue(err);
                absError.addValue(Math.abs(err));

                double delta = simVal - refVal;
                diff.addValue(delta);
                absDiff.addValue(Math.abs(delta));

                writer.write(link.getId().toString());
                writer.write("\t");
                writer.write(String.valueOf(refVal));
                writer.write("\t");
                writer.write(String.valueOf(simVal));
                writer.newLine();
            }
        }
        writer.close();

        logger.info(String.format("OD-relations diff: avr = %s, median = %s, var = %s, min = %s, max = %s",
                diff.getMean(), diff.getPercentile(50), diff.getVariance(), diff.getMin(), diff.getMax()));
        logger.info(
                String.format("OD-relations absolute diff: avr = %s, median = %s, var = %s, min = %s, max = %s",
                        absDiff.getMean(), absDiff.getPercentile(50), absDiff.getVariance(), absDiff.getMin(),
                        absDiff.getMax()));

        logger.info(String.format(
                "Relative OD-relations error: avr = %s, median = %s, var = %s, min = %s, max = %s",
                error.getMean(), error.getPercentile(50), error.getVariance(), error.getMin(), error.getMax()));
        logger.info(String.format(
                "Absolute relative OD-relations error: avr = %s, median = %s, var = %s, min = %s, max = %s",
                absError.getMean(), absError.getPercentile(50), absError.getVariance(), absError.getMin(),
                absError.getMax()));

        file = event.getControler().getControlerIO().getOutputFilename("odCountsDiff.txt");
        writeStats(file, diff, event.getIteration());

        file = event.getControler().getControlerIO().getOutputFilename("odCountsAbsDiff.txt");
        writeStats(file, absDiff, event.getIteration());

        file = event.getControler().getControlerIO().getOutputFilename("odCountsError.txt");
        writeStats(file, error, event.getIteration());

        file = event.getControler().getControlerIO().getOutputFilename("odCountsAbsError.txt");
        writeStats(file, absError, event.getIteration());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.johannes.gsv.sim.cadyts.ODCountsAnalyzer.java

private void writeStats(String file, DescriptiveStatistics stats, int iteration) throws IOException {
    boolean append = true;
    if (iteration == 0)
        append = false;/*ww  w  .j a  v  a  2 s. c  o m*/

    BufferedWriter writer = new BufferedWriter(new FileWriter(file, append));
    if (!append) {
        writer.write("avr\tmedian\tvar\tmin\tmax");
        writer.newLine();
    }

    writer.write(String.valueOf(stats.getMean()));
    writer.write("\t");
    writer.write(String.valueOf(stats.getPercentile(50)));
    writer.write("\t");
    writer.write(String.valueOf(stats.getVariance()));
    writer.write("\t");
    writer.write(String.valueOf(stats.getMin()));
    writer.write("\t");
    writer.write(String.valueOf(stats.getMax()));
    writer.newLine();
    writer.close();
}

From source file:playground.johannes.gsv.synPop.sim3.HamiltonianLogger.java

@Override
public void afterStep(Collection<ProxyPerson> population, Collection<ProxyPerson> mutations, boolean accepted) {
    if (iter.get() % logInterval == 0) {
        long iterNow = iter.get();
        double[] values = new double[population.size()];
        int i = 0;
        for (ProxyPerson person : population) {
            values[i] = h.evaluate(person);
            i++;//from w  ww  .j av  a  2  s  .  co m
        }
        DescriptiveStatistics stats = new DescriptiveStatistics(values);
        double sum = stats.getSum();
        double avr = stats.getMean();
        //         double med = stats.getPercentile(50);
        double max = stats.getMax();
        double min = stats.getMin();

        StringBuilder builder = new StringBuilder();
        builder.append("Statistics for ");
        builder.append(h.getClass().getSimpleName());
        builder.append(String.format(Locale.US, ": Sum = %.4f, ", sum));
        builder.append(String.format(Locale.US, ": Avr = %.4f, ", avr));
        //         builder.append(String.format(Locale.US, ": Med = %.4f, ", med));
        builder.append(String.format(Locale.US, ": Max = %.4f, ", max));
        builder.append(String.format(Locale.US, ": Min = %.4f", min));

        logger.info(builder.toString());

        if (writer != null) {
            try {
                writer.write(String.valueOf(iterNow));
                writer.write(TAB);
                writer.write(String.valueOf(sum));
                writer.write(TAB);
                writer.write(String.valueOf(avr));
                writer.write(TAB);
                //               writer.write(String.valueOf(med));
                //               writer.write(TAB);
                writer.write(String.valueOf(min));
                writer.write(TAB);
                writer.write(String.valueOf(max));
                writer.newLine();
                writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        TDoubleDoubleHashMap hist = Histogram.createHistogram(stats,
                FixedSampleSizeDiscretizer.create(stats.getValues(), 1, 100), true);
        Histogram.normalize(hist);
        //         String file = String.format("%s/%s.%s.txt", outdir, h.getClass().getSimpleName(), iterNow);
        String file = String.format("%s/%s", outdir, h.getClass().getSimpleName());
        File afile = new File(file);
        afile.mkdirs();
        try {
            TXTWriter.writeMap(hist, "value", "frequency", String.format("%s/%s.txt", file, iterNow));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    iter.incrementAndGet();
}

From source file:playground.johannes.sna.util.TXTWriter.java

public static void writeStatistics(TDoubleObjectHashMap<DescriptiveStatistics> statsMap, String xLab,
        String file) throws IOException {
    double[] keys = statsMap.keys();
    Arrays.sort(keys);// w  ww. jav  a2 s . c  o m

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

    writer.write(xLab);
    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:playground.johannes.snowball2.Betweenness.java

@Override
public DescriptiveStatistics calculate(Graph g, int iteration, DescriptiveStatistics reference) {
    centrality.run(g, iteration);//from   ww w  . j  a v  a 2 s.  co m

    gamma = calcGammaExponent(centrality.betweennessValues.getValues(), 1, 1.0);
    wMean = centrality.getBetweennessWeighted();

    double norm = (g.numVertices() - 1) * (g.numVertices() - 2);
    normMean = centrality.getGraphBetweenness() / norm;
    normWMean = centrality.getBetweennessWeighted() / norm;
    double[] normValues = getNormalizedValues(centrality.betweennessValues.getValues(), g);
    gammaNorm = calcGammaExponent(normValues, 0.000001, 0);
    double[] normWValues = getNormalizedValues(centrality.betweennessWValues.toNativeArray(), g);
    gammaWNorm = calcGammaExponent(normWValues, centrality.betweennessWeights.toNativeArray(), 0.000001, 0);

    dumpStatistics(getStatisticsMap(centrality.betweennessValues), iteration);

    if (reference != null) {
        try {
            Histogram hist = centrality.getBetweennessHistogram(reference.getMin(), reference.getMax());
            hist.plot(String.format("%1$s/%2$s.histogram.png", outputDir, iteration), "Histogram");
            hist.dumpRawData(String.format("%1$s/%2$s.histogram.txt", outputDir, iteration));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            Histogram hist = centrality.getBetweennessHistogram();
            hist.plot(String.format("%1$s/%2$s.histogram.png", outputDir, iteration), "Histogram");
            hist.dumpRawData(String.format("%1$s/%2$s.histogram.txt", outputDir, iteration));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    dumpValues(centrality.betweennessValues.getSortedValues(), iteration, "plain");
    double[] wvalues = centrality.betweennessWValues.toNativeArray();
    Arrays.sort(wvalues);
    dumpValues(wvalues, iteration, "weighted");
    Arrays.sort(normValues);
    dumpValues(normValues, iteration, "norm");
    Arrays.sort(normWValues);
    dumpValues(normWValues, iteration, "normW");

    centrality.dumpDegreeCorrelation(String.format("%1$s/%2$s.degreeBetweenness.txt", outputDir, iteration),
            "betweenness");
    return centrality.betweennessValues;
}

From source file:playground.johannes.snowball2.Closeness.java

@Override
public DescriptiveStatistics calculate(Graph g, int iteration, DescriptiveStatistics reference) {
    centrality.run(g, iteration);/*w ww.  ja  va 2 s  .c o m*/

    dumpStatistics(getStatisticsMap(centrality.closenessValues), iteration);
    if (reference != null) {
        try {
            Histogram hist = centrality.getClosenessHistogram(reference.getMin(), reference.getMax());
            hist.plot(String.format("%1$s/%2$s.histogram.png", outputDir, iteration), "Histogram");
            hist.dumpRawData(String.format("%1$s/%2$s.histogram.txt", outputDir, iteration));
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        try {
            Histogram hist = centrality.getClosenessHistogram();
            hist.plot(String.format("%1$s/%2$s.histogram.png", outputDir, iteration), "Histogram");
            hist.dumpRawData(String.format("%1$s/%2$s.histogram.txt", outputDir, iteration));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    centrality.dumpDegreeCorrelation(String.format("%1$s/%2$s.degreeCloseness.txt", outputDir, iteration),
            "closeness");

    return centrality.closenessValues;
}

From source file:playground.johannes.snowball2.Clustering.java

@SuppressWarnings("unchecked")
@Override//from ww  w . j  av  a2  s.c  o  m
public DescriptiveStatistics calculate(Graph g, int iteration, DescriptiveStatistics reference) {
    Map<Vertex, Double> values = GraphStatistics.clusteringCoefficients(g);
    DescriptiveStatistics stats = new DescriptiveStatistics();

    TIntDoubleHashMap degreeClustering = new TIntDoubleHashMap();
    TIntIntHashMap numDegree = new TIntIntHashMap();

    double sum = 0;
    double wsum = 0;

    if (g instanceof SampledGraph) {
        for (Vertex v : values.keySet()) {
            int k = v.degree();

            if (!((SampledVertex) v).isAnonymous()) {
                double cc = degreeClustering.get(k);

                if (v.degree() == 1) {
                    stats.addValue(0.0);
                    //                  sum += (cc / ((SampledVertex)v).getSampleProbability());
                } else {
                    double C = values.get(v);
                    stats.addValue(C);
                    cc += C;
                    sum += (C / ((SampledVertex) v).getSampleProbability());
                }
                degreeClustering.put(k, cc);
                numDegree.put(k, numDegree.get(k) + 1);

                wsum += (1 / ((SampledVertex) v).getSampleProbability());
            }
        }
    } else {
        for (Vertex v : values.keySet()) {
            int k = v.degree();
            double cc = degreeClustering.get(k);

            wsum++;

            if (v.degree() == 1)
                stats.addValue(0.0);
            else {
                double C = values.get(v);
                stats.addValue(C);
                cc += C;
                sum += C;
            }

            degreeClustering.put(k, cc);
            numDegree.put(k, numDegree.get(k) + 1);
        }
    }

    wMean = sum / wsum;

    try {
        BufferedWriter writer = IOUtils
                .getBufferedWriter(String.format("%1$s/%2$s.degreeDependency.txt", outputDir, iteration));
        int[] keys = numDegree.keys();
        Arrays.sort(keys);
        for (int k : keys) {
            double bc = degreeClustering.get(k);
            int numV = numDegree.get(k);

            writer.write(String.valueOf(k));
            writer.write("\t");
            writer.write(String.valueOf(bc / (double) numV));
            writer.newLine();
        }
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    dumpStatistics(getStatisticsMap(stats), iteration);

    if (reference != null) {
        Histogram hist = new Histogram(100, reference.getMin(), reference.getMax());
        plotHistogram(stats.getValues(), hist, iteration);
    } else {
        plotHistogram(stats.getValues(), new Histogram(100), iteration);
    }

    return stats;
}