1 16 package org.apache.commons.math.stat.inference; 17 18 import org.apache.commons.math.MathException; 19 import org.apache.commons.math.distribution.DistributionFactory; 20 import org.apache.commons.math.distribution.TDistribution; 21 import org.apache.commons.math.stat.StatUtils; 22 import org.apache.commons.math.stat.descriptive.StatisticalSummary; 23 24 32 public class TTestImpl implements TTest { 33 34 35 private DistributionFactory distributionFactory = null; 36 37 40 public TTestImpl() { 41 super(); 42 } 43 44 64 public double pairedT(double[] sample1, double[] sample2) 65 throws IllegalArgumentException , MathException { 66 if ((sample1 == null) || (sample2 == null || 67 Math.min(sample1.length, sample2.length) < 2)) { 68 throw new IllegalArgumentException ("insufficient data for t statistic"); 69 } 70 double meanDifference = StatUtils.meanDifference(sample1, sample2); 71 return t(meanDifference, 0, 72 StatUtils.varianceDifference(sample1, sample2, meanDifference), 73 (double) sample1.length); 74 } 75 76 109 public double pairedTTest(double[] sample1, double[] sample2) 110 throws IllegalArgumentException , MathException { 111 double meanDifference = StatUtils.meanDifference(sample1, sample2); 112 return tTest(meanDifference, 0, 113 StatUtils.varianceDifference(sample1, sample2, meanDifference), 114 (double) sample1.length); 115 } 116 117 149 public boolean pairedTTest(double[] sample1, double[] sample2, double alpha) 150 throws IllegalArgumentException , MathException { 151 if ((alpha <= 0) || (alpha > 0.5)) { 152 throw new IllegalArgumentException ("bad significance level: " + alpha); 153 } 154 return (pairedTTest(sample1, sample2) < alpha); 155 } 156 157 172 public double t(double mu, double[] observed) 173 throws IllegalArgumentException { 174 if ((observed == null) || (observed.length < 2)) { 175 throw new IllegalArgumentException ("insufficient data for t statistic"); 176 } 177 return t(StatUtils.mean(observed), mu, StatUtils.variance(observed), 178 observed.length); 179 } 180 181 197 public double t(double mu, StatisticalSummary sampleStats) 198 throws IllegalArgumentException { 199 if ((sampleStats == null) || (sampleStats.getN() < 2)) { 200 throw new IllegalArgumentException ("insufficient data for t statistic"); 201 } 202 return t(sampleStats.getMean(), mu, sampleStats.getVariance(), 203 sampleStats.getN()); 204 } 205 206 239 public double homoscedasticT(double[] sample1, double[] sample2) 240 throws IllegalArgumentException { 241 if ((sample1 == null) || (sample2 == null || 242 Math.min(sample1.length, sample2.length) < 2)) { 243 throw new IllegalArgumentException ("insufficient data for t statistic"); 244 } 245 return homoscedasticT(StatUtils.mean(sample1), StatUtils.mean(sample2), 246 StatUtils.variance(sample1), StatUtils.variance(sample2), 247 (double) sample1.length, (double) sample2.length); 248 } 249 250 278 public double t(double[] sample1, double[] sample2) 279 throws IllegalArgumentException { 280 if ((sample1 == null) || (sample2 == null || 281 Math.min(sample1.length, sample2.length) < 2)) { 282 throw new IllegalArgumentException ("insufficient data for t statistic"); 283 } 284 return t(StatUtils.mean(sample1), StatUtils.mean(sample2), 285 StatUtils.variance(sample1), StatUtils.variance(sample2), 286 (double) sample1.length, (double) sample2.length); 287 } 288 289 320 public double t(StatisticalSummary sampleStats1, 321 StatisticalSummary sampleStats2) 322 throws IllegalArgumentException { 323 if ((sampleStats1 == null) || 324 (sampleStats2 == null || 325 Math.min(sampleStats1.getN(), sampleStats2.getN()) < 2)) { 326 throw new IllegalArgumentException ("insufficient data for t statistic"); 327 } 328 return t(sampleStats1.getMean(), sampleStats2.getMean(), 329 sampleStats1.getVariance(), sampleStats2.getVariance(), 330 (double) sampleStats1.getN(), (double) sampleStats2.getN()); 331 } 332 333 368 public double homoscedasticT(StatisticalSummary sampleStats1, 369 StatisticalSummary sampleStats2) 370 throws IllegalArgumentException { 371 if ((sampleStats1 == null) || 372 (sampleStats2 == null || 373 Math.min(sampleStats1.getN(), sampleStats2.getN()) < 2)) { 374 throw new IllegalArgumentException ("insufficient data for t statistic"); 375 } 376 return homoscedasticT(sampleStats1.getMean(), sampleStats2.getMean(), 377 sampleStats1.getVariance(), sampleStats2.getVariance(), 378 (double) sampleStats1.getN(), (double) sampleStats2.getN()); 379 } 380 381 407 public double tTest(double mu, double[] sample) 408 throws IllegalArgumentException , MathException { 409 if ((sample == null) || (sample.length < 2)) { 410 throw new IllegalArgumentException ("insufficient data for t statistic"); 411 } 412 return tTest( StatUtils.mean(sample), mu, StatUtils.variance(sample), 413 sample.length); 414 } 415 416 451 public boolean tTest(double mu, double[] sample, double alpha) 452 throws IllegalArgumentException , MathException { 453 if ((alpha <= 0) || (alpha > 0.5)) { 454 throw new IllegalArgumentException ("bad significance level: " + alpha); 455 } 456 return (tTest(mu, sample) < alpha); 457 } 458 459 487 public double tTest(double mu, StatisticalSummary sampleStats) 488 throws IllegalArgumentException , MathException { 489 if ((sampleStats == null) || (sampleStats.getN() < 2)) { 490 throw new IllegalArgumentException ("insufficient data for t statistic"); 491 } 492 return tTest(sampleStats.getMean(), mu, sampleStats.getVariance(), 493 sampleStats.getN()); 494 } 495 496 532 public boolean tTest( double mu, StatisticalSummary sampleStats, 533 double alpha) 534 throws IllegalArgumentException , MathException { 535 if ((alpha <= 0) || (alpha > 0.5)) { 536 throw new IllegalArgumentException ("bad significance level: " + alpha); 537 } 538 return (tTest(mu, sampleStats) < alpha); 539 } 540 541 577 public double tTest(double[] sample1, double[] sample2) 578 throws IllegalArgumentException , MathException { 579 if ((sample1 == null) || (sample2 == null || 580 Math.min(sample1.length, sample2.length) < 2)) { 581 throw new IllegalArgumentException ("insufficient data"); 582 } 583 return tTest(StatUtils.mean(sample1), StatUtils.mean(sample2), 584 StatUtils.variance(sample1), StatUtils.variance(sample2), 585 (double) sample1.length, (double) sample2.length); 586 } 587 588 621 public double homoscedasticTTest(double[] sample1, double[] sample2) 622 throws IllegalArgumentException , MathException { 623 if ((sample1 == null) || (sample2 == null || 624 Math.min(sample1.length, sample2.length) < 2)) { 625 throw new IllegalArgumentException ("insufficient data"); 626 } 627 return homoscedasticTTest(StatUtils.mean(sample1), 628 StatUtils.mean(sample2), StatUtils.variance(sample1), 629 StatUtils.variance(sample2), (double) sample1.length, 630 (double) sample2.length); 631 } 632 633 634 685 public boolean tTest(double[] sample1, double[] sample2, 686 double alpha) 687 throws IllegalArgumentException , MathException { 688 if ((alpha <= 0) || (alpha > 0.5)) { 689 throw new IllegalArgumentException ("bad significance level: " + alpha); 690 } 691 return (tTest(sample1, sample2) < alpha); 692 } 693 694 745 public boolean homoscedasticTTest(double[] sample1, double[] sample2, 746 double alpha) 747 throws IllegalArgumentException , MathException { 748 if ((alpha <= 0) || (alpha > 0.5)) { 749 throw new IllegalArgumentException ("bad significance level: " + alpha); 750 } 751 return (homoscedasticTTest(sample1, sample2) < alpha); 752 } 753 754 788 public double tTest(StatisticalSummary sampleStats1, StatisticalSummary sampleStats2) 789 throws IllegalArgumentException , MathException { 790 if ((sampleStats1 == null) || (sampleStats2 == null || 791 Math.min(sampleStats1.getN(), sampleStats2.getN()) < 2)) { 792 throw new IllegalArgumentException ("insufficient data for t statistic"); 793 } 794 return tTest(sampleStats1.getMean(), sampleStats2.getMean(), sampleStats1.getVariance(), 795 sampleStats2.getVariance(), (double) sampleStats1.getN(), 796 (double) sampleStats2.getN()); 797 } 798 799 832 public double homoscedasticTTest(StatisticalSummary sampleStats1, 833 StatisticalSummary sampleStats2) 834 throws IllegalArgumentException , MathException { 835 if ((sampleStats1 == null) || (sampleStats2 == null || 836 Math.min(sampleStats1.getN(), sampleStats2.getN()) < 2)) { 837 throw new IllegalArgumentException ("insufficient data for t statistic"); 838 } 839 return homoscedasticTTest(sampleStats1.getMean(), 840 sampleStats2.getMean(), sampleStats1.getVariance(), 841 sampleStats2.getVariance(), (double) sampleStats1.getN(), 842 (double) sampleStats2.getN()); 843 } 844 845 898 public boolean tTest(StatisticalSummary sampleStats1, 899 StatisticalSummary sampleStats2, double alpha) 900 throws IllegalArgumentException , MathException { 901 if ((alpha <= 0) || (alpha > 0.5)) { 902 throw new IllegalArgumentException ("bad significance level: " + alpha); 903 } 904 return (tTest(sampleStats1, sampleStats2) < alpha); 905 } 906 907 909 913 protected DistributionFactory getDistributionFactory() { 914 if (distributionFactory == null) { 915 distributionFactory = DistributionFactory.newInstance(); 916 } 917 return distributionFactory; 918 } 919 920 929 protected double df(double v1, double v2, double n1, double n2) { 930 return (((v1 / n1) + (v2 / n2)) * ((v1 / n1) + (v2 / n2))) / 931 ((v1 * v1) / (n1 * n1 * (n1 - 1d)) + (v2 * v2) / 932 (n2 * n2 * (n2 - 1d))); 933 } 934 935 944 protected double t(double m, double mu, double v, double n) { 945 return (m - mu) / Math.sqrt(v / n); 946 } 947 948 961 protected double t(double m1, double m2, double v1, double v2, double n1, 962 double n2) { 963 return (m1 - m2) / Math.sqrt((v1 / n1) + (v2 / n2)); 964 } 965 966 978 protected double homoscedasticT(double m1, double m2, double v1, 979 double v2, double n1, double n2) { 980 double pooledVariance = ((n1 - 1) * v1 + (n2 -1) * v2 ) / (n1 + n2 - 2); 981 return (m1 - m2) / Math.sqrt(pooledVariance * (1d / n1 + 1d / n2)); 982 } 983 984 994 protected double tTest(double m, double mu, double v, double n) 995 throws MathException { 996 double t = Math.abs(t(m, mu, v, n)); 997 TDistribution tDistribution = 998 getDistributionFactory().createTDistribution(n - 1); 999 return 1.0 - tDistribution.cumulativeProbability(-t, t); 1000 } 1001 1002 1017 protected double tTest(double m1, double m2, double v1, double v2, 1018 double n1, double n2) 1019 throws MathException { 1020 double t = Math.abs(t(m1, m2, v1, v2, n1, n2)); 1021 double degreesOfFreedom = 0; 1022 degreesOfFreedom= df(v1, v2, n1, n2); 1023 TDistribution tDistribution = 1024 getDistributionFactory().createTDistribution(degreesOfFreedom); 1025 return 1.0 - tDistribution.cumulativeProbability(-t, t); 1026 } 1027 1028 1043 protected double homoscedasticTTest(double m1, double m2, double v1, 1044 double v2, double n1, double n2) 1045 throws MathException { 1046 double t = Math.abs(homoscedasticT(m1, m2, v1, v2, n1, n2)); 1047 double degreesOfFreedom = 0; 1048 degreesOfFreedom = (double) (n1 + n2 - 2); 1049 TDistribution tDistribution = 1050 getDistributionFactory().createTDistribution(degreesOfFreedom); 1051 return 1.0 - tDistribution.cumulativeProbability(-t, t); 1052 } 1053} 1054 | Popular Tags |