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

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

Introduction

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

Prototype

public DescriptiveStatistics() 

Source Link

Document

Construct a DescriptiveStatistics instance with an infinite window

Usage

From source file:playground.johannes.gsv.synPop.analysis.PkmTask.java

@Override
public void analyze(Collection<ProxyPerson> persons, Map<String, DescriptiveStatistics> results) {
    Set<String> purposes = new HashSet<String>();
    for (ProxyPerson person : persons) {
        ProxyPlan plan = person.getPlan();
        for (int i = 0; i < plan.getActivities().size(); i++) {
            purposes.add((String) plan.getActivities().get(i).getAttribute(CommonKeys.ACTIVITY_TYPE));
        }//from  w  w w  .jav a 2 s.c  o m
    }

    purposes.add(null);

    for (String purpose : purposes) {
        double pkm = 0;
        for (ProxyPerson person : persons) {
            ProxyPlan plan = person.getPlans().get(0);

            for (int i = 1; i < plan.getLegs().size(); i++) {
                ProxyObject leg = plan.getLegs().get(i);
                if (mode == null || mode.equalsIgnoreCase(leg.getAttribute(CommonKeys.LEG_MODE))) {
                    ProxyObject act = plan.getActivities().get(i + 1);
                    if (purpose == null
                            || purpose.equalsIgnoreCase(act.getAttribute(CommonKeys.ACTIVITY_TYPE))) {
                        String value = leg.getAttribute(CommonKeys.LEG_ROUTE_DISTANCE);
                        if (value != null) {
                            pkm += Double.parseDouble(value);
                        }
                    }
                }
            }

        }

        if (purpose == null)
            purpose = "all";

        DescriptiveStatistics stats = new DescriptiveStatistics();
        stats.addValue(pkm);
        results.put(String.format("pkm.route.%s", purpose), stats);

    }

}

From source file:playground.johannes.gsv.synPop.analysis.PkmTaskSeason.java

@Override
public void analyze(Collection<ProxyPerson> persons, Map<String, DescriptiveStatistics> results) {
    Set<String> seasons = new HashSet<String>();
    for (ProxyPerson person : persons) {
        String month = (String) person.getAttribute(MIDKeys.PERSON_MONTH);
        if (month != null) {
            String season = "winter";

            if (month.equalsIgnoreCase(MIDKeys.APRIL)) {
                season = "summer";
            } else if (month.equalsIgnoreCase(MIDKeys.MAY)) {
                season = "summer";
            } else if (month.equalsIgnoreCase(MIDKeys.JUNE)) {
                season = "summer";
            } else if (month.equalsIgnoreCase(MIDKeys.JULY)) {
                season = "summer";
            } else if (month.equalsIgnoreCase(MIDKeys.AUGUST)) {
                season = "summer";
            } else if (month.equalsIgnoreCase(MIDKeys.SEPTEMBER)) {
                season = "summer";
            } else if (month.equalsIgnoreCase(MIDKeys.OCTOBER)) {
                season = "summer";
            }//from  w ww. j  a v  a 2  s  .  c  o m
            seasons.add(season);
        }
    }

    //      purposes.add(null);

    for (String season : seasons) {
        double pkm = 0;
        for (ProxyPerson person : persons) {
            String theSeason = person.getAttribute(CommonKeys.ACTIVITY_TYPE);

            ProxyPlan plan = person.getPlans().get(0);

            for (int i = 1; i < plan.getLegs().size(); i++) {
                ProxyObject leg = plan.getLegs().get(i);
                if (mode == null || mode.equalsIgnoreCase(leg.getAttribute(CommonKeys.LEG_MODE))) {
                    //                  ProxyObject act = plan.getActivities().get(i + 1);
                    if (season == null || season.equalsIgnoreCase(theSeason)) {
                        String value = leg.getAttribute(CommonKeys.LEG_ROUTE_DISTANCE);
                        if (value != null) {
                            pkm += Double.parseDouble(value);
                        }
                    }
                }
            }

        }

        if (season == null)
            season = "all";

        DescriptiveStatistics stats = new DescriptiveStatistics();
        stats.addValue(pkm);
        results.put(String.format("pkm.route.%s", season), stats);

    }

}

From source file:playground.johannes.gsv.synPop.analysis.SpeedFactorAnalyzer.java

@Override
public void analyze(Collection<ProxyPerson> persons, Map<String, DescriptiveStatistics> results) {
    Set<String> modes = new HashSet<String>();
    for (ProxyPerson person : persons) {
        ProxyPlan plan = person.getPlan();
        for (int i = 0; i < plan.getLegs().size(); i++) {
            modes.add(plan.getLegs().get(i).getAttribute(CommonKeys.LEG_MODE));
        }/*from  w  ww.j  ava  2s.  c  o m*/
    }

    modes.add(null);

    for (String mode : modes) {
        TDoubleArrayList distances = new TDoubleArrayList(persons.size() * 3);
        TDoubleArrayList durations = new TDoubleArrayList(persons.size() * 3);

        double sumDist = 0;
        double sumDur = 0;
        for (ProxyPerson person : persons) {
            ProxyPlan plan = person.getPlan();
            for (ProxyObject leg : plan.getLegs()) {
                if (mode == null || mode.equalsIgnoreCase(leg.getAttribute(CommonKeys.LEG_MODE))) {
                    String distVal = leg.getAttribute(CommonKeys.LEG_ROUTE_DISTANCE);
                    String startVal = leg.getAttribute(CommonKeys.LEG_START_TIME);
                    String endVal = leg.getAttribute(CommonKeys.LEG_END_TIME);

                    if (distVal != null && startVal != null && endVal != null) {
                        double dist = Double.parseDouble(distVal);
                        double start = Double.parseDouble(startVal);
                        double end = Double.parseDouble(endVal);

                        double tt = end - start;
                        if (tt > 0) {
                            distances.add(dist);
                            durations.add(tt);

                            sumDist += dist;
                            sumDur += tt;
                        }
                    }
                }
            }
        }

        if (mode == null)
            mode = "all";

        String key = String.format("%s.%s", KEY, mode);

        double factor = sumDist / sumDur;

        DescriptiveStatistics stats = new DescriptiveStatistics();
        stats.addValue(factor);
        results.put(key, stats);

        if (outputDirectoryNotNull()) {

            TDoubleDoubleHashMap map = Correlations.mean(distances.toNativeArray(), durations.toNativeArray(),
                    new LinearDiscretizer(1000));
            try {
                TXTWriter.writeMap(map, "Distance", "Traveltime", getOutputDirectory() + key + ".txt");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

From source file:playground.johannes.gsv.synPop.analysis.TripDayVolumeTask.java

@Override
public void analyze(Collection<ProxyPerson> persons, Map<String, DescriptiveStatistics> results) {
    TObjectIntHashMap<String> values = new TObjectIntHashMap<>();

    for (ProxyPerson person : persons) {
        String day = person.getAttribute(CommonKeys.DAY);
        for (ProxyPlan plan : person.getPlans()) {

            int cnt = 0;
            for (ProxyObject leg : plan.getLegs()) {
                if (mode.equalsIgnoreCase(leg.getAttribute(CommonKeys.LEG_MODE))) {
                    cnt++;/*from  www  .j  av  a  2 s.c  o m*/
                }
            }
            values.adjustOrPutValue(day, cnt, cnt);
        }
    }

    TObjectIntIterator<String> it = values.iterator();
    for (int i = 0; i < values.size(); i++) {
        it.advance();
        DescriptiveStatistics tmp = new DescriptiveStatistics();
        tmp.addValue(it.value());
        results.put(it.key(), tmp);
    }

}

From source file:playground.johannes.gsv.synPop.mid.analysis.SeasonsTask.java

@Override
public void analyze(Collection<ProxyPerson> persons, Map<String, DescriptiveStatistics> results) {
    TObjectIntHashMap<String> map = new TObjectIntHashMap<String>();

    for (ProxyPerson person : persons) {
        String month = person.getAttribute(MIDKeys.PERSON_MONTH);
        String season = "NA";
        if (MIDKeys.NOVEMBER.equalsIgnoreCase(month)) {
            season = "win";
        } else if (MIDKeys.DECEMBER.equalsIgnoreCase(month)) {
            season = "win";
        } else if (MIDKeys.JANUARY.equalsIgnoreCase(month)) {
            season = "win";
        } else if (MIDKeys.FEBRUARY.equalsIgnoreCase(month)) {
            season = "win";
        } else if (MIDKeys.MARCH.equalsIgnoreCase(month)) {
            season = "win";
        } else if (month != null) {
            season = "sum";
        }/*from www .  j ava 2 s  .com*/

        String day = person.getAttribute(CommonKeys.DAY);
        String week = "wkday";
        if (CommonKeys.SATURDAY.equalsIgnoreCase(day)) {
            week = "wkend";
        } else if (CommonKeys.SUNDAY.equalsIgnoreCase(day)) {
            week = "wkend";
        }

        Set<String> modes = new HashSet<String>();
        for (ProxyObject leg : person.getPlan().getLegs()) {
            modes.add(leg.getAttribute(CommonKeys.LEG_MODE));
        }

        for (String mode : modes) {
            StringBuilder key = new StringBuilder(100);
            key.append(season);
            key.append(".");
            key.append(day);
            key.append(".");
            key.append(mode);

            map.adjustOrPutValue(key.toString(), 1, 1);

            key = new StringBuilder(100);
            key.append(season);
            key.append(".");
            key.append(week);
            key.append(".");
            key.append(mode);

            map.adjustOrPutValue(key.toString(), 1, 1);
        }
    }

    TObjectIntIterator<String> it = map.iterator();
    for (int i = 0; i < map.size(); i++) {
        it.advance();
        DescriptiveStatistics stats = new DescriptiveStatistics();
        stats.addValue(it.value());
        results.put(it.key(), stats);
    }

}

From source file:playground.johannes.mz2005.analysis.ActivityChainsTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    TObjectDoubleHashMap<String> chains = new TObjectDoubleHashMap<String>();

    for (Trajectory trajectory : trajectories) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < trajectory.getElements().size(); i += 2) {
            String type = ((Activity) trajectory.getElements().get(i)).getType();
            builder.append(type);/*from   w  w  w .  j av a 2s. co m*/
            builder.append("-");
        }

        String chain = builder.toString();
        chains.adjustOrPutValue(chain, 1, 1);

        stats.addValue((trajectory.getElements().size() + 1) / 2);
    }

    results.put(KEY, stats);

    if (outputDirectoryNotNull()) {
        try {
            TXTWriter.writeMap(chains, "chain", "n", getOutputDirectory() + "/actchains.txt", true);

            writeHistograms(stats, new DummyDiscretizer(), KEY, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:playground.johannes.mz2005.analysis.EscortsActivtyTypeTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    Map<String, DescriptiveStatistics> statsMap = new HashMap<String, DescriptiveStatistics>();
    for (Trajectory trajectory : trajectories) {
        for (int i = 2; i < trajectory.getElements().size(); i += 2) {
            Activity destination = (Activity) trajectory.getElements().get(i);
            int escorts = escortData.getEscorts(trajectory.getPerson(), i - 1);

            //            if (escorts > 0) {
            DescriptiveStatistics stats = statsMap.get(destination.getType());
            if (stats == null) {
                stats = new DescriptiveStatistics();
                statsMap.put(destination.getType(), stats);
            }//from   www  .  ja  va 2  s .  c o m

            stats.addValue(escorts);
            //            }
        }
    }

    try {
        TXTWriter.writeStatistics(statsMap, getOutputDirectory() + "/escorts_type.txt");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:playground.johannes.mz2005.analysis.TripDistEscortTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    TDoubleObjectHashMap<DescriptiveStatistics> statsMap = new TDoubleObjectHashMap<DescriptiveStatistics>();

    for (Trajectory trajectory : trajectories) {
        for (int i = 2; i < trajectory.getElements().size(); i += 2) {
            Activity destination = (Activity) trajectory.getElements().get(i);

            Id id = destination.getFacilityId();
            Coord dest = facilities.getFacilities().get(id).getCoord();

            Activity origin = (Activity) trajectory.getElements().get(i - 2);
            id = origin.getFacilityId();
            ActivityFacility fac = facilities.getFacilities().get(id);
            Coord source = fac.getCoord();

            double d = calculator.distance(MatsimCoordUtils.coordToPoint(source),
                    MatsimCoordUtils.coordToPoint(dest));

            int escorts = escortData.getEscorts(trajectory.getPerson(), i - 1);

            DescriptiveStatistics stats = statsMap.get(escorts);
            if (stats == null) {
                stats = new DescriptiveStatistics();
                statsMap.put(escorts, stats);
            }/*from  ww  w . j av  a 2s  . com*/

            if (d > 0)
                stats.addValue(d);
        }
    }

    try {
        TXTWriter.writeStatistics(statsMap, "escorts", getOutputDirectory() + "/d_trip_escorts.txt");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

protected void calcCloseness() {
    double sum = 0;
    double wsum = 0;
    closenessValues = new DescriptiveStatistics();
    for (SparseVertex v : graph.getVertices()) {
        if (isSampled) {
            if (!((SampledVertex) graphDecorator.getVertex(v)).isAnonymous())
                closenessValues.addValue(((CentralityVertex) v).getCloseness());
            double p = ((SampledVertex) graphDecorator.getVertex(v)).getSampleProbability();
            sum += ((CentralityVertex) v).getCloseness() / p;
            wsum += 1 / p;/*  www .  jav a2  s  . c o  m*/
        } else {
            closenessValues.addValue(((CentralityVertex) v).getCloseness());
            sum += ((CentralityVertex) v).getCloseness();
            wsum++;
        }
    }

    closenessWeighted = sum / wsum;
}

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

protected void calcBetweenness() {
    betweennessWeighted = 0;// ww w  .j a  va 2s.com
    double wsum = 0;
    betweennessValues = new DescriptiveStatistics();
    betweennessWValues = new TDoubleArrayList(graph.getVertices().size());
    betweennessWeights = new TDoubleArrayList(graph.getVertices().size());
    for (SparseVertex v : graph.getVertices()) {
        if (isSampled) {
            if (!((SampledVertex) graphDecorator.getVertex(v)).isAnonymous()) {
                betweennessValues.addValue(((CentralityVertex) v).getBetweenness());
                double p = ((SampledVertex) graphDecorator.getVertex(v)).getSampleProbability();
                betweennessWeighted += ((CentralityVertex) v).getBetweenness() / p;
                wsum += 1 / p;

                betweennessWValues.add(((CentralityVertex) v).getBetweenness() / p);
                betweennessWeights.add(1 / p);
            }
        } else {
            betweennessValues.addValue(((CentralityVertex) v).getBetweenness());
            betweennessWeighted += ((CentralityVertex) v).getBetweenness();
            wsum++;

            betweennessWValues.add(((CentralityVertex) v).getBetweenness());
            betweennessWeights.add(1.0);
        }
    }

    betweennessWeighted = betweennessWeighted / wsum;
}