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: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).//from w w w. ja  v a2s. 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) {
//      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(//  w ww .  j av a 2 s .  c  o m
                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.matrix.MatrixCentrality.java

/**
 * Builds the complete shortest path tree for all vertices and
 * simultaneously calculates closeness, betweenness, radius and diameter.
 * /*  w  w  w .j a  v  a  2 s .  c  o m*/
 * @param y an adjacency matrix.
 */
public void run(AdjacencyMatrix<?> y, int[] sources, int[] targets) {
    int n = y.getVertexCount();
    vertexCloseness = new double[n];
    Arrays.fill(vertexCloseness, Double.POSITIVE_INFINITY);
    vertexBetweenness = new double[n];
    edgeBetweenness = new TIntDoubleHashMap[n];
    diameter = 0;
    radius = Integer.MAX_VALUE;
    /*
     * create threads
     */
    if (dijkstraFactory == null)
        dijkstraFactory = new DijkstraFactory();

    List<CentralityThread> threads = new ArrayList<CentralityThread>();
    int size = (int) Math.floor(sources.length / (double) numThreads);
    int i_start = 0;
    int i_stop = size;
    for (int i = 0; i < numThreads - 1; i++) {
        int[] subSources = Arrays.copyOfRange(sources, i_start, i_stop);
        threads.add(new CentralityThread(y, subSources, targets, dijkstraFactory, calcBetweenness));
        i_start = i_stop;
        i_stop += size;
    }
    int[] subSources = Arrays.copyOfRange(sources, i_start, sources.length);
    threads.add(new CentralityThread(y, subSources, targets, dijkstraFactory, calcBetweenness));
    /*
     * start threads
     */
    for (CentralityThread thread : threads) {
        thread.start();
    }
    /*
     * wait for threads
     */
    for (CentralityThread thread : threads) {
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /*
     * merge results of threads
     */
    apl = new DescriptiveStatistics();
    double aplSum = 0;
    double cnt = 0;
    for (CentralityThread thread : threads) {

        if (calcAPLDistribution) {
            for (int i = 0; i < thread.pathLengths.size(); i++) {
                apl.addValue(thread.pathLengths.get(i));
            }
        } else {
            for (int i = 0; i < thread.pathLengths.size(); i++) {
                aplSum += thread.pathLengths.get(i);
            }
        }

        cnt++;

        for (int i = 0; i < n; i++) {
            /*
             * if this thread did not calculate the closeness of i it
             * returns infinity
             */
            vertexCloseness[i] = Math.min(vertexCloseness[i], thread.vertexCloseness[i]);
            /*
             * merge vertex betweenness values
             */
            vertexBetweenness[i] += thread.vertexBetweenness[i];
            /*
             * merge edge betweenness values
             */
            if (thread.edgeBetweenness[i] != null) {
                TIntDoubleIterator it = thread.edgeBetweenness[i].iterator();
                for (int j = 0; j < thread.edgeBetweenness[i].size(); j++) {
                    it.advance();
                    /*
                     * since we have an undirected graph, merge betweenness
                     * for both edges
                     */
                    if (edgeBetweenness[i] == null)
                        edgeBetweenness[i] = new TIntDoubleHashMap();
                    edgeBetweenness[i].adjustOrPutValue(it.key(), it.value(), it.value());

                    if (edgeBetweenness[it.key()] == null)
                        edgeBetweenness[it.key()] = new TIntDoubleHashMap();
                    edgeBetweenness[it.key()].adjustOrPutValue(i, it.value(), it.value());
                }
            }
            /*
             * get diameter and radius
             */
            diameter = Math.max(diameter, thread.diameter);
            radius = Math.min(radius, thread.radius);
        }
    }
    /*
     * calculate mean values
     */
    meanVertexCloseness = StatUtils.mean(vertexCloseness);

    if (!calcAPLDistribution)
        apl.addValue(aplSum / cnt);

    double sum = 0;
    for (int i = 0; i < y.getVertexCount(); i++)
        sum += vertexBetweenness[i];
    meanVertexBetweenness = sum / (double) y.getVertexCount();

    sum = 0;
    double count = 0;
    for (int i = 0; i < n; i++) {
        if (edgeBetweenness[i] != null) {
            TIntDoubleIterator it = edgeBetweenness[i].iterator();
            for (int k = 0; k < edgeBetweenness[i].size(); k++) {
                it.advance();
                sum += it.value();
                count++;
            }
        }
    }
    meanEdgeBetweenness = sum / count;
}

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

@SuppressWarnings("unchecked")
@Override/*from   w w  w.  ja  va2 s  .  c o  m*/
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);//from   w  w  w .  j a v a2 s.  co  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  w  w  . j a v  a 2 s  .  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()));
    }/*  w  w w .j a  v a  2 s.  c  om*/

    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;
}

From source file:org.matsim.contrib.socnetgen.sna.snowball.analysis.BridgeEdgeTask.java

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

    int count = 0;

    for (SampledEdge e : graph.getEdges()) {
        SampledVertex v1 = e.getVertices().getFirst();
        SampledVertex v2 = e.getVertices().getSecond();

        if (v1.getSeed() != v2.getSeed())
            count++;/*  w w  w  .  j  ava 2  s  .  c  o m*/
    }

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

    printStats(stats, KEY);
    results.put(KEY, stats);
}

From source file:org.matsim.contrib.socnetgen.sna.snowball.analysis.EstimatorTask.java

@Override
public void analyze(Graph graph, Map<String, DescriptiveStatistics> stats) {
    TIntDoubleHashMap probas = new TIntDoubleHashMap(graph.getVertices().size());
    TIntIntHashMap counts = new TIntIntHashMap(graph.getVertices().size());

    double N_estim = 0;
    for (Vertex vertex : graph.getVertices()) {
        if (((SampledVertex) vertex).isSampled()) {
            int k = vertex.getNeighbours().size();
            double p = estimator.probability((SampledVertex) vertex);

            probas.adjustOrPutValue(k, p, p);
            counts.adjustOrPutValue(k, 1, 1);

            if (p > 0)
                N_estim += 1 / p;//from w ww. j  a v a  2s .  c  o m
        }
    }
    DescriptiveStatistics ds = new DescriptiveStatistics();
    ds.addValue(N_estim);
    stats.put("N_estim", ds);

    double M_estim = 0;
    for (Edge edge : graph.getEdges()) {
        SampledVertex v_i = (SampledVertex) edge.getVertices().getFirst();
        SampledVertex v_j = (SampledVertex) edge.getVertices().getSecond();
        if (v_i.isSampled() && v_j.isSampled()) {
            double p_i = estimator.probability(v_i);
            double p_j = estimator.probability(v_j);
            if (p_i > 0 && p_j > 0) {
                M_estim += 1 / ((p_i + p_j) - (p_i * p_j));
                //               M_estim += 1/(p_i * p_j);
            }
        }
    }
    ds = new DescriptiveStatistics();
    ds.addValue(M_estim);
    stats.put("M_estim", ds);

    TIntDoubleIterator it = probas.iterator();
    for (int i = 0; i < probas.size(); i++) {
        it.advance();
        it.setValue(it.value() / counts.get(it.key()));
    }

    writeValues(probas, "proba");
}

From source file:org.matsim.contrib.socnetgen.sna.snowball.analysis.ObservedTransitivity.java

/**
 * Returns a descriptive statistics object containing the local clustering
 * coefficient of all sampled vertices that have been sampled up to and
 * including the next to last iteration.
 * /*  w ww .j a  va 2s . com*/
 * @param vertices
 *            a set of sampled vertices
 * @return a descriptive statistics object.
 */
@SuppressWarnings("unchecked")
@Override
public DescriptiveStatistics statistics(Set<? extends Vertex> vertices) {
    DescriptiveStatistics stats = new DescriptiveStatistics();
    int iteration = SnowballStatistics.getInstance().lastIteration((Set<? extends SampledVertex>) vertices);

    TObjectDoubleHashMap<? extends Vertex> coefficients = localClusteringCoefficients(vertices);
    TObjectDoubleIterator<? extends Vertex> it = coefficients.iterator();

    for (int i = 0; i < coefficients.size(); i++) {
        it.advance();
        if (((SampledVertex) it.key()).getIterationSampled() <= iteration - 1) {
            stats.addValue(it.value());
        }
    }

    return stats;
}