| 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 |
package dr.stats; |
| 27 |
|
| 28 |
|
| 29 |
import dr.util.Pair; |
| 30 |
|
| 31 |
import java.util.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public final class FrequencyCounter<T extends Comparable> { |
| 41 |
|
| 42 |
private final List<T> uniqueValues; |
| 43 |
private final Map<T, Integer> frequencies; |
| 44 |
private final Set<T> credibleSet = new LinkedHashSet<T>(); |
| 45 |
private final Set<T> incredibleSet = new LinkedHashSet<T>(); |
| 46 |
|
| 47 |
private final int total; |
| 48 |
private final int min; |
| 49 |
private final int max; |
| 50 |
private final T mode; |
| 51 |
|
| 52 |
public FrequencyCounter(List<T> values) { |
| 53 |
this(values, 0.0); |
| 54 |
} |
| 55 |
|
| 56 |
public FrequencyCounter(List<T> values, double probabilityThreshold) { |
| 57 |
|
| 58 |
|
| 59 |
frequencies = new TreeMap<T, Integer>(); |
| 60 |
|
| 61 |
for (T value : values) { |
| 62 |
if (frequencies.containsKey(value)) { |
| 63 |
int i = frequencies.get(value) + 1; |
| 64 |
frequencies.put(value, i); |
| 65 |
} else { |
| 66 |
frequencies.put(value, 1); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
this.uniqueValues = new ArrayList<T>(frequencies.keySet()); |
| 71 |
|
| 72 |
total = calculateTotalFrequency(); |
| 73 |
int[] minMax = calculateMinMaxFrequency(); |
| 74 |
min = minMax[0]; |
| 75 |
max = minMax[1]; |
| 76 |
mode = calculateMode(); |
| 77 |
|
| 78 |
if (probabilityThreshold > 0.0) { |
| 79 |
calculateCredibleSet(probabilityThreshold); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public Map<T, Integer> getFrequencies() { |
| 84 |
return frequencies; |
| 85 |
} |
| 86 |
|
| 87 |
public int getSize() { |
| 88 |
return frequencies.size(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public List<Integer> getOrderByFrequency() { |
| 95 |
List<Pair<T, Integer>> values = new ArrayList<Pair<T, Integer>>(); |
| 96 |
int i = 0; |
| 97 |
for (T value : uniqueValues) { |
| 98 |
values.add(new Pair<T, Integer>(value, i)); |
| 99 |
i++; |
| 100 |
} |
| 101 |
Collections.sort(values, new Comparator<Pair<T, Integer>>() { |
| 102 |
public int compare(Pair<T, Integer> value1, Pair<T, Integer> value2) { |
| 103 |
return getFrequency(value2.fst) - getFrequency(value1.fst); |
| 104 |
} |
| 105 |
}); |
| 106 |
List<Integer> order = new ArrayList<Integer>(); |
| 107 |
for (Pair<T, Integer> value : values) { |
| 108 |
order.add(value.snd); |
| 109 |
} |
| 110 |
return order; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public List<T> getUniqueValues() { |
| 120 |
List<T> values = new ArrayList<T>(uniqueValues); |
| 121 |
Collections.sort(values); |
| 122 |
return values; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
public int getMinFrequency() { |
| 127 |
return min; |
| 128 |
} |
| 129 |
|
| 130 |
public int getMaxFrequency() { |
| 131 |
return max; |
| 132 |
} |
| 133 |
|
| 134 |
public T getMode() { |
| 135 |
return mode; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public int getFrequency(T value) { |
| 146 |
if (frequencies.containsKey(value)) { |
| 147 |
return frequencies.get(value); |
| 148 |
} else { |
| 149 |
return 0; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public int getTotalFrequency() { |
| 159 |
return total; |
| 160 |
} |
| 161 |
|
| 162 |
public double getProbability(T value) { |
| 163 |
return (double) getFrequency(value) / (double)total; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public double getProportionalFrequency(T value) { |
| 173 |
return (double) getFrequency(value) / (double)max; |
| 174 |
} |
| 175 |
|
| 176 |
public Set<T> getCredibleSet() { |
| 177 |
return credibleSet; |
| 178 |
} |
| 179 |
|
| 180 |
public Set<T> getIncredibleSet() { |
| 181 |
return incredibleSet; |
| 182 |
} |
| 183 |
|
| 184 |
private int calculateTotalFrequency() { |
| 185 |
int total = 0; |
| 186 |
for (int value : frequencies.values()) { |
| 187 |
total += value; |
| 188 |
} |
| 189 |
return total; |
| 190 |
} |
| 191 |
|
| 192 |
private int[] calculateMinMaxFrequency() { |
| 193 |
int min = Collections.min(frequencies.values()); |
| 194 |
int max = Collections.max(frequencies.values()); |
| 195 |
return new int[]{min, max}; |
| 196 |
} |
| 197 |
|
| 198 |
private T calculateMode() { |
| 199 |
int maxFreq = 0; |
| 200 |
T mode = null; |
| 201 |
for (T value : getUniqueValues()) { |
| 202 |
if (getFrequency(value) > maxFreq) { |
| 203 |
maxFreq = getFrequency(value); |
| 204 |
mode = value; |
| 205 |
} |
| 206 |
} |
| 207 |
return mode; |
| 208 |
} |
| 209 |
|
| 210 |
private void calculateCredibleSet(double probabilityThreshold) { |
| 211 |
double totalProbability = 0; |
| 212 |
for (int index : getOrderByFrequency()) { |
| 213 |
T value = uniqueValues.get(index); |
| 214 |
double probability = getProbability(value); |
| 215 |
|
| 216 |
if (totalProbability < probabilityThreshold) { |
| 217 |
credibleSet.add(value); |
| 218 |
} else { |
| 219 |
incredibleSet.add(value); |
| 220 |
} |
| 221 |
totalProbability += probability; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
} |