-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCounters.java
More file actions
121 lines (110 loc) · 3.5 KB
/
Copy pathCounters.java
File metadata and controls
121 lines (110 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.rutgers.util;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import com.rutgers.util.SloppyMath;
public class Counters {
public static <E> Counter<E> normalize(Counter<E> counter) {
Counter<E> normalizedCounter = new Counter<E>();
double total = counter.totalCount();
for (E key : counter.keySet()) {
normalizedCounter.setCount(key, counter.getCount(key) / total);
}
return normalizedCounter;
}
public static <K,V> CounterMap<K,V> conditionalNormalize(CounterMap<K,V> counterMap) {
CounterMap<K,V> normalizedCounterMap = new CounterMap<K,V>();
for (K key : counterMap.keySet()) {
Counter<V> normalizedSubCounter = normalize(counterMap.getCounter(key));
for (V value : normalizedSubCounter.keySet()) {
double count = normalizedSubCounter.getCount(value);
normalizedCounterMap.setCount(key, value, count);
}
}
return normalizedCounterMap;
}
public static <E> String toBiggestValuesFirstString(Counter<E> c) {
return c.asPriorityQueue().toString();
}
public static <E> String toBiggestValuesFirstString(Counter<E> c, int k) {
PriorityQueue<E> pq = c.asPriorityQueue();
PriorityQueue<E> largestK = new FastPriorityQueue<E>();
while (largestK.size() < k && pq.hasNext()) {
double firstScore = pq.getPriority();
E first = pq.next();
largestK.setPriority(first, firstScore);
}
return largestK.toString();
}
public static <E> List<E> sortedKeys(Counter<E> counter) {
List<E> sortedKeyList = new ArrayList<E>();
PriorityQueue<E> pq = counter.asPriorityQueue();
while (pq.hasNext()) {
sortedKeyList.add(pq.next());
}
return sortedKeyList;
}
/**
*
* @param <E>
* @param x
* @param y
* @return
*/
public static<E> double jensenShannonDivergence(Counter<E> x, Counter<E> y) {
double sum = 0.0;
double xTotal = x.totalCount();
double yTotal = y.totalCount();
for (E key: x.keySet()) {
//x -> x+y/2
double xVal = x.getCount(key) / xTotal;
double yVal = y.getCount(key) / yTotal;
double avg = 0.5 * (xVal + yVal);
sum += xVal * Math.log(xVal / avg);
}
for (E key: y.keySet()) {
//y -> x+y/2
double xVal = x.getCount(key)/ xTotal ;
double yVal = y.getCount(key) / yTotal;
double avg = 0.5 * (xVal + yVal);
sum += yVal * Math.log(yVal / avg);
}
return sum / 0.5;
}
/**
* Simple sparse dot product method. Try to put the sparser <code>Counter</code> as the <code>x</code>
* parameter since we iterate over those keys and search for them in the <code>y</code> parameter.
*
* @param x
* @param y
* @return dotProduct
*/
public static <E> double dotProduct(Counter<E> x, Counter<E> y) {
double total = 0.0;
for (E keyX: x.keySet()){
total += x.getCount(keyX) * y.getCount(keyX);
}
return total;
}
private static final Random random = new Random();
public static <E> E sample(Counter<E> counter) {
double total = counter.totalCount();
double rand = random.nextDouble();
double sum = 0.0;
if (total <= 0.0) {
throw new RuntimeException("Non-positive counter total: " + total);
}
for (E key: counter.keySet()) {
double count = counter.getCount(key);
if (count < 0.0) {
throw new RuntimeException("Negative count in counter: " + key + " => " + count);
}
double prob = count / total;
sum += prob;
if (rand < sum) {
return key;
}
}
throw new RuntimeException("Shouldn't Reach Here");
}
}