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

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

Introduction

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

Prototype

public void addValue(char v) throws MathIllegalArgumentException 

Source Link

Document

Adds 1 to the frequency count for 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.  c  om

    // 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:jnetention.Core.java

public static Frequency tokenBag(String x, int minLength, int maxTokenLength) {
    String[] tokens = tokenize(x);
    Frequency f = new Frequency();
    for (String t : tokens) {
        if (t == null)
            continue;
        if (t.length() < minLength)
            continue;
        if (t.length() > maxTokenLength)
            continue;
        t = t.toLowerCase();//from ww w  . j  a va 2  s .c om
        f.addValue(t);
    }
    return f;
}

From source file:com.itemanalysis.psychometrics.statistics.FrequencyTest.java

/**
 * Test of toString method, of class Frequency.
 *//*ww  w  .  j a  v  a 2s.  c  o m*/
@Test
public void testToString() {
    String[] data = { "a", "a", "a", "a", "a", "a", "b", "b", "b", "c", "c" };
    long[] expected = { 6, 3, 2 };

    Frequency f = new Frequency();
    for (int i = 0; i < data.length; i++) {
        f.addValue(data[i]);
    }

    Iterator<Comparable<?>> iter = f.valuesIterator();
    int index = 0;
    while (iter.hasNext()) {
        assertEquals("[row " + index + "]", expected[index], f.getCount(iter.next()));
        index++;
    }
}

From source file:com.graphhopper.jsprit.core.util.UnassignedJobReasonTrackerTest.java

@Test
public void testFreq() {
    Frequency frequency = new Frequency();
    frequency.addValue("VehicleDependentTimeWindowHardActivityConstraint");
    frequency.addValue("b");
    frequency.addValue("VehicleDependentTimeWindowHardActivityConstraint");

    Iterator<Map.Entry<Comparable<?>, Long>> entryIterator = frequency.entrySetIterator();
    while (entryIterator.hasNext()) {
        Map.Entry<Comparable<?>, Long> e = entryIterator.next();
        System.out.println(e.getKey().toString() + " " + e.getValue());
    }/*from  w w  w.  j  a v a2s.co m*/
}

From source file:ch.bfh.lca._15h.server.statisticServices.StatisticHandler.java

/**
 * Calculates the frequencies of genders within an dataSource. The result is given in a GenericResultRow
 * @param name Name of Age (group)//from   ww w. jav  a2s  .co  m
 * @param dataSource Source with datas of DPCs
 * @return GenericResultRow (Contains in field 0 the description of the Age (group),
 *   in field 1 the amount of male within that group
 *   in field 2 the amount of females within the group.
 */
private GenericResultRow genderFrequency(String name, DataSource dataSource) {
    Frequency freqGender = new Frequency();
    GenericResultRow grr = new DBResultRow();

    for (DoctorPatientContact dpc : dataSource) {
        freqGender.addValue(dpc.getPatSex().getValue());
    }

    String[] colName = { "AgeGroup", "Male", "Fenale" };
    Object[] values = { name, freqGender.getCount(0), freqGender.getCount(1) };

    grr.setValues(colName, values);
    return grr;
}

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 a  v  a 2s .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:net.sourceforge.jabm.test.util.DiscreteProbabilityDistributionTest.java

public void compareDistributions(DiscreteProbabilityDistribution subject, double[] probs) {

    System.out.println("Testing with subject: " + subject);

    Frequency frequency = new Frequency();
    SummaryStats eventData = new SummaryStats("Event_Data");
    for (int trial = 0; trial < NUM_TRIALS; trial++) {
        int event = subject.generateRandomEvent();
        eventData.newData(event);/*from w  w w. j  a v  a 2 s  .  co m*/
        frequency.addValue(event);
    }
    System.out.println(frequency);
    System.out.println(eventData);
    double mean = subject.computeMean();

    System.out.println("target mean = " + mean);

    for (int j = 0; j < probs.length; j++) {
        assertTrue(approxEqual(probs[j], frequency.getPct(j)));
    }

    assertTrue(approxEqual(eventData.getMean(), mean));
}

From source file:com.itemanalysis.psychometrics.irt.estimation.ItemResponseFileSummary.java

private ItemResponseVector[] readTapData() {
    byte[][] tap = new byte[35][18];
    try {//from   www  . jav a2 s.c  o  m
        File f = FileUtils.toFile(this.getClass().getResource("/testdata/tap-data.txt"));
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = "";
        String[] s = null;
        int row = 0;
        while ((line = br.readLine()) != null) {
            s = line.split(",");
            for (int j = 0; j < s.length; j++) {
                tap[row][j] = Byte.parseByte(s[j]);
            }
            row++;
        }
        br.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    Frequency freq = new Frequency();
    for (int i = 0; i < tap.length; i++) {
        freq.addValue(Arrays.toString(tap[i]));
    }

    ItemResponseVector[] responseData = new ItemResponseVector[freq.getUniqueCount()];
    ItemResponseVector irv = null;
    Iterator<Comparable<?>> iter = freq.valuesIterator();
    int index = 0;

    //create array of ItemResponseVector objects
    while (iter.hasNext()) {
        //get response string from frequency summary and convert to byte array
        Comparable<?> value = iter.next();
        String s = value.toString();
        s = s.substring(1, s.lastIndexOf("]"));
        String[] sa = s.split(",");
        byte[] rv = new byte[sa.length];
        for (int i = 0; i < sa.length; i++) {
            rv[i] = Byte.parseByte(sa[i].trim());
        }

        //create response vector objects
        irv = new ItemResponseVector(rv, Long.valueOf(freq.getCount(value)).doubleValue());
        responseData[index] = irv;
        index++;
    }
    //        //display results of summary
    //        for(int i=0;i<responseData.length;i++){
    //            System.out.println(responseData[i].toString() + ": " + responseData[i].getFrequency());
    //        }

    return responseData;
}

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  w ww .  j  ava 2s  .c om*/
        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.itemanalysis.ItemAnalysis.java

public int numberOfSubscales() {
    Frequency table = new Frequency();
    for (VariableAttributes v : variables) {
        table.addValue(v.getItemGroup());
    }//w w w.  ja va 2 s.c om
    return table.getUniqueCount();
}