Debian Codegraph @

src/dr/stats/DiscreteStatistics.java

raw symbols SCIP
1 /*
2 * DiscreteStatistics.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 import dr.util.HeapSort;
29
30 /**
31 * simple discrete statistics (mean, variance, cumulative probability, quantiles etc.)
32 *
33 * @author Korbinian Strimmer
34 * @author Alexei Drummond
35 * @version $Id: DiscreteStatistics.java,v 1.11 2006/07/02 21:14:53 rambaut Exp $
36 */
37 public class DiscreteStatistics {
38 /**
39 * compute mean
40 *
41 * @param x list of numbers
42 * @return mean
43 */
44 public static double mean(double[] x) {
45 double m = 0;
46 int count = x.length;
47 for(double aX : x) {
48 if( Double.isNaN(aX) ) {
49 count--;
50 } else {
51 m += aX;
52 }
53 }
54
55 return m / (double) count;
56 }
57
58 /**
59 * compute median
60 *
61 * @param x list of numbers
62 * @param indices index sorting x
63 * @return median
64 */
65 public static double median(double[] x, int[] indices) {
66
67 int pos = x.length / 2;
68 if (x.length % 2 == 1) {
69 return x[indices[pos]];
70 } else {
71 return (x[indices[pos - 1]] + x[indices[pos]]) / 2.0;
72 }
73 }
74
75 /**
76 * compute the mean squared error
77 *
78 * @param x list of numbers
79 * @param trueValue truth
80 * @return MSE
81 */
82
83 public static double meanSquaredError(double[] x, double trueValue) {
84
85 if (x == null || x.length == 0) {
86 throw new IllegalArgumentException();
87 }
88
89 double total = 0;
90 for(double sample : x) {
91 total += (sample - trueValue) * (sample - trueValue);
92 }
93 total /= x.length;
94 return total;
95 }
96
97 /**
98 * compute median
99 *
100 * @param x list of numbers
101 * @return median
102 */
103 public static double median(double[] x) {
104
105 if (x == null || x.length == 0) {
106 throw new IllegalArgumentException();
107 }
108
109 int[] indices = new int[x.length];
110 HeapSort.sort(x, indices);
111
112 return median(x, indices);
113 }
114
115
116 /**
117 * compute variance (ML estimator)
118 *
119 * @param x list of numbers
120 * @param mean assumed mean of x
121 * @return variance of x (ML estimator)
122 */
123 public static double variance(double[] x, double mean) {
124 double var = 0;
125 int count = x.length;
126 for(double aX : x) {
127 if( Double.isNaN(aX) ) {
128 count--;
129 } else {
130 double diff = aX - mean;
131 var += diff * diff;
132 }
133 }
134
135 if (count < 2) {
136 count = 1; // to avoid division by zero
137 } else {
138 count = count - 1; // for ML estimate
139 }
140
141 return var / (double) count;
142 }
143
144
145 /**
146 * compute covariance
147 *
148 * @param x list of numbers
149 * @param y list of numbers
150 * @return covariance of x and y
151 */
152 @SuppressWarnings({"SuspiciousNameCombination"})
153 public static double covariance(double[] x, double[] y) {
154
155 return covariance(x, y, mean(x), mean(y), stdev(x), stdev(y));
156 }
157
158 /**
159 * compute covariance
160 *
161 * @param x list of numbers
162 * @param y list of numbers
163 * @param xmean assumed mean of x
164 * @param ymean assumed mean of y
165 * @param xstdev assumed stdev of x
166 * @param ystdev assumed stdev of y
167 * @return covariance of x and y
168 */
169 public static double covariance(double[] x, double[] y, double xmean, double ymean, double xstdev, double ystdev) {
170
171 if (x.length != y.length) throw new IllegalArgumentException("x and y arrays must be same length!");
172
173 int count = x.length;
174 double covar = 0.0;
175 for (int i = 0; i < x.length; i++) {
176 if (Double.isNaN(x[i]) || Double.isNaN(y[i])) {
177 count --;
178 } else {
179 covar += (x[i] - xmean) * (y[i] - ymean);
180 }
181 }
182 covar /= count;
183 covar /= (xstdev * ystdev);
184 return covar;
185 }
186
187 /**
188 * compute fisher skewness
189 *
190 * @param x list of numbers
191 * @return skewness of x
192 */
193 public static double skewness(double[] x) {
194
195 double mean = mean(x);
196 double stdev = stdev(x);
197 double skew = 0.0;
198 double len = x.length;
199
200 for (double xv : x) {
201 double diff = xv - mean;
202 diff /= stdev;
203
204 skew += (diff * diff * diff);
205 }
206
207 skew *= (len / ((len - 1) * (len - 2)));
208
209 return skew;
210 }
211
212 /**
213 * compute standard deviation
214 *
215 * @param x list of numbers
216 * @return standard deviation of x
217 */
218 public static double stdev(double[] x) {
219 return Math.sqrt(variance(x));
220 }
221
222 /**
223 * compute variance (ML estimator)
224 *
225 * @param x list of numbers
226 * @return variance of x (ML estimator)
227 */
228 public static double variance(double[] x) {
229 final double m = mean(x);
230 return variance(x, m);
231 }
232
233
234 /**
235 * compute variance of sample mean (ML estimator)
236 *
237 * @param x list of numbers
238 * @param mean assumed mean of x
239 * @return variance of x (ML estimator)
240 */
241 public static double varianceSampleMean(double[] x, double mean) {
242 return variance(x, mean) / (double) x.length;
243 }
244
245 /**
246 * compute variance of sample mean (ML estimator)
247 *
248 * @param x list of numbers
249 * @return variance of x (ML estimator)
250 */
251 public static double varianceSampleMean(double[] x) {
252 return variance(x) / (double) x.length;
253 }
254
255
256 /**
257 * compute the q-th quantile for a distribution of x
258 * (= inverse cdf)
259 *
260 * @param q quantile (0 < q <= 1)
261 * @param x discrete distribution (an unordered list of numbers)
262 * @param indices index sorting x
263 * @return q-th quantile
264 */
265 public static double quantile(double q, double[] x, int[] indices) {
266 if (q < 0.0 || q > 1.0) throw new IllegalArgumentException("Quantile out of range");
267
268 if (q == 0.0) {
269 // for q==0 we have to "invent" an entry smaller than the smallest x
270
271 return x[indices[0]] - 1.0;
272 }
273
274 return x[indices[(int) Math.ceil(q * indices.length) - 1]];
275 }
276
277 /**
278 * compute the q-th quantile for a distribution of x
279 * (= inverse cdf)
280 *
281 * @param q quantile (0 <= q <= 1)
282 * @param x discrete distribution (an unordered list of numbers)
283 * @return q-th quantile
284 */
285 public static double quantile(double q, double[] x) {
286 int[] indices = new int[x.length];
287 HeapSort.sort(x, indices);
288
289 return quantile(q, x, indices);
290 }
291
292 /**
293 * compute the q-th quantile for a distribution of x
294 * (= inverse cdf)
295 *
296 * @param q quantile (0 <= q <= 1)
297 * @param x discrete distribution (an unordered list of numbers)
298 * @param count use only first count entries in x
299 * @return q-th quantile
300 */
301 public static double quantile(double q, double[] x, int count) {
302 int[] indices = new int[count];
303 HeapSort.sort(x, indices);
304
305 return quantile(q, x, indices);
306 }
307
308 /**
309 * Determine the highest posterior density for a list of values.
310 * The HPD is the smallest interval containing the required amount of elements.
311 *
312 * @param proportion of elements inside the interval
313 * @param x values
314 * @param indices index sorting x
315 * @return the interval, an array of {low, high} values.
316 */
317 public static double[] HPDInterval(double proportion, double[] x, int[] indices) {
318
319 double minRange = Double.MAX_VALUE;
320 int hpdIndex = 0;
321
322 final int diff = (int) Math.round(proportion * (double) x.length);
323 for (int i = 0; i <= (x.length - diff); i++) {
324 final double minValue = x[indices[i]];
325 final double maxValue = x[indices[i + diff - 1]];
326 final double range = Math.abs(maxValue - minValue);
327 if (range < minRange) {
328 minRange = range;
329 hpdIndex = i;
330 }
331 }
332
333 return new double[]{x[indices[hpdIndex]], x[indices[hpdIndex + diff - 1]]};
334 }
335
336 /**
337 * compute the cumulative probability Pr(x <= z) for a given z
338 * and a distribution of x
339 *
340 * @param z threshold value
341 * @param x discrete distribution (an unordered list of numbers)
342 * @param indices index sorting x
343 * @return cumulative probability
344 */
345 public static double cdf(double z, double[] x, int[] indices) {
346 int i;
347 for (i = 0; i < x.length; i++) {
348 if (x[indices[i]] > z) break;
349 }
350
351 return (double) i / (double) x.length;
352 }
353
354 /**
355 * compute the cumulative probability Pr(x <= z) for a given z
356 * and a distribution of x
357 *
358 * @param z threshold value
359 * @param x discrete distribution (an unordered list of numbers)
360 * @return cumulative probability
361 */
362 public static double cdf(double z, double[] x) {
363 int[] indices = new int[x.length];
364 HeapSort.sort(x, indices);
365
366 return cdf(z, x, indices);
367 }
368
369 public static double max(double[] x) {
370 double max = x[0];
371 for (int i = 1; i < x.length; i++) {
372 if (x[i] > max) max = x[i];
373 }
374 return max;
375 }
376
377 public static double min(double[] x) {
378 double min = x[0];
379 for (int i = 1; i < x.length; i++) {
380 if (x[i] < min) min = x[i];
381 }
382 return min;
383 }
384
385 public static double geometricMean(double[] x) {
386 double gm = 0;
387 int len = x.length;
388 for (int i = 0; i < len; i++)
389 {
390 gm += Math.log(x[i]);
391 }
392
393 return Math.exp(gm/(double) len);
394 }
395 }