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

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

Introduction

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

Prototype

public double getMean() 

Source Link

Document

Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm"> arithmetic mean </a> of the available values

Usage

From source file:playground.johannes.gsv.analysis.CountsCompareAnalyzer.java

@Override
public void notifyAfterMobsim(AfterMobsimEvent event) {
    Network network = event.getControler().getScenario().getNetwork();
    DescriptiveStatistics error = new DescriptiveStatistics();
    DescriptiveStatistics errorAbs = new DescriptiveStatistics();
    DescriptivePiStatistics errorWeighted = new WSMStatsFactory().newInstance();

    TDoubleArrayList errorVals = new TDoubleArrayList();
    TDoubleArrayList caps = new TDoubleArrayList();
    TDoubleArrayList speeds = new TDoubleArrayList();

    for (Count count : counts.getCounts().values()) {
        if (!count.getLocId().toString().startsWith(ODCalibrator.VIRTUAL_ID_PREFIX)) {
            double obsVal = 0;
            for (int i = 1; i < 25; i++) {
                obsVal += count.getVolume(i).getValue();
            }/*from   w  w  w .ja v a2  s. c  om*/

            if (obsVal > 0) {
                double simVal = calculator.getOccupancy(count.getLocId());
                simVal *= factor;

                double err = (simVal - obsVal) / obsVal;

                error.addValue(err);
                errorAbs.addValue(Math.abs(err));
                errorWeighted.addValue(Math.abs(err), 1 / obsVal);

                Link link = network.getLinks().get(count.getLocId());
                errorVals.add(Math.abs(err));
                caps.add(link.getCapacity());
                speeds.add(link.getFreespeed());
            }
        }
    }

    logger.info(String.format("Relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            error.getMean(), error.getVariance(), error.getStandardDeviation(), error.getMin(),
            error.getMax()));
    logger.info(String.format(
            "Absolute relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            errorAbs.getMean(), errorAbs.getVariance(), errorAbs.getStandardDeviation(), errorAbs.getMin(),
            errorAbs.getMax()));
    logger.info(String.format(
            "Absolute weigthed relative counts error: mean = %s, var = %s, stderr = %s, min = %s, max = %s",
            errorWeighted.getMean(), errorWeighted.getVariance(), errorWeighted.getStandardDeviation(),
            errorWeighted.getMin(), errorWeighted.getMax()));

    String outdir = event.getControler().getControlerIO().getIterationPath(event.getIteration());

    try {
        TDoubleDoubleHashMap map = Correlations.mean(caps.toNativeArray(), errorVals.toNativeArray());
        TXTWriter.writeMap(map, "capacity", "counts", String.format("%s/countsError.capacity.txt", outdir));

        map = Correlations.mean(speeds.toNativeArray(), errorVals.toNativeArray());
        TXTWriter.writeMap(map, "speed", "counts", String.format("%s/countsError.speed.txt", outdir));

        TXTWriter.writeMap(Histogram.createHistogram(error, new LinearDiscretizer(0.1), false), "Error",
                "Frequency", String.format("%s/countsError.hist.txt", outdir));
        TXTWriter.writeMap(Histogram.createHistogram(errorAbs, new LinearDiscretizer(0.1), false),
                "Error (absolute)", "Frequency", String.format("%s/countsErrorAbs.hist.txt", outdir));
        TXTWriter.writeMap(Histogram.createHistogram(errorWeighted, new LinearDiscretizer(0.1), true),
                "Error (weighted)", "Frequency", String.format("%s/countsErrorWeighted.hist.txt", outdir));

        CountsCompare2GeoJSON.write(calculator, counts, factor, network, outdir);
        NetworkLoad2GeoJSON.write(event.getControler().getScenario().getNetwork(), calculator, factor,
                outdir + "/network.json");
    } catch (IOException e) {
        e.printStackTrace();
    }

    String rootOutDir = event.getControler().getControlerIO().getOutputPath();
    boolean append = false;
    if (event.getIteration() > 0) {
        append = true;
    }
    writeErrorFile(error, String.format("%s/countsError.txt", rootOutDir), append);
    writeErrorFile(errorAbs, String.format("%s/countsAbsError.txt", rootOutDir), append);
}

From source file:playground.johannes.gsv.analysis.CountsCompareAnalyzer.java

private void writeErrorFile(DescriptiveStatistics error, String file, boolean append) {
    try {/*from   w  ww.jav  a 2s.c  o m*/
        BufferedWriter writer = new BufferedWriter(new FileWriter(file, append));
        if (!append) {
            // write header
            writer.write("mean\tvar\tstderr\tmin\tmax");
            writer.newLine();
        }

        writer.write(String.valueOf(error.getMean()));
        writer.write("\t");
        writer.write(String.valueOf(error.getVariance()));
        writer.write("\t");
        writer.write(String.valueOf(error.getStandardDeviation()));
        writer.write("\t");
        writer.write(String.valueOf(error.getMin()));
        writer.write("\t");
        writer.write(String.valueOf(error.getMax()));
        writer.newLine();

        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

/**
 * @param args/*from w ww . jav  a  2s .  c o m*/
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    KeyMatrixXMLReader reader = new KeyMatrixXMLReader();
    reader.setValidating(false);

    reader.parse("/home/johannes/gsv/matrices/refmatrices/itp.xml");
    KeyMatrix carItpMatrix = reader.getMatrix();
    MatrixOperations.applyFactor(carItpMatrix, 1 / 365.0);

    reader.parse("/home/johannes/gsv/matrices/simmatrices/miv.819.2.xml");
    KeyMatrix carSimMatrix = reader.getMatrix();
    //      MatrixOperations.symetrize(carSimMatrix);
    //      MatrixOperations.applyFactor(carSimMatrix, 11.8);

    reader.parse("/home/johannes/gsv/matrices/refmatrices/tomtom.de.xml");
    KeyMatrix carTomTomMatrix = reader.getMatrix();

    reader.parse("/home/johannes/gsv/matrices/analysis/marketShares/rail.all.nuts3.xml");
    KeyMatrix railSimMatrix = reader.getMatrix();
    MatrixOperations.applyFactor(railSimMatrix, 1 / 365.0);

    reader.parse("/home/johannes/gsv/matrices/analysis/marketShares/car.share.xml");
    KeyMatrix shareRefMatrix = reader.getMatrix();

    ZoneCollection zones = new ZoneCollection();
    String data = new String(Files.readAllBytes(Paths.get("/home/johannes/gsv/gis/nuts/de.nuts3.gk3.geojson")));
    zones.addAll(Zone2GeoJSON.parseFeatureCollection(data));
    data = null;
    zones.setPrimaryKey("gsvId");

    ODUtils.cleanDistances(railSimMatrix, zones, 100000);
    ODUtils.cleanDistances(carSimMatrix, zones, 100000);

    ODUtils.cleanDistances(carTomTomMatrix, zones, 100000);
    //      ODUtils.cleanVolumes(carTomTomMatrix, zones, 100);
    double c = ODUtils.calcNormalization(carTomTomMatrix, carSimMatrix);
    MatrixOperations.applyFactor(carTomTomMatrix, c);

    removeUnknownZones(railSimMatrix);
    List<Tuple<String, String>> relations = getRelations(railSimMatrix, 3000);
    //      List<Tuple<String, String>> relations = getRelations(carTomTomMatrix, 3000);

    BufferedWriter writer = new BufferedWriter(
            new FileWriter("/home/johannes/gsv/matrices/analysis/marketShares/miv-shares.txt"));
    writer.write("from\tto\tshareRef\tshareSim\tcarVol\trailVol\tshareItp\tvolItp\tcarTomTom\tshareTomTom");
    writer.newLine();

    DescriptiveStatistics simStats = new DescriptiveStatistics();
    DescriptiveStatistics itpStats = new DescriptiveStatistics();
    DescriptiveStatistics simItpStats = new DescriptiveStatistics();

    TDoubleArrayList railVolumes = new TDoubleArrayList();
    TDoubleArrayList shareSimDiff = new TDoubleArrayList();
    TDoubleArrayList shareItpDiff = new TDoubleArrayList();

    TDoubleArrayList refShares = new TDoubleArrayList();
    TDoubleArrayList simShares = new TDoubleArrayList();
    TDoubleArrayList itpShares = new TDoubleArrayList();
    TDoubleArrayList tomtomShares = new TDoubleArrayList();

    for (Tuple<String, String> relation : relations) {
        String i = relation.getFirst();
        String j = relation.getSecond();

        Double carSimVal = carSimMatrix.get(i, j);
        if (carSimVal == null)
            carSimVal = 0.0;

        Double carItpVal = carItpMatrix.get(i, j);
        if (carItpVal == null)
            carItpVal = 0.0;

        Double carTomTomVal = carTomTomMatrix.get(i, j);
        if (carTomTomVal == null)
            carTomTomVal = 0.0;

        Double railVal = railSimMatrix.get(i, j);
        if (railVal == null)
            railVal = 0.0;

        Double shareVal = shareRefMatrix.get(i, j);
        if (shareVal == null)
            shareVal = 0.0;

        double simShare = carSimVal / (carSimVal + railVal);
        double itpShare = carItpVal / (carItpVal + railVal);
        double tomtomShare = carTomTomVal / (carTomTomVal + railVal);

        simStats.addValue(simShare - shareVal);
        itpStats.addValue(itpShare - shareVal);
        simItpStats.addValue(itpShare - simShare);

        refShares.add(shareVal);
        simShares.add(simShare);
        itpShares.add(itpShare);
        tomtomShares.add(tomtomShare);

        railVolumes.add(railVal);
        shareSimDiff.add(shareVal - simShare);
        shareItpDiff.add(shareVal - itpShare);

        writer.write(i);
        writer.write("\t");
        writer.write(j);
        writer.write("\t");
        writer.write(String.valueOf(shareVal));
        writer.write("\t");
        writer.write(String.valueOf(simShare));
        writer.write("\t");
        writer.write(String.valueOf(carSimVal));
        writer.write("\t");
        writer.write(String.valueOf(railVal));
        writer.write("\t");
        writer.write(String.valueOf(itpShare));
        writer.write("\t");
        writer.write(String.valueOf(carItpVal));
        writer.write("\t");
        writer.write(String.valueOf(carTomTomVal));
        writer.write("\t");
        writer.write(String.valueOf(tomtomShare));

        writer.newLine();
    }
    writer.close();

    System.out.println("Avr sim share error: " + simStats.getMean());
    System.out.println("Avr itp share error: " + itpStats.getMean());
    System.out.println("Avr sim-itp share error: " + simItpStats.getMean());

    writeCorrelation(refShares, simShares, "model", "sim",
            "/home/johannes/gsv/matrices/analysis/marketShares/modelSim.txt");
    writeCorrelation(itpShares, simShares, "itp", "sim",
            "/home/johannes/gsv/matrices/analysis/marketShares/itpSim.txt");
    writeCorrelation(tomtomShares, simShares, "tomtom", "sim",
            "/home/johannes/gsv/matrices/analysis/marketShares/tomtomSim.txt");
    writeCorrelation(itpShares, tomtomShares, "itp", "tomtom",
            "/home/johannes/gsv/matrices/analysis/marketShares/itpTomtom.txt");

    //      double[] samples = railVolumes.toNativeArray();
    //      TDoubleDoubleHashMap hist = Correlations.mean(samples, shareSimDiff.toNativeArray(), FixedSampleSizeDiscretizer.create(samples, 50));
    //      TXTWriter.writeMap(hist, "rail volume", "share diff", "/home/johannes/gsv/matrices/analysis/marketShares/simVolDiff.txt");
    //      
    //      hist = Correlations.mean(samples, shareItpDiff.toNativeArray(), FixedSampleSizeDiscretizer.create(samples, 50));
    //      TXTWriter.writeMap(hist, "rail volume", "share diff", "/home/johannes/gsv/matrices/analysis/marketShares/itpVolDiff.txt");
    //      
    //      hist = Correlations.mean(refShares.toNativeArray(), simShares.toNativeArray());
    //      TXTWriter.writeMap(hist, "ref share", "sim share", "/home/johannes/gsv/matrices/analysis/marketShares/simShareCorrel.txt");
    //      
    //      hist = Correlations.mean(refShares.toNativeArray(), itpShares.toNativeArray());
    //      TXTWriter.writeMap(hist, "ref share", "itp share", "/home/johannes/gsv/matrices/analysis/marketShares/itpShareCorrel.txt");
}

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

private static void writeRelations(KeyMatrix tomtom, KeyMatrix sim, KeyMatrix itp, KeyMatrix dist,
        List<Tuple<String, String>> relations, String file) throws IOException {
    BufferedWriter writer = new BufferedWriter(new FileWriter(file));
    writer.write("from\tto\ttomtom\tsim\titp");
    writer.newLine();//from w  w  w .jav a  2s.  co  m

    DescriptiveStatistics simStats = new DescriptiveStatistics();
    DescriptiveStatistics itpStats = new DescriptiveStatistics();

    for (Tuple<String, String> tuple : relations) {
        writer.write(tuple.getFirst());
        writer.write("\t");
        writer.write(tuple.getSecond());
        writer.write("\t");

        Double tomtomVal = tomtom.get(tuple.getFirst(), tuple.getSecond());
        if (tomtomVal == null)
            tomtomVal = 0.0;
        writer.write(String.valueOf(tomtomVal));
        writer.write("\t");

        Double val = sim.get(tuple.getFirst(), tuple.getSecond());
        if (val == null)
            val = 0.0;
        writer.write(String.valueOf(val));
        writer.write("\t");
        simStats.addValue(Math.abs((val - tomtomVal) / tomtomVal));

        val = itp.get(tuple.getFirst(), tuple.getSecond());
        if (val == null)
            val = 0.0;
        writer.write(String.valueOf(val));
        itpStats.addValue(Math.abs((val - tomtomVal) / tomtomVal));

        writer.write("\t");
        writer.write(String.valueOf(dist.get(tuple.getFirst(), tuple.getSecond())));
        writer.newLine();
    }
    writer.close();

    System.out.println("sim: " + simStats.getMean());
    System.out.println("itp: " + itpStats.getMean());
}

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

/**
 * @param args/*from  w  w w .  j  a va2 s . c  o  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/*ww w.j a  va2 s  . c o m*/
 */
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  w w w. j a  v 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;/*  www  .  jav  a2  s  .  co 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   www .  j  ava2  s. c om*/
        }
        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();
}