Debian Codegraph @

src/dr/inference/trace/TraceDistribution.java

raw symbols SCIP
1 /*
2 * TraceDistribution.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.inference.trace;
27
28 import dr.stats.DiscreteStatistics;
29 import dr.stats.FrequencyCounter;
30 import dr.util.HeapSort;
31
32 import java.util.*;
33
34 /**
35 * A class that stores the distribution statistics for a trace
36 *
37 * @author Andrew Rambaut
38 * @author Alexei Drummond
39 * @version $Id: TraceDistribution.java,v 1.1.1.2 2006/04/25 23:00:09 rambaut Exp $
40 */
41 public class TraceDistribution {
42 private TraceType traceType;
43
44 // For discrete and categorical traces
45 private Map<Integer, String> categoryLabelMap;
46 private List<Integer> categoryOrder;
47 private FrequencyCounter<Integer> frequencyCounter;
48
49 public TraceDistribution(List<Double> values, TraceType traceType) {
50 this(values, traceType, false);
51 }
52
53 public TraceDistribution(List<Double> values, TraceType traceType, boolean isConstant) {
54 this.traceType = traceType;
55 this.isConstant = isConstant;
56
57 assert traceType != TraceType.CATEGORICAL : "cant use this constructor with categorical data";
58
59 initStatistics(values, 0.95);
60 }
61
62 public TraceDistribution(List<Double> values, Map<Integer, String> categoryLabelMap, List<Integer> categoryOrder, boolean isConstant) {
63 this.traceType = TraceType.CATEGORICAL;
64 this.categoryLabelMap = categoryLabelMap;
65 this.categoryOrder = categoryOrder;
66 this.isConstant = isConstant;
67 initStatistics(values, 0.95);
68 }
69
70 private void initStatistics(List<Double> values, double proportion) {
71 if (values.size() < 1) throw new RuntimeException("There is no value sent to statistics calculation !");
72
73 if (traceType.isNumber()) {
74 analyseDistributionNumeric(values, proportion);
75 }
76
77 if (traceType != TraceType.REAL) {
78 analyseDistributionDiscrete(values, proportion);
79 }
80 }
81
82 public TraceType getTraceType() {
83 return traceType;
84 }
85
86 public int getSize() {
87 return size;
88 }
89
90 public double getMean() {
91 return mean;
92 }
93
94 public double getVariance() {
95 return variance;
96 }
97
98 public double getStdError() {
99 return stdError;
100 }
101
102 public boolean hasGeometricMean() {
103 return hasGeometricMean;
104 }
105
106 public double getGeometricMean() {
107 return geometricMean;
108 }
109
110 public boolean isConstant() {
111 return isConstant;
112 }
113
114 public double getMedian() {
115 return median;
116 }
117
118 public double getQ1() {
119 return q1;
120 }
121
122 public double getQ3() {
123 return q3;
124 }
125
126 public double getLowerHPD() {
127 return hpdLower;
128 }
129
130 public double getUpperHPD() {
131 return hpdUpper;
132 }
133
134 public double getLowerCPD() {
135 return cpdLower;
136 }
137
138 public double getUpperCPD() {
139 return cpdUpper;
140 }
141
142 public double getMinimum() {
143 return minimum;
144 }
145
146 public double getMaximum() {
147 return maximum;
148 }
149
150 public double getHpdLowerCustom() {
151 return hpdLowerCustom;
152 }
153
154 public double getHpdUpperCustom() {
155 return hpdUpperCustom;
156 }
157
158 public double getMeanSquaredError(double[] values, double trueValue) {
159
160 if (values == null) {
161 throw new RuntimeException("Trace values not yet set");
162 }
163
164 if (traceType.isNumber()) {
165 return DiscreteStatistics.meanSquaredError(values, trueValue);
166 } else {
167 throw new RuntimeException("Require Real or Ordinal Trace Type in the Trace Distribution: " + this);
168 }
169 }
170
171 /**
172 * The major method to analyse traces in numeric values including Double, Integer
173 * @param valueList the values to analyze
174 */
175 private void analyseDistributionNumeric(List<Double> valueList, double proportion) {
176 double[] values = new double[valueList.size()];
177 for (int i = 0; i < valueList.size(); i++) {
178 values[i] = valueList.get(i);
179 }
180
181 size = values.length;
182
183 if (!isConstant) {
184 minimum = Double.POSITIVE_INFINITY;
185 maximum = Double.NEGATIVE_INFINITY;
186
187 for (double value : values) {
188 if (value < minimum) minimum = value;
189 if (value > maximum) maximum = value;
190 }
191
192 if (maximum == minimum) {
193 isConstant = true;
194 }
195 }
196
197 if (isConstant) {
198 mean = values[0];
199 stdError = 0;
200 variance = 0;
201 minimum = maximum = values[0];
202
203 return;
204 } else {
205 mean = DiscreteStatistics.mean(values);
206 stdError = DiscreteStatistics.stdev(values);
207 variance = DiscreteStatistics.variance(values);
208
209 if (minimum > 0) {
210 geometricMean = DiscreteStatistics.geometricMean(values);
211 hasGeometricMean = true;
212 }
213 }
214
215 int[] indices = new int[values.length];
216 HeapSort.sort(values, indices);
217 median = DiscreteStatistics.quantile(0.5, values, indices);
218 cpdLower = DiscreteStatistics.quantile(0.025, values, indices);
219 cpdUpper = DiscreteStatistics.quantile(0.975, values, indices);
220 q1 = DiscreteStatistics.quantile(0.25, values, indices);
221 q3 = DiscreteStatistics.quantile(0.75, values, indices);
222 calculateHPDInterval(proportion, values, indices);
223 // ESS = values.length; // move to TraceCorrelation
224 calculateHPDIntervalCustom(0.5, values, indices);
225
226 }
227
228 /**
229 * @param proportion the proportion of probability mass included within interval.
230 * @param array the data array
231 * @param indices the indices of the ranks of the values (sort order)
232 */
233 private void calculateHPDInterval(double proportion, double[] array, int[] indices) {
234 final double[] hpd = DiscreteStatistics.HPDInterval(proportion, array, indices);
235 hpdLower = hpd[0];
236 hpdUpper = hpd[1];
237 }
238
239 private void calculateHPDIntervalCustom(double proportion, double[] array, int[] indices) {
240 final double[] hpd = DiscreteStatistics.HPDInterval(proportion, array, indices);
241 hpdLowerCustom = hpd[0];
242 hpdUpperCustom = hpd[1];
243 }
244
245 private boolean isConstant;
246
247 protected boolean hasGeometricMean = false;
248
249 protected int size = 0;
250 protected double minimum, maximum;
251 protected double mean;
252 protected double median, q1, q3;
253 protected double geometricMean;
254 protected double stdError, meanSquaredError;
255 protected double variance;
256 protected double cpdLower, cpdUpper, hpdLower, hpdUpper;
257 protected double hpdLowerCustom, hpdUpperCustom;
258
259 //************************************************************************
260 // new types
261 //************************************************************************
262
263
264 // init FrequencyCounter used for Integer and String
265 private void analyseDistributionDiscrete(List<Double> valueList, double proportion) {
266 List<Integer> integerValues = new ArrayList<Integer>();
267 for (Double value : valueList) {
268 integerValues.add(value.intValue());
269 }
270
271 if (size == 0) {
272 size = integerValues.size();
273 }
274
275 if (getTraceType() == TraceType.CATEGORICAL) {
276 frequencyCounter = new FrequencyCounter<Integer>(integerValues, proportion);
277 } else {
278 frequencyCounter = new FrequencyCounter<Integer>(integerValues);
279 }
280
281 }
282
283 public Set<Integer> getValueSet() {
284 if (categoryOrder != null) {
285 return new LinkedHashSet<Integer>(categoryOrder);
286 } else {
287 return new LinkedHashSet<Integer>(frequencyCounter.getUniqueValues());
288 }
289 }
290
291 public FrequencyCounter<Integer> getFrequencyCounter() {
292 assert traceType.isDiscrete();
293 return frequencyCounter;
294 }
295
296 public Set<Integer> getCredibleSet() {
297 return frequencyCounter.getCredibleSet();
298 }
299
300 public Set<Integer> getIncredibleSet() {
301 return frequencyCounter.getIncredibleSet();
302 }
303
304 public String valueToString(int value) {
305 if (categoryLabelMap != null) {
306 return categoryLabelMap.get(value);
307 }
308 return Integer.toString(value);
309 }
310 public String setToString(Set<Integer> aSet) {
311 StringBuilder sb = new StringBuilder("{");
312 boolean isFirst = true;
313 for (int value : aSet) {
314 String label = valueToString(value);
315 if (!isFirst) {
316 sb.append(", ");
317 } else {
318 isFirst = false;
319 }
320 sb.append(label);
321 }
322 sb.append("}");
323 return sb.toString();
324 }
325
326 public int getMode() {
327 return frequencyCounter.getMode();
328 }
329
330 public int getFrequencyOfMode() {
331 return frequencyCounter.getFrequency(getMode());
332 }
333
334 public double getProbabilityOfMode() {
335 if (getSize() > 0) {
336 return (double) getFrequencyOfMode() / (double) getSize();
337 }
338 return 0;
339 }
340
341 public List<String> getRange() {
342 List<String> valuesList = new ArrayList<String>();
343 for (Integer value : frequencyCounter.getUniqueValues() ) {
344 if (traceType.isInteger()) { // as Integer is stored as Double in Trace
345 if (!valuesList.contains(Integer.toString(((Number) value).intValue())))
346 valuesList.add(Integer.toString(((Number) value).intValue()));
347 } else {
348 if (!valuesList.contains(value.toString()))
349 valuesList.add(value.toString());
350 }
351 }
352 return valuesList;
353 }
354
355
356 }