Debian Codegraph @

src/dr/stats/FrequencyCounter.java

raw symbols SCIP
1 /*
2 * FrequencyCounter.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.stats;
27
28
29 import dr.util.Pair;
30
31 import java.util.*;
32
33 /**
34 * A utility class to store the frequency of a set of discrete values
35 * and provide some basic statistics on them (mode, credible sets etc).
36 *
37 * @author Walter Xie
38 * @author Andrew Rambaut
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 // use a treemap so the keys are sorted by their value
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 * sort counter by counts to calculate correct credibility set
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 * the unique values in a frequency counter,
115 * which are also the keys of the map
116 *
117 * @return
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 * get the count from counter given a value
141 *
142 * @param value
143 * @return
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 * the total counts of this frequency counter
155 *
156 * @return
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 * rescale the frequency to make maximum count equal to 1.
168 * <code>count / max_count</code>.
169 * @param value
170 * @return
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 // include the last one to make totPr >= probability
216 if (totalProbability < probabilityThreshold) {
217 credibleSet.add(value);
218 } else {
219 incredibleSet.add(value);
220 }
221 totalProbability += probability;
222 }
223 }
224
225 }