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

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

Introduction

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

Prototype

public Iterator<Comparable<?>> valuesIterator() 

Source Link

Document

Returns an Iterator over the set of values that have been added.

Usage

From source file:com.github.rinde.rinsim.scenario.generator.PoissonProcessTest.java

/**
 * Checks whether the observations conform to a Poisson process with the
 * specified intensity. Uses a chi square test with the specified confidence.
 * The null hypothesis is that the observations are the result of a poisson
 * process./*w  w  w .ja v  a 2  s.  c  om*/
 * @param observations
 * @param intensity
 * @param confidence
 * @return <code>true</code> if the observations
 */
static boolean isPoissonProcess(Frequency observations, double intensity, double length, double confidence) {
    final PoissonDistribution pd = new PoissonDistribution(length * intensity);

    final Iterator<?> it = observations.valuesIterator();
    final long[] observed = new long[observations.getUniqueCount()];
    final double[] expected = new double[observations.getUniqueCount()];

    int index = 0;
    while (it.hasNext()) {
        final Long l = (Long) it.next();
        observed[index] = observations.getCount(l);
        expected[index] = pd.probability(l.intValue()) * observations.getSumFreq();
        if (expected[index] == 0) {
            return false;
        }
        index++;
    }
    final double chi = TestUtils.chiSquareTest(expected, observed);
    return !(chi < confidence);
}

From source file:net.sourceforge.jabm.report.FrequencyByTimeDataset.java

public void updateCategories(Frequency f) {
    @SuppressWarnings("rawtypes")
    Iterator<Comparable<?>> it = f.valuesIterator();
    while (it.hasNext()) {
        Comparable<?> category = it.next();
        registerCategory(category);// www  . ja va2  s .  c  o m
    }
}

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

/**
 * Test of toString method, of class Frequency.
 *//*from w  ww.ja v a2  s  .  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.itemanalysis.psychometrics.irt.estimation.ItemResponseFileSummary.java

private ItemResponseVector[] readTapData() {
    byte[][] tap = new byte[35][18];
    try {//from   www .  ja v  a 2s  . c  om
        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.irt.estimation.ItemResponseFileSummary.java

/**
 * Summarize comma delimited file. It will extract the data beginning in the column indicated by start
 * and it will continue for nItems columns.
 *
 * @param f file to summarize//from w ww  . j  a v a  2  s.  c om
 * @param start the column index of the first item. It is zero based. If teh data start in the first column, then start=0.
 * @param nItems number of items to read from the file. It will begin at the column indicated by start.
 * @param headerIncluded true if header is included. False otherwise. The header will be omitted.
 * @return an array of item resposne vectors
 */
public ItemResponseVector[] getCondensedResponseVectors(File f, int start, int nItems, boolean headerIncluded) {
    Frequency freq = new Frequency();
    String responseString = "";

    try {
        BufferedReader br = new BufferedReader(new FileReader(f));
        String line = "";
        String[] s = null;
        if (headerIncluded)
            br.readLine();//skip header
        while ((line = br.readLine()) != null) {
            s = line.split(",");
            line = "";

            for (int j = 0; j < nItems; j++) {
                line += s[j + start];
            }
            freq.addValue(line);
        }
        br.close();

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

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

    //create array of ItemResponseVector objects
    while (iter.hasNext()) {
        Comparable<?> value = iter.next();
        responseString = value.toString();

        int n = responseString.length();
        rv = new byte[n];

        String response = "";
        for (int i = 0; i < n; i++) {
            response = String.valueOf(responseString.charAt(i)).toString();
            rv[i] = Byte.parseByte(response);
        }

        //create response vector objects
        irv = new ItemResponseVector(rv, Long.valueOf(freq.getCount(value)).doubleValue());
        responseData[index] = irv;
        index++;
    }
    return responseData;

}

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. jav  a 2  s.  c  om
        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;
    }/*from w  ww .j  a va2s. co  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;
}