/** * Private helper method. * Assumes parameters have been validated. * @param values input data * @param begin index (0-based) of the first array element to include * @param length the number of elements to include * @return array of array of the most frequently occurring element(s) sorted in ascending order. */ private static double[] getMode(double[] values, final int begin, final int length) { // Add the values to the frequency table Frequency freq = new Frequency(); for (int i = begin; i < begin + length; i++) { final double value = values[i]; if (!Double.isNaN(value)) { freq.addValue(Double.valueOf(value)); } } List<Comparable<?>> list = freq.getMode(); // Convert the list to an array of primitive double double[] modes = new double[list.size()]; int i = 0; for(Comparable<?> c : list) { modes[i++] = ((Double) c).doubleValue(); } return modes; }
if (getSumFreq() == 0) { return 0; return getCumFreq(((Integer) v).longValue()); return getSumFreq(); // v is comparable, but greater than the last value Iterator<Comparable<?>> values = valuesIterator(); while (values.hasNext()) { Comparable<?> nextValue = values.next(); if (c.compare(v, nextValue) > 0) { result += getCount(nextValue); } else { return result;
/** * Return a string representation of this frequency distribution. * * @return a string representation. */ @Override public String toString() { NumberFormat nf = NumberFormat.getPercentInstance(); StringBuilder outBuffer = new StringBuilder(); outBuffer.append("Value \t Freq. \t Pct. \t Cum Pct. \n"); Iterator<Comparable<?>> iter = freqTable.keySet().iterator(); while (iter.hasNext()) { Comparable<?> value = iter.next(); outBuffer.append(value); outBuffer.append('\t'); outBuffer.append(getCount(value)); outBuffer.append('\t'); outBuffer.append(nf.format(getPct(value))); outBuffer.append('\t'); outBuffer.append(nf.format(getCumPct(value))); outBuffer.append('\n'); } return outBuffer.toString(); }
/** * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). * <p> * Returns <code>Double.NaN</code> if no values have been added. * Returns 0 if at least one value has been added, but v is not comparable * to the values set.</p> * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(Comparable<?> v) { final long sumFreq = getSumFreq(); if (sumFreq == 0) { return Double.NaN; } return (double) getCumFreq(v) / (double) sumFreq; }
/** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). * <p> * Returns <code>Double.NaN</code> if no values have been added. * Returns 0 if at least one value has been added, but v is not comparable * to the values set.</p> * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(Comparable<?> v) { final long sumFreq = getSumFreq(); if (sumFreq == 0) { return Double.NaN; } return (double) getCount(v) / (double) sumFreq; }
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]); 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 }
Frequency frequency = new Frequency(); frequency.addValue(number.longValue()); Iterator iterator = frequency.valuesIterator(); 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));
@Override public void informJobUnassigned(Job unassigned, Collection<String> failedConstraintNames) { if (!this.failedConstraintNamesFrequencyMapping.containsKey(unassigned.getId())) { this.failedConstraintNamesFrequencyMapping.put(unassigned.getId(), new Frequency()); } for (String r : failedConstraintNames) { if (failedConstraintNamesToBeIgnored.contains(r)) continue; this.failedConstraintNamesFrequencyMapping.get(unassigned.getId()).addValue(r); } }
/** * Adds 1 to the frequency count for v. * * @param v the value to add. * @throws MathIllegalArgumentException if the table contains entries not * comparable to Long */ public void addValue(int v) throws MathIllegalArgumentException { addValue(Long.valueOf(v)); }
/** * Computes the sum of scores for those values in columns. * * @return sum of scores */ public double sumOfScores(){ double sum = 0.0; Iterator<Comparable<?>> iter = columns.valuesIterator(); Double score = null; while(iter.hasNext()){ score = (Double)iter.next(); sum += score*columns.getCount(score); } return sum; }
public PercentileRank(Integer min, Integer max){ freqTable = new Frequency(); this.min = min; this.max = max; }
/** * Returns the number of values equal to v. * * @param v the value to lookup. * @return the frequency of v. */ public long getCount(char v) { return getCount(Character.valueOf(v)); }
/** * 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. * @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); }
/** * Returns the cumulative percentage of values less than or equal to v * (as a proportion between 0 and 1). * <p> * Returns 0 if v is not comparable to the values set.</p> * * @param v the value to lookup * @return the proportion of values less than or equal to v */ public double getCumPct(char v) { return getCumPct(Character.valueOf(v)); }
public double[] getThresholds(){ double[] alpha = new double[freqY.getUniqueCount()-1]; Iterator<Comparable<?>> iter = freqY.valuesIterator(); Comparable<?> v = null; int index = 0; while(iter.hasNext()){ v = iter.next(); if(iter.hasNext()){ alpha[index] = norm.inverseCumulativeProbability(freqY.getCumPct(v)); index++; } } return alpha; }
/** * Returns the cumulative frequency of values less than or equal to v. * <p> * Returns 0 if v is not comparable to the values set.</p> * * @param v the value to lookup * @return the proportion of values equal to v */ public long getCumFreq(int v) { return getCumFreq(Long.valueOf(v)); }
public double[][] getTable(){ int nrow = rowMargin.getUniqueCount(); int ncol = colMargin.getUniqueCount(); double[][] table = new double[nrow][ncol]; Iterator<Comparable<?>> rows = rowMargin.valuesIterator(); Iterator<Comparable<?>> cols = null; Comparable<?> rTemp = null; int i=0; int j=0; while(rows.hasNext()){ cols = colMargin.valuesIterator(); rTemp = rows.next(); j=0; while(cols.hasNext()){ table[i][j] = (double)getCount(rTemp, cols.next()); j++; } i++; } return table; }
/** * Merge another Frequency object's counts into this instance. * This Frequency's counts will be incremented (or set when not already set) * by the counts represented by other. * * @param other the other {@link Frequency} object to be merged * @throws NullArgumentException if {@code other} is null * @since 3.1 */ public void merge(final Frequency other) throws NullArgumentException { MathUtils.checkNotNull(other, LocalizedFormats.NULL_NOT_ALLOWED); final Iterator<Map.Entry<Comparable<?>, Long>> iter = other.entrySetIterator(); while (iter.hasNext()) { final Map.Entry<Comparable<?>, Long> entry = iter.next(); incrementValue(entry.getKey(), entry.getValue().longValue()); } }
/** * Returns the percentage of values that are equal to v * (as a proportion between 0 and 1). * * @param v the value to lookup * @return the proportion of values equal to v */ public double getPct(int v) { return getPct(Long.valueOf(v)); }
public Iterator<Comparable<?>> colValuesIterator(){ return colMargin.valuesIterator(); }