| 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 |
import dr.util.HeapSort; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DiscreteStatistics { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 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; |
| 137 |
} else { |
| 138 |
count = count - 1; |
| 139 |
} |
| 140 |
|
| 141 |
return var / (double) count; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 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 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public static double stdev(double[] x) { |
| 219 |
return Math.sqrt(variance(x)); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public static double variance(double[] x) { |
| 229 |
final double m = mean(x); |
| 230 |
return variance(x, m); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public static double varianceSampleMean(double[] x, double mean) { |
| 242 |
return variance(x, mean) / (double) x.length; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public static double varianceSampleMean(double[] x) { |
| 252 |
return variance(x) / (double) x.length; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 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 |
|
| 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 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 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 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 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 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 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 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 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 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 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 |
} |