List of usage examples for org.apache.commons.math.stat.descriptive DescriptiveStatistics DescriptiveStatistics
public DescriptiveStatistics()
From source file:playground.johannes.gsv.matrices.analysis.DumpRelations.java
private static DescriptiveStatistics avrRankDiff(Map<Tuple<String, String>, Integer> ranks1, Map<Tuple<String, String>, Integer> ranks2) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (Entry<Tuple<String, String>, Integer> e1 : ranks1.entrySet()) { Integer r1 = e1.getValue(); Integer r2 = ranks2.get(e1.getKey()); if (r2 != null) { stats.addValue(Math.abs(r1 - r2)); }//from w w w . j a v a 2 s .c om } return stats; }
From source file:playground.johannes.gsv.matrices.analysis.MatrixCompare.java
public static DescriptiveStatistics relErrorAll(Matrix m1, Matrix m2, boolean absolute, boolean ignoreZeros) { Set<String> zoneIds = new HashSet<>(); zoneIds.addAll(m1.getFromLocations().keySet()); zoneIds.addAll(m1.getToLocations().keySet()); zoneIds.addAll(m2.getFromLocations().keySet()); zoneIds.addAll(m2.getToLocations().keySet()); DescriptiveStatistics stats = new DescriptiveStatistics(); int zeros = 0; for (String id1 : zoneIds) { for (String id2 : zoneIds) { if (!id1.equals(id2)) { Entry e1 = m1.getEntry(id1, id2); Entry e2 = m2.getEntry(id1, id2); if (e1 != null && e2 != null) { Double val1 = e1.getValue(); Double val2 = e2.getValue(); if (!(ignoreZeros && val2 == 0)) { if (val1 > 0) { double err = calcRelError(val1, val2, absolute); stats.addValue(err); }//from ww w. ja va2 s . c om } } else zeros++; } } } if (zeros > 0) { System.out.println(String.format("%s cells where either old or new is zero.", zeros)); } return stats; }
From source file:playground.johannes.gsv.matrices.analysis.MatrixCompare.java
public static DescriptiveStatistics errorStats(TObjectDoubleHashMap<String> values) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (double val : values.getValues()) { stats.addValue(val); }//w ww . j a va 2 s.c om return stats; }
From source file:playground.johannes.gsv.matrices.analysis.NUTSCompare.java
private static DescriptiveStatistics absError(KeyMatrix m1, KeyMatrix m2) { KeyMatrix errMatrix = MatrixOperations.errorMatrix(m1, m2); // KeyMatrix errMatrix = MatrixOperations.diffMatrix(m1, m2); DescriptiveStatistics stats = new DescriptiveStatistics(); Set<String> keys = errMatrix.keys(); for (String i : keys) { for (String j : keys) { // if(i != j) { Double val = errMatrix.get(i, j); if (val != null) { stats.addValue(Math.abs(val)); // stats.addValue(val); // if(val >= 1) logger.info(String.format("%s - %s: %s", i, j, val)); // } }/*from w w w . ja va2 s. c om*/ } } return stats; }
From source file:playground.johannes.gsv.misc.MatrixCompareNorm.java
/** * @param args/*from ww w . jav a2 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 {/* w w w . j ava2 s . co 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.synPop.analysis.ActivityChainTask.java
@Override public void analyze(Collection<ProxyPerson> persons, Map<String, DescriptiveStatistics> results) { TObjectDoubleHashMap<String> chains = new TObjectDoubleHashMap<String>(); TObjectIntHashMap<String> typeCount = new TObjectIntHashMap<String>(); TDoubleDoubleHashMap tripCounts = new TDoubleDoubleHashMap(); for (ProxyPerson person : persons) { ProxyPlan plan = person.getPlan(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < plan.getActivities().size(); i++) { String type = (String) plan.getActivities().get(i).getAttribute(CommonKeys.ACTIVITY_TYPE); typeCount.adjustOrPutValue(type, 1, 1); builder.append(type);// www . ja va2s . co m builder.append("-"); } String chain = builder.toString(); chains.adjustOrPutValue(chain, 1, 1); tripCounts.adjustOrPutValue(plan.getLegs().size(), 1, 1); } for (Object key : typeCount.keys()) { DescriptiveStatistics stats = new DescriptiveStatistics(); stats.addValue(typeCount.get((String) key)); results.put(String.format("%s.%s", KEY, key), stats); } if (outputDirectoryNotNull()) { try { TXTWriter.writeMap(chains, "chain", "n", getOutputDirectory() + "/actchains.txt", true); TXTWriter.writeMap(tripCounts, "nTrips", "n", getOutputDirectory() + "/tripcounts.txt", true); } catch (IOException e) { e.printStackTrace(); } } }
From source file:playground.johannes.gsv.synPop.analysis.ActivityDistanceTask.java
protected DescriptiveStatistics statistics(Collection<ProxyPerson> persons, String purpose, String mode) { DescriptiveStatistics stats = new DescriptiveStatistics(); for (ProxyPerson person : persons) { ProxyPlan plan = person.getPlan(); for (int i = 1; i < plan.getActivities().size(); i++) { ProxyObject thisAct = plan.getActivities().get(i); ProxyObject leg = plan.getLegs().get(i - 1); if (mode.equalsIgnoreCase(leg.getAttribute(CommonKeys.LEG_MODE))) { if (purpose == null || purpose.equalsIgnoreCase(thisAct.getAttribute(CommonKeys.ACTIVITY_TYPE))) { ProxyObject prevAct = plan.getActivities().get(i - 1); Id<ActivityFacility> prevId = Id.create(prevAct.getAttribute(CommonKeys.ACTIVITY_FACILITY), ActivityFacility.class); ActivityFacility prevFac = facilities.getFacilities().get(prevId); Id<ActivityFacility> thisId = Id.create(thisAct.getAttribute(CommonKeys.ACTIVITY_FACILITY), ActivityFacility.class); ActivityFacility thisFac = facilities.getFacilities().get(thisId); Point p1 = MatsimCoordUtils.coordToPoint(prevFac.getCoord()); Point p2 = MatsimCoordUtils.coordToPoint(thisFac.getCoord()); double d = calc.distance(p1, p2); stats.addValue(d);//from w w w . java 2s . c o m } } } } return stats; }
From source file:playground.johannes.gsv.synPop.analysis.ActivityDistanceTruncatedTask.java
@Override protected DescriptiveStatistics statistics(Collection<ProxyPerson> persons, String purpose, String mode) { DescriptiveStatistics stats = super.statistics(persons, purpose, mode); if (threshold > 0) { DescriptiveStatistics newStats = new DescriptiveStatistics(); for (int i = 0; i < stats.getN(); i++) { double val = stats.getElement(i); if (val >= threshold) { newStats.addValue(val); }//w w w. ja v a 2s . c om } return newStats; } else { return stats; } }
From source file:playground.johannes.gsv.synPop.analysis.LegDistanceTask.java
protected DescriptiveStatistics statistics(Collection<ProxyPerson> persons, String purpose, String mode) { DescriptiveStatistics stats = new DescriptiveStatistics(); int cntNoVal = 0; for (ProxyPerson person : persons) { ProxyPlan plan = person.getPlan(); for (int i = 0; i < plan.getLegs().size(); i++) { ProxyObject leg = plan.getLegs().get(i); ProxyObject act = plan.getActivities().get(i + 1); if (mode == null || mode.equalsIgnoreCase(leg.getAttribute(CommonKeys.LEG_MODE))) { if (purpose == null || purpose.equalsIgnoreCase((String) act.getAttribute(CommonKeys.ACTIVITY_TYPE))) { String distStr = leg.getAttribute(attKey); if (distStr != null) { double d = Double.parseDouble(distStr); stats.addValue(d); } else { cntNoVal++;/*from www . j av a 2 s . com*/ } } } } } if (cntNoVal > 0) { logger.warn(String.format("No value specified for %s trips of purpose %s.", cntNoVal, purpose)); } return stats; }