Exemplo n.º 1
0
  /**
   * Get the top values from a list of value-frequencies
   *
   * @param values
   * @param bias
   * @return
   */
  public List<ValueFreq<T>> getTopValueUsingPriorityQueue(List<ValueFreq<T>> valueFreqs, int bias) {
    // sort by values
    PriorityQueue<ValueFreq<T>> pq =
        new PriorityQueue<ValueFreq<T>>(
            bias,
            new Comparator<ValueFreq<T>>() {
              public int compare(ValueFreq bv1, ValueFreq bv2) {
                if ((double) (Double) bv1.getValue() < (double) (Double) bv2.getValue()) return -1;
                else if ((double) (Double) bv1.getValue() == (double) (Double) bv2.getValue())
                  return 0;
                else return 1;
              }
            });

    // maintain a priority queue
    for (int i = 0; i < valueFreqs.size(); i++) {
      if (i < bias) {
        pq.add(valueFreqs.get(i));
      } else {
        pq.add(valueFreqs.get(i));
        pq.poll();
      }
    }

    LinkedList<ValueFreq<T>> biasValues = new LinkedList<ValueFreq<T>>();
    Iterator it = pq.iterator();
    while (it.hasNext()) {
      ValueFreq<T> bv = (ValueFreq<T>) it.next();
      biasValues.add(new ValueFreq<T>(bv.getValue(), bv.getFreq()));
    }
    return biasValues;
  }
Exemplo n.º 2
0
 /**
  * Return all values
  *
  * @param vf
  * @return
  */
 public List<T> getValues(List<ValueFreq<T>> vf) {
   ArrayList<T> list = new ArrayList<T>();
   Iterator it = vf.iterator();
   while (it.hasNext()) {
     ValueFreq<T> bv = (ValueFreq) it.next();
     for (int i = 0; i < bv.getFreq(); i++) list.add(bv.getValue());
   }
   return list;
 }
Exemplo n.º 3
0
 /**
  * Calculate a value list
  *
  * @param vf
  * @return
  */
 public List<T> getDistinctValues(List<ValueFreq<T>> vf) {
   ArrayList<T> list = new ArrayList<T>();
   Iterator it = vf.iterator();
   while (it.hasNext()) {
     ValueFreq<T> bv = (ValueFreq) it.next();
     list.add(bv.getValue());
   }
   return list;
 }
Exemplo n.º 4
0
 /**
  * Calculate a frequency list
  *
  * @param vf
  * @return
  */
 public List<Integer> getFreqs(List<ValueFreq<T>> vf) {
   ArrayList<Integer> list = new ArrayList<Integer>();
   Iterator it = vf.iterator();
   while (it.hasNext()) {
     ValueFreq bv = (ValueFreq) it.next();
     list.add(bv.getFreq());
   }
   return list;
 }
Exemplo n.º 5
0
 /**
  * Get sum of all freqs
  *
  * @param valueFreqs
  * @return
  */
 public int totalNumbers(List<ValueFreq<T>> valueFreqs) {
   int totalNumber = 0;
   Iterator it = valueFreqs.iterator();
   while (it.hasNext()) {
     ValueFreq vf = (ValueFreq) it.next();
     totalNumber += vf.getFreq();
   }
   return totalNumber;
 }
Exemplo n.º 6
0
 public List<Integer> getFreqs(List<ValueFreq<T>> valueFreqs, int start, int end) {
   List<ValueFreq<T>> tmp_valueFreqs = valueFreqs.subList(start, end);
   ArrayList<Integer> list = new ArrayList<Integer>();
   Iterator it = tmp_valueFreqs.iterator();
   while (it.hasNext()) {
     ValueFreq<T> bv = (ValueFreq<T>) it.next();
     list.add(bv.getFreq());
   }
   return list;
 }
Exemplo n.º 7
0
 /**
  * Return weight mean, weight is frequency
  * http://en.wikipedia.org/wiki/Weighted_variance#Weighted_sample_variance
  *
  * @param list
  * @return
  */
 public double weightedMean(List<ValueFreq> list) {
   double wm = 0.0;
   double sumOfWeight = 0.0;
   Iterator it = list.iterator();
   while (it.hasNext()) {
     ValueFreq vr = (ValueFreq) it.next();
     double weight = vr.getFreq();
     sumOfWeight += weight;
     wm += Double.parseDouble(vr.getValue().toString()) * weight;
   }
   return wm / sumOfWeight;
 }
Exemplo n.º 8
0
  /**
   * Calculate weighted variance
   *
   * @param list
   * @return
   */
  public double weightedVarianceSample(List<ValueFreq> list) {
    // weighted mean
    double wm = weightedMean(list);

    // calculate sum of weight
    double sumOfWeight = 0.0;

    // weighted variance
    double variance = 0.0;
    Iterator it = list.iterator();
    while (it.hasNext()) {
      ValueFreq vr = (ValueFreq) it.next();
      double weight = vr.getFreq();
      sumOfWeight += weight;
      variance += weight * Math.pow(Double.parseDouble(vr.getValue().toString()) - wm, 2);
    }
    return variance / (sumOfWeight - 1);
  }
Exemplo n.º 9
0
  /**
   * Total area of a distribution
   *
   * @param valueFreqs
   * @return
   */
  public double area(List<ValueFreq<T>> valueFreqs) {
    double totalArea = 0.0;
    Iterator it = valueFreqs.iterator();
    T lastValue = null;
    int lastFreq = 0;
    if (it.hasNext()) {
      ValueFreq<T> vf = (ValueFreq<T>) it.next();
      lastValue = vf.getValue();
      lastFreq = vf.getFreq();
    }
    while (it.hasNext()) {
      ValueFreq<T> vf = (ValueFreq<T>) it.next();

      totalArea +=
          0.5
              * (vf.getFreq() + lastFreq)
              * ((Double) vf.getValue() - Double.valueOf(lastValue.toString()));
      lastValue = vf.getValue();
      lastFreq = vf.getFreq();
    }
    return totalArea;
  }
Exemplo n.º 10
0
  public List<Double> areas(List<ValueFreq<T>> valueFreqs) {
    List<Double> areas = new ArrayList<Double>();
    Iterator it = valueFreqs.iterator();
    T lastValue = null;
    int lastFreq = 0;
    if (it.hasNext()) {
      ValueFreq<T> vf = (ValueFreq<T>) it.next();
      lastValue = vf.getValue();
      lastFreq = vf.getFreq();
    }
    while (it.hasNext()) {
      ValueFreq<T> vf = (ValueFreq<T>) it.next();

      double area =
          0.5
              * (vf.getFreq() + lastFreq)
              * ((Double) vf.getValue() - Double.valueOf(lastValue.toString()));
      areas.add(area);
      lastValue = vf.getValue();
      lastFreq = vf.getFreq();
    }
    return areas;
  }