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

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

Introduction

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

Prototype

public double getMax() 

Source Link

Document

Returns the maximum of the available values

Usage

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

protected TObjectDoubleHashMap<String> getStatisticsMap(DescriptiveStatistics stats) {
    TObjectDoubleHashMap<String> statsMap = new TObjectDoubleHashMap<String>();
    statsMap.put(MIN_KEY, stats.getMin());
    statsMap.put(MAX_KEY, stats.getMax());
    statsMap.put(MEAN_KEY, stats.getMean());
    statsMap.put(VARIANCE_KEY, stats.getVariance());
    statsMap.put(SKEWNESS_KEY, stats.getSkewness());
    statsMap.put(KURTOSIS_KEY, stats.getKurtosis());
    return statsMap;
}

From source file:playground.johannes.socialnetworks.snowball2.sim.EstimatorTest.java

private void analyze(Graph graph, String output) throws IOException {
    final int N = graph.getVertices().size();
    DescriptiveStatistics k_distr = Degree.getInstance().statistics(graph.getVertices());
    final int k_max = (int) k_distr.getMax();
    /*//www.  j a  v a2  s  . c o  m
     * initialize arrays
     */
    TDoubleDoubleHashMap kHist = Histogram.createHistogram(k_distr, new LinearDiscretizer(1.0), false);
    TDoubleDoubleHashMap[] pObs_k = new TDoubleDoubleHashMap[maxIteration + 1];
    TDoubleDoubleHashMap[] wObs_k = new TDoubleDoubleHashMap[maxIteration + 1];
    TDoubleDoubleHashMap[] pEstim_k = new TDoubleDoubleHashMap[maxIteration + 1];
    TDoubleDoubleHashMap[] wEstim_k = new TDoubleDoubleHashMap[maxIteration + 1];
    TDoubleDoubleHashMap[] N_estim_hist = new TDoubleDoubleHashMap[maxIteration + 1];
    TDoubleDoubleHashMap[] M_estim_hist = new TDoubleDoubleHashMap[maxIteration + 1];
    double[] mse_p = new double[maxIteration + 1];
    double[] mse_w = new double[maxIteration + 1];
    double[] bias_p = new double[maxIteration + 1];
    double[] bias_w = new double[maxIteration + 1];
    Distribution[] p_ratio = new Distribution[maxIteration + 1];
    Distribution[] w_ratio = new Distribution[maxIteration + 1];
    double[] N_estim_mean = new double[maxIteration + 1];
    double[] M_estim_mean = new double[maxIteration + 1];

    int[] samples = new int[maxIteration + 1];
    TIntIntHashMap[] samples_k = new TIntIntHashMap[maxIteration + 1];
    /*
     * boxplot for probability to sample k
     */
    List<TIntObjectHashMap<TDoubleArrayList>> pObsList_k = new ArrayList<TIntObjectHashMap<TDoubleArrayList>>(
            maxIteration + 1);
    for (int i = 0; i < maxIteration + 1; i++) {
        pObsList_k.add(new TIntObjectHashMap<TDoubleArrayList>());
    }
    int maxListSize = 0;
    /*
     * iterate over all vertices
     */
    for (Vertex v : graph.getVertices()) {
        int k = v.getNeighbours().size();
        int[] vertexCount = vertexCounts.get(v);
        double[] estimProba = vertexProbas.get(v);
        double[] estimWeigth = vertexWeights.get(v);
        /*
         * iterate over all snowball iterations
         */
        for (int i = 0; i <= maxIteration; i++) {
            int n_i = vertexCount[i];
            double pObs_i = n_i / (double) nSimulations[i];

            double pRnd_i = nSampledVertices[i] / ((double) nSimulations[i] * N);
            /*
             * initialize arrays
             */
            if (pObs_k[i] == null) {
                samples_k[i] = new TIntIntHashMap();
                pObs_k[i] = new TDoubleDoubleHashMap();
                wObs_k[i] = new TDoubleDoubleHashMap();
                pEstim_k[i] = new TDoubleDoubleHashMap();
                wEstim_k[i] = new TDoubleDoubleHashMap();
                p_ratio[i] = new Distribution();
                w_ratio[i] = new Distribution();
            }
            /*
             * accumulate samples
             */
            pObs_k[i].adjustOrPutValue(k, pObs_i, pObs_i);

            if (n_i > 0) {
                double wObs_i = 1 / pObs_i;
                wObs_k[i].adjustOrPutValue(k, wObs_i, wObs_i);

                samples[i]++;

                double pEstimMean = estimProba[i] / (double) n_i;
                double wEstimMean = estimWeigth[i] / (double) n_i;
                //               double wEstimMean = 1/pEstimMean;
                double diff_p = pObs_i - pEstimMean;
                double diff_w = wObs_i - wEstimMean;

                mse_p[i] += diff_p * diff_p;
                mse_w[i] += diff_w * diff_w;

                bias_p[i] += Math.abs(pObs_i - pRnd_i);
                bias_w[i] += Math.abs(wObs_i - 1 / pRnd_i);

                pEstim_k[i].adjustOrPutValue(k, pEstimMean, pEstimMean);
                wEstim_k[i].adjustOrPutValue(k, wEstimMean, wEstimMean);
                samples_k[i].adjustOrPutValue(k, 1, 1);

                p_ratio[i].add(pEstimMean / pObs_i);
                w_ratio[i].add(wEstimMean / wObs_i);
            }
            /*
             * boxplot for probability to sample k
             */
            TIntObjectHashMap<TDoubleArrayList> k_table = pObsList_k.get(i);
            TDoubleArrayList list = k_table.get(k);

            if (list == null) {
                list = new TDoubleArrayList(nSimulations[i]);
                k_table.put(k, list);
            }

            list.add(pObs_i);
            maxListSize = Math.max(maxListSize, list.size());
        }
    }
    /*
     * iterate over all edges
     */
    TIntObjectHashMap<double[][]> p_edge_estim_it = new TIntObjectHashMap<double[][]>();
    TIntObjectHashMap<double[][]> p_edge_obs_it = new TIntObjectHashMap<double[][]>();
    TIntObjectHashMap<double[][]> p_edge_it = new TIntObjectHashMap<double[][]>();
    for (int it = 0; it <= maxIteration; it++) {
        double[][] p_edge_estim_k = new double[k_max + 1][k_max + 1];
        p_edge_estim_it.put(it, p_edge_estim_k);

        double[][] p_edge_obs_k = new double[k_max + 1][k_max + 1];
        p_edge_obs_it.put(it, p_edge_obs_k);

        double[][] p_edge_k = new double[k_max + 1][k_max + 1];
        p_edge_it.put(it, p_edge_k);

        int[][] n_edge_k = new int[k_max + 1][k_max + 1];

        for (Edge e : graph.getEdges()) {
            int cnt = edgeCounts.get(e)[it];
            if (cnt > 0) {
                double p_estim = edgeProbas.get(e)[it] / (double) cnt;
                double p_obs = cnt / (double) nSimulations[it];

                int k_i = e.getVertices().getFirst().getNeighbours().size();
                int k_j = e.getVertices().getSecond().getNeighbours().size();

                p_edge_estim_k[k_i][k_j] += p_estim;
                p_edge_estim_k[k_j][k_i] += p_estim;

                p_edge_obs_k[k_i][k_j] += p_obs;
                p_edge_obs_k[k_j][k_i] += p_obs;

                n_edge_k[k_i][k_j]++;
                n_edge_k[k_j][k_i]++;
            }
        }

        for (int i = 0; i < p_edge_estim_k.length; i++) {
            for (int j = i; j < p_edge_estim_k.length; j++) {
                if (i == j) {
                    p_edge_estim_k[i][j] = p_edge_estim_k[i][j] / (double) n_edge_k[i][j];
                    p_edge_obs_k[j][i] = p_edge_obs_k[j][i] / (double) n_edge_k[j][i];
                } else {

                    p_edge_estim_k[i][j] = p_edge_estim_k[i][j] / (double) n_edge_k[i][j];
                    p_edge_estim_k[j][i] = p_edge_estim_k[j][i] / (double) n_edge_k[j][i];

                    p_edge_obs_k[i][j] = p_edge_obs_k[i][j] / (double) n_edge_k[i][j];
                    p_edge_obs_k[j][i] = p_edge_obs_k[j][i] / (double) n_edge_k[j][i];
                }
            }
        }
        /*
         * shared plot
         */
        for (int i = 0; i < p_edge_estim_k.length; i++) {
            for (int j = 0; j < p_edge_estim_k.length; j++) {
                if (i < j) {
                    p_edge_k[i][j] = p_edge_estim_k[i][j];
                } else {
                    p_edge_k[i][j] = p_edge_obs_k[i][j];
                }
            }
        }
    }

    /*
     * calculate averages
     */
    for (int i = 0; i <= maxIteration; i++) {
        nSampledVertices[i] = (int) (nSampledVertices[i] / (double) nSimulations[i]);
        mse_p[i] = mse_p[i] / (double) samples[i];
        mse_w[i] = mse_w[i] / (double) samples[i];
        bias_p[i] = bias_p[i] / (double) samples[i];
        bias_w[i] = bias_w[i] / (double) samples[i];

        double[] values = N_estim[i].toNativeArray();
        N_estim_mean[i] = StatUtils.mean(values);
        N_estim_hist[i] = new Distribution(values)
                .absoluteDistribution((StatUtils.max(values) - StatUtils.min(values)) / 100.0);

        values = M_estim[i].toNativeArray();
        M_estim_mean[i] = StatUtils.mean(values);
        M_estim_hist[i] = new Distribution(values)
                .absoluteDistribution((StatUtils.max(values) - StatUtils.min(values)) / 100.0);

        if (pObs_k[i] != null) {
            TDoubleDoubleIterator it = pObs_k[i].iterator();
            for (int k = 0; k < pObs_k[i].size(); k++) {
                it.advance();
                it.setValue(it.value() / (double) kHist.get((int) it.key()));
            }
        }

        if (wObs_k[i] != null) {
            TDoubleDoubleIterator it = wObs_k[i].iterator();
            for (int k = 0; k < wObs_k[i].size(); k++) {
                it.advance();
                it.setValue(it.value() / (double) samples_k[i].get((int) it.key()));
            }
        }

        if (pEstim_k[i] != null) {
            TDoubleDoubleIterator it = pEstim_k[i].iterator();
            for (int k = 0; k < pEstim_k[i].size(); k++) {
                it.advance();
                it.setValue(it.value() / (double) samples_k[i].get((int) it.key()));
            }
        }

        if (wEstim_k[i] != null) {
            TDoubleDoubleIterator it = wEstim_k[i].iterator();
            for (int k = 0; k < wEstim_k[i].size(); k++) {
                it.advance();
                it.setValue(it.value() / (double) samples_k[i].get((int) it.key()));
            }
        }
    }
    /*
     * write data
     */
    writeIntArray(nSampledVertices, String.format("%1$s/n_sampled.txt", output), "it\tn");
    writeDoubleArray(mse_p, String.format("%1$s/mse_p.txt", output), "it\tmse");
    writeDoubleArray(mse_w, String.format("%1$s/mse_w.txt", output), "it\tmse");
    writeDoubleArray(bias_p, String.format("%1$s/bias_p.txt", output), "it\tbias");
    writeDoubleArray(bias_w, String.format("%1$s/bias_w.txt", output), "it\tbias");
    writeDoubleArray(N_estim_mean, String.format("%1$s/N_estim.txt", output), "it\tN_estim");
    writeDoubleArray(M_estim_mean, String.format("%1$s/M_estim.txt", output), "it\tM_estim");
    writeHistogramArray(pObs_k, output, "pobs");
    writeHistogramArray(wObs_k, output, "wobs");
    writeHistogramArray(pEstim_k, output, "pestim");
    writeHistogramArray(wEstim_k, output, "westim");
    writeHistogramArray(N_estim_hist, output, "N_estim");
    writeHistogramArray(M_estim_hist, output, "M_estim");

    writeDistributionArray(p_ratio, output, "p_ratio");
    writeDistributionBoxplot(p_ratio, output, "p_ratio_boxplot");
    writeDistributionArray(w_ratio, output, "w_ratio");
    writeDistributionBoxplot(w_ratio, output, "w_ratio_boxplot");

    writeMatrix(p_edge_obs_it, output, "pEdgeObs");
    writeMatrix(p_edge_estim_it, output, "pEdgeEstim");
    writeMatrix(p_edge_it, output, "pEdge");
    /*
     * boxplot
     */
    for (int i = 0; i < pObsList_k.size(); i++) {
        BufferedWriter writer = new BufferedWriter(
                new FileWriter(String.format("%1$s/%2$s.pObsBoxplot.txt", output, i)));
        TIntObjectHashMap<TDoubleArrayList> k_table = pObsList_k.get(i);
        int[] keys = k_table.keys();
        Arrays.sort(keys);

        for (int k : keys) {
            writer.write(String.valueOf(k));
            writer.write("\t");
        }
        writer.newLine();

        for (int j = 0; j < maxListSize; j++) {
            for (int k : keys) {
                TDoubleArrayList list = k_table.get(k);
                if (list != null) {
                    if (j < list.size()) {
                        writer.write(String.valueOf(list.get(j)));
                    }
                    writer.write("\t");
                }
            }
            writer.newLine();
        }
        writer.close();
    }
    /*
     * weighted estim diff
     */
    for (int i = 0; i <= maxIteration; i++) {
        BufferedWriter writer = new BufferedWriter(
                new FileWriter(String.format("%1$s/%2$s.wdiff.txt", output, i)));
        writer.write("k\twdiff");
        writer.newLine();

        double ks[] = pObs_k[i].keys();
        Arrays.sort(ks);

        for (int j = 0; j < ks.length; j++) {
            double k = ks[j];
            double diff = (Math.abs(pEstim_k[i].get(k) - pObs_k[i].get(k))) * samples_k[i].get((int) k);
            writer.write(String.valueOf(k));
            writer.write("\t");
            writer.write(String.valueOf(diff));
            writer.newLine();
        }
        writer.close();

    }
}

From source file:pro.foundev.strategies.BenchmarkStrategy.java

private void exec(Runnable runnable, String name, int runs, BenchmarkReport report) {
    logger.info("Starting run of " + name);
    DescriptiveStatistics stats = new DescriptiveStatistics();

    Stopwatch timer = new Stopwatch();
    for (int i = 0; i < runs; i++) {
        timer.start();/*from   w w  w  .j  a  va 2 s.  co m*/
        runnable.run();
        timer.stop();
        logger.info("Time to execute load run #" + i + " it took " + timer);
        stats.addValue(timer.elapsed(TimeUnit.MILLISECONDS));
        timer.reset();
    }
    logger.info("Finished run of " + name);
    report.addLine(name, stats.getMin(), stats.getMax(), stats.getPercentile(50), stats.getPercentile(90),
            stats.getMean());
}