1 16 package org.apache.commons.math.stat; 17 18 import org.apache.commons.math.stat.descriptive.UnivariateStatistic; 19 import org.apache.commons.math.stat.descriptive.moment.GeometricMean; 20 import org.apache.commons.math.stat.descriptive.moment.Mean; 21 import org.apache.commons.math.stat.descriptive.moment.Variance; 22 import org.apache.commons.math.stat.descriptive.rank.Max; 23 import org.apache.commons.math.stat.descriptive.rank.Min; 24 import org.apache.commons.math.stat.descriptive.rank.Percentile; 25 import org.apache.commons.math.stat.descriptive.summary.Product; 26 import org.apache.commons.math.stat.descriptive.summary.Sum; 27 import org.apache.commons.math.stat.descriptive.summary.SumOfLogs; 28 import org.apache.commons.math.stat.descriptive.summary.SumOfSquares; 29 30 36 public final class StatUtils { 37 38 39 private static UnivariateStatistic sum = new Sum(); 40 41 42 private static UnivariateStatistic sumSq = new SumOfSquares(); 43 44 45 private static UnivariateStatistic prod = new Product(); 46 47 48 private static UnivariateStatistic sumLog = new SumOfLogs(); 49 50 51 private static UnivariateStatistic min = new Min(); 52 53 54 private static UnivariateStatistic max = new Max(); 55 56 57 private static UnivariateStatistic mean = new Mean(); 58 59 60 private static Variance variance = new Variance(); 61 62 63 private static Percentile percentile = new Percentile(); 64 65 66 private static GeometricMean geometricMean = new GeometricMean(); 67 68 71 private StatUtils() { 72 } 73 74 86 public static double sum(final double[] values) { 87 return sum.evaluate(values); 88 } 89 90 104 public static double sum(final double[] values, final int begin, 105 final int length) { 106 return sum.evaluate(values, begin, length); 107 } 108 109 120 public static double sumSq(final double[] values) { 121 return sumSq.evaluate(values); 122 } 123 124 138 public static double sumSq(final double[] values, final int begin, 139 final int length) { 140 return sumSq.evaluate(values, begin, length); 141 } 142 143 153 public static double product(final double[] values) { 154 return prod.evaluate(values); 155 } 156 157 171 public static double product(final double[] values, final int begin, 172 final int length) { 173 return prod.evaluate(values, begin, length); 174 } 175 176 189 public static double sumLog(final double[] values) { 190 return sumLog.evaluate(values); 191 } 192 193 210 public static double sumLog(final double[] values, final int begin, 211 final int length) { 212 return sumLog.evaluate(values, begin, length); 213 } 214 215 228 public static double mean(final double[] values) { 229 return mean.evaluate(values); 230 } 231 232 249 public static double mean(final double[] values, final int begin, 250 final int length) { 251 return mean.evaluate(values, begin, length); 252 } 253 254 267 public static double geometricMean(final double[] values) { 268 return geometricMean.evaluate(values); 269 } 270 271 288 public static double geometricMean(final double[] values, final int begin, 289 final int length) { 290 return geometricMean.evaluate(values, begin, length); 291 } 292 293 294 309 public static double variance(final double[] values) { 310 return variance.evaluate(values); 311 } 312 313 333 public static double variance(final double[] values, final int begin, 334 final int length) { 335 return variance.evaluate(values, begin, length); 336 } 337 338 364 public static double variance(final double[] values, final double mean, 365 final int begin, final int length) { 366 return variance.evaluate(values, mean, begin, length); 367 } 368 369 391 public static double variance(final double[] values, final double mean) { 392 return variance.evaluate(values, mean); 393 } 394 395 412 public static double max(final double[] values) { 413 return max.evaluate(values); 414 } 415 416 438 public static double max(final double[] values, final int begin, 439 final int length) { 440 return max.evaluate(values, begin, length); 441 } 442 443 460 public static double min(final double[] values) { 461 return min.evaluate(values); 462 } 463 464 486 public static double min(final double[] values, final int begin, 487 final int length) { 488 return min.evaluate(values, begin, length); 489 } 490 491 514 public static double percentile(final double[] values, final double p) { 515 return percentile.evaluate(values,p); 516 } 517 518 545 public static double percentile(final double[] values, final int begin, 546 final int length, final double p) { 547 return percentile.evaluate(values, begin, length, p); 548 } 549 550 560 public static double sumDifference(final double[] sample1, final double[] sample2) 561 throws IllegalArgumentException { 562 int n = sample1.length; 563 if (n != sample2.length || n < 1) { 564 throw new IllegalArgumentException 565 ("Input arrays must have the same (positive) length."); 566 } 567 double result = 0; 568 for (int i = 0; i < n; i++) { 569 result += sample1[i] - sample2[i]; 570 } 571 return result; 572 } 573 574 584 public static double meanDifference(final double[] sample1, final double[] sample2) 585 throws IllegalArgumentException { 586 return sumDifference(sample1, sample2) / (double) sample1.length; 587 } 588 589 601 public static double varianceDifference(final double[] sample1, final double[] sample2, 602 double meanDifference) throws IllegalArgumentException { 603 double sum1 = 0d; 604 double sum2 = 0d; 605 double diff = 0d; 606 int n = sample1.length; 607 if (n < 2) { 608 throw new IllegalArgumentException ("Input array lengths must be at least 2."); 609 } 610 for (int i = 0; i < n; i++) { 611 diff = sample1[i] - sample2[i]; 612 sum1 += (diff - meanDifference) *(diff - meanDifference); 613 sum2 += diff - meanDifference; 614 } 615 return (sum1 - (sum2 * sum2 / (double) n)) / (double) (n - 1); 616 } 617 618 } | Popular Tags |