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.coopsim.analysis.AbstractTrajectoryProperty.java

public DescriptiveStatistics statistics(Set<? extends Trajectory> trajectories, boolean ignoreZeros) {
    if (!ignoreZeros)
        return this.statistics(trajectories);
    else {//from www.  j  ava 2s .co  m
        TObjectDoubleHashMap<Trajectory> values = values(trajectories);
        DescriptiveStatistics stats = new DescriptiveStatistics();

        int zeros = 0;
        TObjectDoubleIterator<Trajectory> it = values.iterator();
        for (int i = 0; i < values.size(); i++) {
            it.advance();
            if (it.value() > 0)
                stats.addValue(it.value());
            else
                zeros++;
        }

        if (zeros > 0)
            logger.debug(String.format("Ignored %1$s zero values.", zeros));

        return stats;
    }
}

From source file:playground.johannes.coopsim.analysis.AbstractTrajectoryProperty.java

@Override
public DescriptiveStatistics statistics(Set<? extends Trajectory> trajectories) {
    TObjectDoubleHashMap<Trajectory> values = values(trajectories);
    DescriptiveStatistics stats = new DescriptiveStatistics();

    TObjectDoubleIterator<Trajectory> it = values.iterator();
    for (int i = 0; i < values.size(); i++) {
        it.advance();/*from w w  w . j a v  a2s  .  c  o m*/
        stats.addValue(it.value());
    }

    return stats;
}

From source file:playground.johannes.coopsim.analysis.DesiredTimeDiffTask.java

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

    for (Trajectory t : trajectories) {
        Activity act = (Activity) t.getElements().get(2);
        String type = act.getType();
        if (!type.equals("idle")) {
            ActivityDesires desire = desires.get(t.getPerson());

            double desiredDuration = desire.getActivityDuration(type);
            double desiredStartTime = desire.getActivityStartTime(type);

            double realizedDuration = t.getTransitions().get(3) - t.getTransitions().get(2);
            double realizedStartTime = t.getTransitions().get(2);

            durStats.addValue(Math.abs(realizedDuration - desiredDuration));
            arrStats.addValue(Math.abs(realizedStartTime - desiredStartTime));
        }//from   w  w w .  j  a  v a 2 s. c  om
    }
    results.put("dur_diff", durStats);
    results.put("arr_diff", arrStats);

    try {
        writeHistograms(durStats, "dur_diff", 50, 50);
        writeHistograms(arrStats, "arr_diff", 50, 50);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.johannes.coopsim.analysis.JointActivityTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    DescriptiveStatistics timeStatsAll = new DescriptiveStatistics();
    DescriptiveStatistics visitorStatsAll = new DescriptiveStatistics();
    Map<String, DescriptiveStatistics> timeStatsTypeMap = new HashMap<String, DescriptiveStatistics>();
    Map<String, DescriptiveStatistics> visitorStatsTypeMap = new HashMap<String, DescriptiveStatistics>();

    for (Trajectory t : trajectories) {
        Person person = t.getPerson();/*from w w  w . j av  a2  s  .  c  o m*/
        SocialVertex ego = personVertexMap.get(person);
        String type = ((Activity) t.getElements().get(2)).getType();

        DescriptiveStatistics timeStatsType = timeStatsTypeMap.get(type);
        DescriptiveStatistics visitorStatsType = visitorStatsTypeMap.get(type);

        if (timeStatsType == null) {
            timeStatsType = new DescriptiveStatistics();
            timeStatsTypeMap.put(type, timeStatsType);
            visitorStatsType = new DescriptiveStatistics();
            visitorStatsTypeMap.put(type, visitorStatsType);
        }

        int visitors = 0;
        List<Person> alterPersons = new ArrayList<Person>(ego.getNeighbours().size());
        for (SocialVertex alter : ego.getNeighbours()) {
            double time = tracker.timeOverlap(person, alter.getPerson().getPerson());
            if (time > 0) {
                timeStatsType.addValue(time);
                timeStatsAll.addValue(time);
            }

            alterPersons.add(alter.getPerson().getPerson());
        }

        visitors = tracker.metAlters(person, alterPersons);
        visitorStatsType.addValue(visitors);
        visitorStatsAll.addValue(visitors);
    }

    String timeKey = "t_joint";
    results.put(timeKey, timeStatsAll);
    for (Entry<String, DescriptiveStatistics> entry : timeStatsTypeMap.entrySet()) {
        results.put("t_joint_" + entry.getKey(), entry.getValue());
    }

    String visitorKey = "visitors";
    results.put(visitorKey, visitorStatsAll);
    for (Entry<String, DescriptiveStatistics> entry : visitorStatsTypeMap.entrySet()) {
        results.put("visitors_" + entry.getKey(), entry.getValue());
    }

    if (outputDirectoryNotNull()) {
        try {
            writeHistograms(timeStatsAll, timeKey, 50, 50);
            writeHistograms(visitorStatsAll, new DummyDiscretizer(), visitorKey, false);

            for (Entry<String, DescriptiveStatistics> entry : timeStatsTypeMap.entrySet()) {
                writeHistograms(entry.getValue(), "t_joint_" + entry.getKey(), 50, 50);
            }

            for (Entry<String, DescriptiveStatistics> entry : visitorStatsTypeMap.entrySet()) {
                writeHistograms(entry.getValue(), new DummyDiscretizer(), "visitors_" + entry.getKey(), false);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:playground.johannes.coopsim.analysis.LegFrequencyTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    Set<String> modes = TrajectoryUtils.getModes(trajectories);
    modes.add(null);/*from w ww.ja va2  s  .c  o  m*/

    for (String mode : modes) {
        PlanElementCondition<Leg> condition;
        if (mode != null) {
            condition = new LegModeCondition(mode);
        } else {
            condition = DefaultCondition.getInstance();
        }

        DescriptiveStatistics stats = new DescriptiveStatistics();
        for (Trajectory t : trajectories) {
            int cnt = 0;
            for (int i = 1; i < t.getElements().size(); i += 2) {
                if (condition.test(t, (Leg) t.getElements().get(i), i)) {
                    cnt++;
                }
            }
            stats.addValue(cnt);
        }

        String key;
        if (mode == null)
            key = String.format("%s.all", KEY, mode);
        else
            key = String.format("%s.%s", KEY, mode);

        results.put(key, stats);

        if (outputDirectoryNotNull()) {
            try {
                writeHistograms(stats, DummyDiscretizer.getInstance(), key, false);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

From source file:playground.johannes.coopsim.analysis.PersonAgeTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (Trajectory t : trajectories) {
        stats.addValue(((PersonImpl) t.getPerson()).getAge());
    }/*from  w w  w .  j a  v  a  2 s.c  o m*/

    results.put("person_age", stats);
    try {
        writeHistograms(stats, new DummyDiscretizer(), "age", false);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.johannes.coopsim.analysis.PkmRouteTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    Map<String, PlanElementConditionComposite<Leg>> conditions = Conditions.getLegConditions(trajectories);

    for (Entry<String, PlanElementConditionComposite<Leg>> entry : conditions.entrySet()) {
        double sum = 0;
        int cnt = 0;
        for (Trajectory t : trajectories) {
            for (int i = 1; i < t.getElements().size(); i += 2) {
                Leg leg = (Leg) t.getElements().get(i);
                if (entry.getValue().test(t, leg, i)) {
                    if (leg.getRoute() instanceof NetworkRoute) {
                        NetworkRoute route = (NetworkRoute) leg.getRoute();
                        sum += RouteUtils.calcDistance(route, network);

                        Link startLink = network.getLinks().get(route.getStartLinkId());
                        sum += startLink.getLength() * startEndFactor;

                        Link endLink = network.getLinks().get(route.getEndLinkId());
                        sum += endLink.getLength() * startEndFactor;
                    } else {
                        sum += leg.getRoute().getDistance();

                        if (leg.getMode().equals("car")) {
                            cnt++;//from w ww  .  j  a v  a2 s  .com
                        }
                    }
                }
            }

        }

        DescriptiveStatistics stats = new DescriptiveStatistics();
        stats.addValue(sum);

        results.put(String.format("%s.%s.%.2f", KEY, entry.getKey(), startEndFactor), stats);

        if (cnt > 0) {
            logger.warn(String.format("mode=car, type=%s: %s not NetworkRoute", entry.getKey(), cnt));
        }
    }

}

From source file:playground.johannes.coopsim.analysis.ScoreTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    DescriptiveStatistics allScores = new DescriptiveStatistics();
    for (Trajectory t : trajectories)
        allScores.addValue(t.getPerson().getSelectedPlan().getScore());
    results.put("score", allScores);

    DescriptiveStatistics actScores = ActivityEvaluator.stopLogging();
    results.put("score_act", actScores);

    DescriptiveStatistics legScores = LegEvaluator.stopLogging();
    results.put("score_leg", legScores);

    Map<String, DescriptiveStatistics> jointScore = JointActivityEvaluator2.stopLogging();
    //      Map<String, DescriptiveStatistics> jointScore = JointActivityEvaluator.stopLogging();
    for (Entry<String, DescriptiveStatistics> entry : jointScore.entrySet()) {
        results.put("score_join_" + entry.getKey(), entry.getValue());
    }//from  w  w  w.j a  v  a 2s  .c om

    DescriptiveStatistics typeScore = ActivityTypeEvaluator.stopLogging();
    results.put("score_type", typeScore);

    try {
        writeHistograms(allScores, "score", 50, 50);
        writeHistograms(actScores, "score_act", 50, 50);
        writeHistograms(legScores, "score_leg", 50, 50);
        for (Entry<String, DescriptiveStatistics> entry : jointScore.entrySet()) {
            writeHistograms(entry.getValue(), new LinearDiscretizer(0.5), "score_join_" + entry.getKey(),
                    false);
            writeHistograms(entry.getValue(), "score_join_" + entry.getKey(), 50, 50);
        }

        writeHistograms(typeScore, new DummyDiscretizer(), "score_type", false);
    } catch (IOException e) {
        e.printStackTrace();
    }

    scores.add(allScores.getMean());

    if (scores.size() >= MIN_SAMPLES) {
        SimpleRegression reg = new SimpleRegression();

        for (int i = scores.size() - MIN_SAMPLES; i < scores.size(); i++) {
            reg.addData(i, scores.get(i));
        }

        if (reg.getSlope() < THRESHOLD)
            converged = true;
    }

}

From source file:playground.johannes.coopsim.analysis.SocialTripCorrelationTask.java

@Override
public void analyze(Set<Trajectory> trajectories, Map<String, DescriptiveStatistics> results) {
    TDoubleArrayList xvals = new TDoubleArrayList(trajectories.size());
    TDoubleArrayList yvals = new TDoubleArrayList(trajectories.size());

    for (Trajectory t : trajectories) {
        SocialVertex v = vertexMapping.get(t.getPerson());
        double val_v = getValue(v);

        List<Person> alters = new ArrayList<Person>(v.getNeighbours().size());
        for (SocialVertex w : v.getNeighbours())
            alters.add(w.getPerson().getPerson());

        for (Person alter : alters) {
            if (tracker.timeOverlap(t.getPerson(), alter) > 0) {
                double val_w = getValue(vertexMapping.get(alter));

                xvals.add(val_v);
                yvals.add(val_w);
            }/*  w ww . j  a  v a  2 s.c  o m*/
        }
    }

    double r = new PearsonsCorrelation().correlation(xvals.toNativeArray(), yvals.toNativeArray());
    String key2 = String.format("r_trip_%1$s", key);
    DescriptiveStatistics stats = new DescriptiveStatistics();
    stats.addValue(r);
    results.put(key2, stats);

    try {
        TDoubleDoubleHashMap correl = Correlations.mean(xvals.toNativeArray(), yvals.toNativeArray());
        TXTWriter.writeMap(correl, key, key, String.format("%1$s/%2$s.txt", getOutputDirectory(), key2));

        TDoubleObjectHashMap<DescriptiveStatistics> table = Correlations.statistics(xvals.toNativeArray(),
                yvals.toNativeArray(), new DummyDiscretizer());
        TXTWriter.writeBoxplotStats(table, String.format("%1$s/%2$s.table.txt", getOutputDirectory(), key2));
        TXTWriter.writeScatterPlot(table, String.format("%1$s/%2$s.xy.txt", getOutputDirectory(), key2));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:playground.johannes.coopsim.analysis.TransitionProbaAnalyzer.java

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

    double pi = mentalEngine.getTotalPiSum() / (double) dumpInterval;

    stats.addValue(pi);//from  ww  w  .ja  v a  2  s  . c  o  m

    results.put("pi", stats);

    mentalEngine.cleatTotalPiSum();
}