| 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.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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class TraceDistribution { |
| 42 |
private TraceType traceType; |
| 43 |
|
| 44 |
|
| 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 |
|
| 173 |
|
| 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 |
|
| 224 |
calculateHPDIntervalCustom(0.5, values, indices); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 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()) { |
| 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 |
} |