Example usage for org.apache.commons.math3.stat Frequency getCumFreq

List of usage examples for org.apache.commons.math3.stat Frequency getCumFreq

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat Frequency getCumFreq.

Prototype

public long getCumFreq(char v) 

Source Link

Document

Returns the cumulative frequency of values less than or equal to v.

Usage

From source file:com.sop4j.SimpleStatistics.java

public static void main(String[] args) {
    final MersenneTwister rng = new MersenneTwister(); // used for RNG... READ THE DOCS!!!
    final int[] values = new int[NUM_VALUES];

    final DescriptiveStatistics descriptiveStats = new DescriptiveStatistics(); // stores values
    final SummaryStatistics summaryStats = new SummaryStatistics(); // doesn't store values
    final Frequency frequency = new Frequency();

    // add numbers into our stats
    for (int i = 0; i < NUM_VALUES; ++i) {
        values[i] = rng.nextInt(MAX_VALUE);

        descriptiveStats.addValue(values[i]);
        summaryStats.addValue(values[i]);
        frequency.addValue(values[i]);//  w w w  . j a  va  2s .com
    }

    // print out some standard stats
    System.out.println("MIN: " + summaryStats.getMin());
    System.out.println("AVG: " + String.format("%.3f", summaryStats.getMean()));
    System.out.println("MAX: " + summaryStats.getMax());

    // get some more complex stats only offered by DescriptiveStatistics
    System.out.println("90%: " + descriptiveStats.getPercentile(90));
    System.out.println("MEDIAN: " + descriptiveStats.getPercentile(50));
    System.out.println("SKEWNESS: " + String.format("%.4f", descriptiveStats.getSkewness()));
    System.out.println("KURTOSIS: " + String.format("%.4f", descriptiveStats.getKurtosis()));

    // quick and dirty stats (need a little help from Guava to convert from int[] to double[])
    System.out.println("MIN: " + StatUtils.min(Doubles.toArray(Ints.asList(values))));
    System.out.println("AVG: " + String.format("%.4f", StatUtils.mean(Doubles.toArray(Ints.asList(values)))));
    System.out.println("MAX: " + StatUtils.max(Doubles.toArray(Ints.asList(values))));

    // some stats based upon frequencies
    System.out.println("NUM OF 7s: " + frequency.getCount(7));
    System.out.println("CUMULATIVE FREQUENCY OF 7: " + frequency.getCumFreq(7));
    System.out.println("PERCENTAGE OF 7s: " + frequency.getPct(7));
}

From source file:com.github.rvesse.github.pr.stats.PullRequestStats.java

private void outputPercentile(Frequency freq, Percentile percentiles, int p, String metric) {
    long value = (long) percentiles.evaluate((double) p);
    System.out.println("  " + p + "% (" + value + " " + metric + "): " + (long) freq.getCumFreq(value));
}

From source file:com.itemanalysis.psychometrics.polycor.AbstractPolyserialCorrelation.java

public void summarize(double[] x, int[] y) {
    if (x.length != y.length)
        throw new IllegalArgumentException("X and Y are of different lengths.");
    N = (double) x.length;
    Mean meanX = new Mean();
    StandardDeviation sdX = new StandardDeviation();
    PearsonCorrelation rxy = new PearsonCorrelation();
    Frequency table = new Frequency();

    for (int i = 0; i < N; i++) {
        meanX.increment(x[i]);//from  w ww .j  ava2s.co  m
        sdX.increment(x[i]);
        rxy.increment(x[i], (double) y[i]);
        table.addValue(y[i]);
    }

    //compute thresholds
    int nrow = table.getUniqueCount();
    double[] freqDataY = new double[nrow];
    double ntotal = table.getSumFreq();
    for (int i = 0; i < (nrow - 1); i++) {
        freqDataY[i] = table.getCumFreq(i + 1);
        thresholds[i] = norm.inverseCumulativeProbability(freqDataY[i] / ntotal);
    }
    thresholds[nrow - 1] = 10;//set last threshold to a large number less than infinity

}

From source file:com.itemanalysis.psychometrics.polycor.PolyserialLogLikelihoodTwoStep.java

public void summarize() throws DimensionMismatchException {
    if (dataX.length != dataY.length)
        throw new DimensionMismatchException(dataX.length, dataY.length);
    Frequency table = new Frequency();
    meanX = new Mean();
    sdX = new StandardDeviation();
    rxy = new PearsonCorrelation();
    for (int i = 0; i < nrow; i++) {
        meanX.increment(dataX[i]);//from www  .j  av  a  2 s .com
        sdX.increment(dataX[i]);
        rxy.increment(dataX[i], (double) dataY[i]);
        table.addValue(dataY[i]);
    }

    //compute thresholds
    nrow = table.getUniqueCount();
    freqDataY = new double[nrow];
    double ntotal = table.getSumFreq();
    for (int i = 0; i < (nrow - 1); i++) {
        freqDataY[i] = table.getCumFreq(i + 1);
        alpha[i] = normal.inverseCumulativeProbability(freqDataY[i] / ntotal);
    }
    alpha[nrow - 1] = 10;//set last threshold to a large number less than infinity
}

From source file:com.itemanalysis.jmetrik.stats.frequency.FrequencyAnalysis.java

public void publishTable(VariableAttributes v) {
    TextTableColumnFormat[] cformats = new TextTableColumnFormat[6];
    cformats[0] = new TextTableColumnFormat();
    cformats[0].setStringFormat(11, TextTableColumnFormat.OutputAlignment.LEFT);
    cformats[1] = new TextTableColumnFormat();
    cformats[1].setIntFormat(10, TextTableColumnFormat.OutputAlignment.RIGHT);
    cformats[2] = new TextTableColumnFormat();
    cformats[2].setDoubleFormat(10, 4, TextTableColumnFormat.OutputAlignment.RIGHT);
    cformats[3] = new TextTableColumnFormat();
    cformats[3].setDoubleFormat(10, 4, TextTableColumnFormat.OutputAlignment.RIGHT);
    cformats[4] = new TextTableColumnFormat();
    cformats[4].setIntFormat(10, TextTableColumnFormat.OutputAlignment.RIGHT);
    cformats[5] = new TextTableColumnFormat();
    cformats[5].setDoubleFormat(10, 4, TextTableColumnFormat.OutputAlignment.RIGHT);

    Frequency temp = frequencyTables.get(v);

    TextTable table = new TextTable();
    table.addAllColumnFormats(cformats, 4);
    table.getRowAt(0).addHeader(0, 6, v.getName().toString(), TextTablePosition.CENTER);
    table.getRowAt(1).addHorizontalRule(0, 6, "=");
    table.getRowAt(2).addHeader(0, 1, "Value", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(1, 1, "Frequency", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(2, 1, "Percent", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(3, 1, "Valid Pct.", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(4, 1, "Cum. Freq.", TextTablePosition.CENTER);
    table.getRowAt(2).addHeader(5, 1, "Cum. Pct.", TextTablePosition.CENTER);
    table.getRowAt(3).addHorizontalRule(0, 6, "-");

    Iterator<Comparable<?>> iter = temp.valuesIterator();

    int index = 4;
    double pctSum = 0.0;
    long validSum = temp.getSumFreq();
    long miss = (long) (maxProgress - validSum);

    while (iter.hasNext()) {
        table.addRow(new TextTableRow(cformats.length), cformats);

        Comparable<?> value = iter.next();
        table.addStringAt(index, 0, value.toString());
        table.addLongAt(index, 1, temp.getCount(value));
        table.addDoubleAt(index, 2, ((double) temp.getCount(value) / maxProgress) * 100);
        table.addDoubleAt(index, 3, temp.getPct(value) * 100);
        table.addLongAt(index, 4, temp.getCumFreq(value));
        table.addDoubleAt(index, 5, temp.getCumPct(value) * 100);
        index++;//from w  w  w  .  ja v a 2 s.  co  m
        pctSum += (double) temp.getCount(value) / (double) maxProgress;
    }

    table.addRow(new TextTableRow(cformats.length), cformats);
    table.addStringAt(index, 0, "Valid Total");
    table.addLongAt(index, 1, validSum);
    table.addDoubleAt(index, 2, pctSum * 100);
    table.addDoubleAt(index, 3, (double) validSum / (double) (maxProgress - miss) * 100);
    index++;

    table.addRow(new TextTableRow(cformats.length), cformats);
    table.addStringAt(index, 0, "Missing");
    table.addLongAt(index, 1, miss);
    table.addDoubleAt(index, 2, ((double) miss / maxProgress) * 100);
    index++;

    table.addRow(new TextTableRow(cformats.length), cformats);
    table.addStringAt(index, 0, "Grand Total");
    table.addLongAt(index, 1, (long) maxProgress);
    table.addDoubleAt(index, 2, ((double) (miss + temp.getSumFreq()) / maxProgress) * 100);
    index++;

    table.addRow(new TextTableRow(cformats.length), cformats);
    table.getRowAt(index).addHorizontalRule(0, 6, "=");
    publish(table.toString() + "\n");
}

From source file:org.apache.solr.client.solrj.io.eval.FrequencyTableEvaluator.java

@Override
public Object doWork(Object... values) throws IOException {
    if (Arrays.stream(values).anyMatch(item -> null == item)) {
        return null;
    }//w ww.jav  a2s  .  c  o  m

    List<?> sourceValues;

    if (values.length == 1) {
        sourceValues = values[0] instanceof List<?> ? (List<?>) values[0] : Arrays.asList(values[0]);
    } else {
        throw new IOException(
                String.format(Locale.ROOT, "Invalid expression %s - expecting at least one value but found %d",
                        toExpression(constructingFactory), containedEvaluators.size()));
    }

    Frequency frequency = new Frequency();

    for (Object o : sourceValues) {
        Number number = (Number) o;
        frequency.addValue(number.longValue());
    }

    List<Tuple> histogramBins = new ArrayList<>();

    Iterator iterator = frequency.valuesIterator();

    while (iterator.hasNext()) {
        Long value = (Long) iterator.next();
        Map<String, Number> map = new HashMap<>();
        map.put("value", value.longValue());
        map.put("count", frequency.getCount(value));
        map.put("cumFreq", frequency.getCumFreq(value));
        map.put("cumPct", frequency.getCumPct(value));
        map.put("pct", frequency.getPct(value));
        histogramBins.add(new Tuple(map));
    }
    return histogramBins;
}