public Factory<Counter<T>> getFactory() { return counter.getFactory(); }
@SuppressWarnings("unchecked") public static <E, C extends Counter<E>> C perturbCounts(C c, Random random, double p) { C result = (C) c.getFactory().create(); for (E key : c.keySet()) { double count = c.getCount(key); double noise = -Math.log(1.0 - random.nextDouble()); // inverse of CDF for // exponential // distribution // log.info("noise=" + noise); double perturbedCount = count + noise * p; result.setCount(key, perturbedCount); } return result; }
public static <E> Counter<E> add(Counter<E> c1, Collection<E> c2) { Counter<E> result = c1.getFactory().create(); addInPlace(result, c1); for (E key : c2) { result.incrementCount(key, 1); } return result; }
public static <T> Counter<T> exp(Counter<T> c) { Counter<T> d = c.getFactory().create(); for (T t : c.keySet()) { d.setCount(t, Math.exp(c.getCount(t))); } return d; }
public static <T> Counter<T> pow(Counter<T> c, double temp) { Counter<T> d = c.getFactory().create(); for (T t : c.keySet()) { d.setCount(t, Math.pow(c.getCount(t), temp)); } return d; }
/** * Returns a Counter that is the union of the two Counters passed in (counts * are added). * * @return A Counter that is the union of the two Counters passed in (counts * are added). */ @SuppressWarnings("unchecked") public static <E, C extends Counter<E>> C union(C c1, C c2) { C result = (C) c1.getFactory().create(); addInPlace(result, c1); addInPlace(result, c2); return result; }
/** * Returns a counter where each element corresponds to the normalized count of * the corresponding element in c raised to the given power. */ public static <E> Counter<E> powNormalized(Counter<E> c, double temp) { Counter<E> d = c.getFactory().create(); double total = c.totalCount(); for (E e : c.keySet()) { d.setCount(e, Math.pow(c.getCount(e) / total, temp)); } return d; }
/** * increments every key in the counter by value */ public static <E> Counter<E> add(Counter<E> c1, double value) { Counter<E> result = c1.getFactory().create(); for (E key : c1.keySet()) { result.setCount(key, c1.getCount(key) + value); } return result; }
/** * Returns a new Counter which is scaled by the given scale factor. * * @param c The counter to scale. It is not changed * @param s The constant to scale the counter by * @return A new Counter which is the argument scaled by the given scale * factor. */ @SuppressWarnings("unchecked") public static <E, C extends Counter<E>> C scale(C c, double s) { C scaled = (C) c.getFactory().create(); for (E key : c.keySet()) { scaled.setCount(key, c.getCount(key) * s); } return scaled; }
/** * Returns |c1 - c2|. * * @return The difference between sets c1 and c2. */ public static <E> Counter<E> absoluteDifference(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.union(c1.keySet(), c2.keySet())) { double newCount = Math.abs(c1.getCount(key) - c2.getCount(key)); if (newCount > 0) { result.setCount(key, newCount); } } return result; }
/** * Returns a new Counter with counts averaged from the two given Counters. The * average Counter will contain the union of keys in both source Counters, and * each count will be the average of the two source counts for that key, where * as usual a missing count in one Counter is treated as count 0. * * @return A new counter with counts that are the mean of the resp. counts in * the given counters. */ public static <E> Counter<E> average(Counter<E> c1, Counter<E> c2) { Counter<E> average = c1.getFactory().create(); Set<E> allKeys = Generics.newHashSet(c1.keySet()); allKeys.addAll(c2.keySet()); for (E key : allKeys) { average.setCount(key, (c1.getCount(key) + c2.getCount(key)) * 0.5); } return average; }
/** * Returns a new Counter which is the input counter with log tf scaling * * @param c The counter to scale. It is not changed * @param base The base of the logarithm used for tf scaling by 1 + log tf * @return A new Counter which is the argument scaled by the given scale * factor. */ @SuppressWarnings("unchecked") public static <E, C extends Counter<E>> C tfLogScale(C c, double base) { C scaled = (C) c.getFactory().create(); for (E key : c.keySet()) { double cnt = c.getCount(key); double scaledCnt = 0.0; if (cnt > 0) { scaledCnt = 1.0 + SloppyMath.log(cnt, base); } scaled.setCount(key, scaledCnt); } return scaled; }
/** * Returns the product of c1 and c2. * * @return The product of c1 and c2. */ public static <E> Counter<E> product(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.intersection(c1.keySet(), c2.keySet())) { result.setCount(key, c1.getCount(key) * c2.getCount(key)); } return result; }
/** * Returns c1 divided by c2. Note that this can create NaN if c1 has non-zero * counts for keys that c2 has zero counts. * * @return c1 divided by c2. */ public static <E> Counter<E> division(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.union(c1.keySet(), c2.keySet())) { result.setCount(key, c1.getCount(key) / c2.getCount(key)); } return result; }
/** * Returns a Counter which is a weighted average of c1 and c2. Counts from c1 * are weighted with weight w1 and counts from c2 are weighted with w2. */ public static <E> Counter<E> linearCombination(Counter<E> c1, double w1, Counter<E> c2, double w2) { Counter<E> result = c1.getFactory().create(); for (E o : c1.keySet()) { result.incrementCount(o, c1.getCount(o) * w1); } for (E o : c2.keySet()) { result.incrementCount(o, c2.getCount(o) * w2); } return result; }
public static <T> Counter<T> diff(Counter<T> goldFeatures, Counter<T> guessedFeatures) { Counter<T> result = goldFeatures.getFactory().create(); for (T key : Sets.union(goldFeatures.keySet(), guessedFeatures.keySet())) { result.setCount(key, goldFeatures.getCount(key) - guessedFeatures.getCount(key)); } retainNonZeros(result); return result; }
public static <E> Counter<E> f1(Counter<E> precision, Counter<E> recall) { Counter<E> result = precision.getFactory().create(); for(E key : Sets.intersection(precision.keySet(),recall.keySet())) { result.setCount(key,f1(precision.getCount(key),recall.getCount(key))); } return result; }
public static <E> Counter<E> add(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.union(c1.keySet(), c2.keySet())) { result.setCount(key, c1.getCount(key) + c2.getCount(key)); } retainNonZeros(result); return result; }
/** * Returns c1 divided by c2. Safe - will not calculate scores for keys that are zero or that do not exist in c2 * * @return c1 divided by c2. */ public static <E> Counter<E> divisionNonNaN(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.union(c1.keySet(), c2.keySet())) { if(c2.getCount(key) != 0) result.setCount(key, c1.getCount(key) / c2.getCount(key)); } return result; }
/** * Returns a counter that is the intersection of c1 and c2. If both c1 and c2 * contain a key, the min of the two counts is used. * * @return A counter that is the intersection of c1 and c2 */ public static <E> Counter<E> intersection(Counter<E> c1, Counter<E> c2) { Counter<E> result = c1.getFactory().create(); for (E key : Sets.union(c1.keySet(), c2.keySet())) { double count1 = c1.getCount(key); double count2 = c2.getCount(key); double minCount = (count1 < count2 ? count1 : count2); if (minCount > 0) { result.setCount(key, minCount); } } return result; }