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

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

Introduction

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

Prototype

public void addValue(double v) 

Source Link

Document

Adds the value to the dataset.

Usage

From source file:org.matsim.contrib.socnetgen.sna.graph.analysis.Degree.java

/**
 * Returns a descriptive statistics object containing the elements of <tt>vertices</tt>. The
 * graph is treated as undirected./*w  w  w . j a va2s.  com*/
 * 
 * @param vertices
 *            a collection of vertices.
 *            
 * @return a descriptive statistics object.
 */
public DescriptiveStatistics statistics(Set<? extends Vertex> vertices) {
    DescriptiveStatistics distribution = new DescriptiveStatistics();
    for (Vertex v : vertices)
        distribution.addValue(v.getEdges().size());

    return distribution;
}

From source file:org.matsim.contrib.socnetgen.sna.graph.analysis.DegreeTask.java

/**
 * Determines the degree distribution and the degree correlation of a graph.
 * Writes the histogram of the degree distribution into the output directory
 * (if specified).//from w ww.ja  va 2s  . c om
 * 
 * @param graph
 *            a graph.
 * @param stats
 *            a map where the results of the analysis are stored.
 */
@Override
public void analyze(Graph graph, Map<String, DescriptiveStatistics> statsMap) {
    DescriptiveStatistics stats = module.statistics(graph.getVertices());
    printStats(stats, key);
    statsMap.put(key, stats);
    if (outputDirectoryNotNull()) {
        try {
            writeHistograms(stats, new LinearDiscretizer(1.0), key, false);
            writeHistograms(stats, new LinearDiscretizer(5.0), key + "_5", false);
            writeHistograms(stats, key, 13, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    stats = new DescriptiveStatistics();
    stats.addValue(module.assortativity(graph));
    statsMap.put("r_" + key, stats);
    printStats(stats, "r_" + key);
}

From source file:org.matsim.contrib.socnetgen.sna.graph.analysis.GraphSizeTask.java

/**
 * Counts the number of vertices and edges in a graph.
 * /*from w  w  w  . ja  v  a 2 s.  c  om*/
 * @param graph
 *            a graph.
 * @param stats
 *            a map where the results of the analysis are stored.
 */
//   @Override
//   public void analyze(Graph graph, Map<String, Double> stats) {
////      int n_vertex = graph.getVertices().size();
////      int n_edge = graph.getEdges().size();
////      stats.put(NUM_VERTICES, new Double(n_vertex));
////      stats.put(NUM_EDGES, new Double(n_edge));
////      logger.info(String.format("%1$s = %2$s, %3$s = %4$s", NUM_VERTICES, n_vertex, NUM_EDGES, n_edge));
//   }

@Override
public void analyze(Graph graph, Map<String, DescriptiveStatistics> statsMap) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    stats.addValue(graph.getVertices().size());
    statsMap.put(NUM_VERTICES, stats);
    printStats(stats, NUM_VERTICES);

    stats = new DescriptiveStatistics();
    stats.addValue(graph.getEdges().size());
    statsMap.put(NUM_EDGES, stats);
    printStats(stats, NUM_EDGES);
}

From source file:org.matsim.contrib.socnetgen.sna.graph.analysis.Transitivity.java

/**
 * Calculates the distribution of local clustering coefficients.
 * /* w  w w.  j a  v  a 2  s.c o m*/
 * @param vertices
 *            a set of vertices.
 * 
 * @return the distribution of local clustering coefficients.
 */
public DescriptiveStatistics localClusteringDistribution(Set<? extends Vertex> vertices) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    TObjectDoubleHashMap<?> values = values(vertices);
    TObjectDoubleIterator<?> it = values.iterator();
    for (int i = 0; i < values.size(); i++) {
        it.advance();
        stats.addValue(it.value());
    }
    return stats;
}

From source file:org.matsim.contrib.socnetgen.sna.graph.analysis.TransitivityTask.java

/**
 * Calculates the mean local clustering coefficient, the maximum local
 * clustering coefficient, the minimum local clustering coefficient and the
 * global clustering coefficient. Writes the histogram of the local
 * clustering coefficient distribution into the output directory (if
 * specified).//ww  w. j  av  a2s.  c o  m
 * 
 * @param graph
 *            a graph.
 * @param stats
 *            a map where the results of the analysis are stored.
 */
//   @Override
//   public void analyze(Graph graph, Map<String, Double> stats) {
//      DescriptiveStatistics distr = module.localClusteringDistribution(graph.getVertices());
//      double c_mean = distr.getMean();
//      double c_max = distr.getMax();
//      double c_min = distr.getMin();
//      stats.put(MEAN_LOCAL_CLUSTERING, c_mean);
//      stats.put(MAX_LOCAL_CLUSTERING, c_max);
//      stats.put(MIN_LOCAL_CLUSTERING, c_min);
//
//      double c_global = module.globalClusteringCoefficient(graph);
//      stats.put(GLOBAL_CLUSTERING_COEFFICIENT, c_global);
//
//      logger.info(String.format(
//            "c_local_mean = %1$.4f, c_local_max = %2$.4f, c_local_min = %3$.4f, c_global = %4$.4f.", c_mean, c_max,
//            c_min, c_global));
//
//      if (getOutputDirectory() != null) {
//         try {
//            writeHistograms(distr, new LinearDiscretizer(0.05), "c_local", false);
//         } catch (FileNotFoundException e) {
//            e.printStackTrace();
//         } catch (IOException e) {
//            e.printStackTrace();
//         }
//      }
//   }

public void analyze(Graph graph, Map<String, DescriptiveStatistics> statsMap) {
    String subKey = key + "_local";

    DescriptiveStatistics stats = module.statistics(graph.getVertices());
    printStats(stats, subKey);
    statsMap.put(subKey, stats);

    if (outputDirectoryNotNull()) {
        try {
            writeHistograms(stats, new LinearDiscretizer(0.05), subKey, false);
            writeHistograms(stats, subKey, 100, 20);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    subKey = key + "_global";
    stats = new DescriptiveStatistics();
    stats.addValue(module.globalClusteringCoefficient(graph));
    printStats(stats, subKey);
    statsMap.put(subKey, stats);
}

From source file:org.matsim.contrib.socnetgen.sna.graph.matrix.MatrixAPL.java

public DescriptiveStatistics apl(AdjacencyMatrix<?> y) {
    List<APLThread> threads = new ArrayList<APLThread>();
    int size = (int) Math.floor(y.getVertexCount() / (double) numThreads);
    int i_start = 0;
    int i_stop = size;
    for (int i = 0; i < numThreads - 1; i++) {
        threads.add(//from  ww w  . j  a  v  a  2  s .com
                new APLThread(y, new SingelPathDijkstra(y, new CostFunction()), i_start, i_stop, calcDistr));
        i_start = i_stop;
        i_stop += size;
    }
    threads.add(new APLThread(y, new SingelPathDijkstra(y, new CostFunction()), i_start, y.getVertexCount(),
            calcDistr));
    /*
     * start threads
     */
    logger.info(String.format("Calculating average path lenth on %1$s threads.", numThreads));
    ProgressLogger.init(y.getVertexCount(), 1, 5);
    for (APLThread thread : threads) {
        thread.start();
    }
    /*
     * wait for threads
     */
    for (APLThread thread : threads) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /*
     * merge results
     */
    DescriptiveStatistics stats = new DescriptiveStatistics();
    if (calcDistr) {
        for (APLThread thread : threads) {
            TDoubleArrayList vals = thread.getValues();
            for (int i = 0; i < vals.size(); i++) {
                stats.addValue(vals.get(i));
            }
        }
    } else {
        long lengthSum = 0;
        long pathCount = 0;
        for (APLThread thread : threads) {
            lengthSum += thread.lengthSum;
            pathCount += thread.pathCount;
        }
        stats.addValue(lengthSum / (double) pathCount);
    }

    return stats;
}

From source file:org.matsim.contrib.socnetgen.sna.graph.social.analysis.AgeTask.java

@SuppressWarnings("unchecked")
@Override/*from   w  w  w  . java2  s .com*/
public void analyze(Graph graph, Map<String, DescriptiveStatistics> statsMap) {
    DescriptiveStatistics stats = module.statistics(graph.getVertices());
    statsMap.put(key, stats);
    printStats(stats, key);

    if (outputDirectoryNotNull()) {
        try {
            writeHistograms(stats, new LinearDiscretizer(1.0), key, false);

            StatsWriter.writeHistogram(module.correlation((Set<? extends SocialVertex>) graph.getVertices()),
                    "age", "age_mean", getOutputDirectory() + "/age_age.mean.txt");

            TDoubleObjectHashMap<DescriptiveStatistics> stat = module
                    .boxplot((Set<? extends SocialVertex>) graph.getVertices());
            StatsWriter.writeBoxplotStats(stat, getOutputDirectory() + "age_age.table.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    stats = new DescriptiveStatistics();
    stats.addValue(module.correlationCoefficient((Set<? extends SocialEdge>) graph.getEdges()));
    statsMap.put("r_" + key, stats);
    printStats(stats, "r_" + key);
}

From source file:org.matsim.contrib.socnetgen.sna.graph.social.analysis.GenderTask.java

@Override
public void analyze(Graph g, Map<String, DescriptiveStatistics> statsMap) {
    SocialGraph graph = (SocialGraph) g;

    Map<SocialVertex, String> values = module.values(graph.getVertices());
    TObjectDoubleHashMap<String> hist = LinguisticHistogram.create(values.values());

    DescriptiveStatistics male = new DescriptiveStatistics();
    male.addValue(hist.get(Gender.MALE));
    String key = "n_male";
    statsMap.put(key, male);/* w  w  w.  jav a2s.c o  m*/
    printStats(male, key);

    DescriptiveStatistics female = new DescriptiveStatistics();
    female.addValue(hist.get(Gender.FEMALE));
    key = "n_female";
    statsMap.put(key, female);
    printStats(female, key);

    DescriptiveStatistics r = new DescriptiveStatistics();
    r.addValue(module.correlation(graph.getEdges()));
    key = "r_gender";
    statsMap.put(key, r);
    printStats(r, key);

    if (outputDirectoryNotNull()) {
        try {
            StatsWriter.writeLabeledHistogram(hist, "gender", "n",
                    String.format("%1$s/gender.txt", getOutputDirectory()));

            SocioMatrix<String> m = module.countsMatrix(graph.getVertices());
            m.toFile(String.format("%1$s/gender.countsMatrix.txt", getOutputDirectory()));

            SocioMatrixBuilder.normalizeTotalSum(m);
            m.toFile(String.format("%1$s/gender.countsMatrix.normTotal.txt", getOutputDirectory()));

            m = module.countsMatrix(graph.getVertices());
            SocioMatrixBuilder.normalizeRowSum(m);
            m.toFile(String.format("%1$s/gender.countsMatrix.normRow.txt", getOutputDirectory()));

            m = module.probaMatrix(graph.getVertices());
            m.toFile(String.format("%1$s/gender.probaMatrix.txt", getOutputDirectory()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.matsim.contrib.socnetgen.sna.graph.spatial.analysis.Distance.java

public DescriptiveStatistics statistics(Set<? extends SpatialVertex> vertices) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    for (double val : distribution(vertices).getValues()) {
        stats.addValue(val);
    }//from   w ww  .j av a2s  .  co  m
    return stats;
}

From source file:org.matsim.contrib.socnetgen.sna.graph.spatial.analysis.EdgeLengthMedian.java

private double edgeLengthMedian(SpatialVertex vertex) {
    if (calculator == null) {
        calculator = DistanceCalculatorFactory
                .createDistanceCalculator(CRSUtils.getCRS(vertex.getPoint().getSRID()));
    }//from w  ww  .ja va2  s .  c o  m

    DescriptiveStatistics stats = new DescriptiveStatistics();

    for (SpatialVertex neighbor : vertex.getNeighbours()) {
        if (neighbor.getPoint() != null) {
            if (vertex.getPoint().getSRID() == neighbor.getPoint().getSRID()) {
                stats.addValue(calculator.distance(vertex.getPoint(), neighbor.getPoint()));
            } else {
                throw new RuntimeException("Points do not share the same coordinate reference system.");
            }
        }
    }

    if (stats.getN() > 1)
        return stats.getPercentile(50);
    else
        return Double.NaN;
}