001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.commons.math.stat.inference; 018 019 import java.util.Collection; 020 import org.apache.commons.math.MathException; 021 import org.apache.commons.math.stat.descriptive.StatisticalSummary; 022 023 /** 024 * A collection of static methods to create inference test instances or to 025 * perform inference tests. 026 * 027 * @since 1.1 028 * @version $Revision: 811827 $ $Date: 2009-09-06 11:32:50 -0400 (Sun, 06 Sep 2009) $ 029 */ 030 public class TestUtils { 031 032 /** Singleton TTest instance using default implementation. */ 033 private static TTest tTest = new TTestImpl(); 034 035 /** Singleton ChiSquareTest instance using default implementation. */ 036 private static ChiSquareTest chiSquareTest = 037 new ChiSquareTestImpl(); 038 039 /** Singleton ChiSquareTest instance using default implementation. */ 040 private static UnknownDistributionChiSquareTest unknownDistributionChiSquareTest = 041 new ChiSquareTestImpl(); 042 043 /** Singleton OneWayAnova instance using default implementation. */ 044 private static OneWayAnova oneWayAnova = 045 new OneWayAnovaImpl(); 046 047 /** 048 * Prevent instantiation. 049 */ 050 protected TestUtils() { 051 super(); 052 } 053 054 /** 055 * Set the (singleton) TTest instance. 056 * 057 * @param chiSquareTest the new instance to use 058 * @since 1.2 059 */ 060 public static void setChiSquareTest(TTest chiSquareTest) { 061 TestUtils.tTest = chiSquareTest; 062 } 063 064 /** 065 * Return a (singleton) TTest instance. Does not create a new instance. 066 * 067 * @return a TTest instance 068 */ 069 public static TTest getTTest() { 070 return tTest; 071 } 072 073 /** 074 * Set the (singleton) ChiSquareTest instance. 075 * 076 * @param chiSquareTest the new instance to use 077 * @since 1.2 078 */ 079 public static void setChiSquareTest(ChiSquareTest chiSquareTest) { 080 TestUtils.chiSquareTest = chiSquareTest; 081 } 082 083 /** 084 * Return a (singleton) ChiSquareTest instance. Does not create a new instance. 085 * 086 * @return a ChiSquareTest instance 087 */ 088 public static ChiSquareTest getChiSquareTest() { 089 return chiSquareTest; 090 } 091 092 /** 093 * Set the (singleton) UnknownDistributionChiSquareTest instance. 094 * 095 * @param unknownDistributionChiSquareTest the new instance to use 096 * @since 1.2 097 */ 098 public static void setUnknownDistributionChiSquareTest(UnknownDistributionChiSquareTest unknownDistributionChiSquareTest) { 099 TestUtils.unknownDistributionChiSquareTest = unknownDistributionChiSquareTest; 100 } 101 102 /** 103 * Return a (singleton) UnknownDistributionChiSquareTest instance. Does not create a new instance. 104 * 105 * @return a UnknownDistributionChiSquareTest instance 106 */ 107 public static UnknownDistributionChiSquareTest getUnknownDistributionChiSquareTest() { 108 return unknownDistributionChiSquareTest; 109 } 110 111 /** 112 * Set the (singleton) OneWayAnova instance 113 * 114 * @param oneWayAnova the new instance to use 115 * @since 1.2 116 */ 117 public static void setOneWayAnova(OneWayAnova oneWayAnova) { 118 TestUtils.oneWayAnova = oneWayAnova; 119 } 120 121 /** 122 * Return a (singleton) OneWayAnova instance. Does not create a new instance. 123 * 124 * @return a OneWayAnova instance 125 * @since 1.2 126 */ 127 public static OneWayAnova getOneWayAnova() { 128 return oneWayAnova; 129 } 130 131 132 // CHECKSTYLE: stop JavadocMethodCheck 133 134 /** 135 * @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(double[], double[]) 136 */ 137 public static double homoscedasticT(double[] sample1, double[] sample2) 138 throws IllegalArgumentException { 139 return tTest.homoscedasticT(sample1, sample2); 140 } 141 142 /** 143 * @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) 144 */ 145 public static double homoscedasticT(StatisticalSummary sampleStats1, 146 StatisticalSummary sampleStats2) 147 throws IllegalArgumentException { 148 return tTest.homoscedasticT(sampleStats1, sampleStats2); 149 } 150 151 /** 152 * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[], double) 153 */ 154 public static boolean homoscedasticTTest(double[] sample1, double[] sample2, 155 double alpha) 156 throws IllegalArgumentException, MathException { 157 return tTest. homoscedasticTTest(sample1, sample2, alpha); 158 } 159 160 /** 161 * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[]) 162 */ 163 public static double homoscedasticTTest(double[] sample1, double[] sample2) 164 throws IllegalArgumentException, MathException { 165 return tTest.homoscedasticTTest(sample1, sample2); 166 } 167 168 /** 169 * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) 170 */ 171 public static double homoscedasticTTest(StatisticalSummary sampleStats1, 172 StatisticalSummary sampleStats2) 173 throws IllegalArgumentException, MathException { 174 return tTest.homoscedasticTTest(sampleStats1, sampleStats2); 175 } 176 177 /** 178 * @see org.apache.commons.math.stat.inference.TTest#pairedT(double[], double[]) 179 */ 180 public static double pairedT(double[] sample1, double[] sample2) 181 throws IllegalArgumentException, MathException { 182 return tTest.pairedT(sample1, sample2); 183 } 184 185 /** 186 * @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[], double) 187 */ 188 public static boolean pairedTTest(double[] sample1, double[] sample2, 189 double alpha) 190 throws IllegalArgumentException, MathException { 191 return tTest.pairedTTest(sample1, sample2, alpha); 192 } 193 194 /** 195 * @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[]) 196 */ 197 public static double pairedTTest(double[] sample1, double[] sample2) 198 throws IllegalArgumentException, MathException { 199 return tTest.pairedTTest(sample1, sample2); 200 } 201 202 /** 203 * @see org.apache.commons.math.stat.inference.TTest#t(double, double[]) 204 */ 205 public static double t(double mu, double[] observed) 206 throws IllegalArgumentException { 207 return tTest.t(mu, observed); 208 } 209 210 /** 211 * @see org.apache.commons.math.stat.inference.TTest#t(double, org.apache.commons.math.stat.descriptive.StatisticalSummary) 212 */ 213 public static double t(double mu, StatisticalSummary sampleStats) 214 throws IllegalArgumentException { 215 return tTest.t(mu, sampleStats); 216 } 217 218 /** 219 * @see org.apache.commons.math.stat.inference.TTest#t(double[], double[]) 220 */ 221 public static double t(double[] sample1, double[] sample2) 222 throws IllegalArgumentException { 223 return tTest.t(sample1, sample2); 224 } 225 226 /** 227 * @see org.apache.commons.math.stat.inference.TTest#t(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) 228 */ 229 public static double t(StatisticalSummary sampleStats1, 230 StatisticalSummary sampleStats2) 231 throws IllegalArgumentException { 232 return tTest.t(sampleStats1, sampleStats2); 233 } 234 235 /** 236 * @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[], double) 237 */ 238 public static boolean tTest(double mu, double[] sample, double alpha) 239 throws IllegalArgumentException, MathException { 240 return tTest.tTest(mu, sample, alpha); 241 } 242 243 /** 244 * @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[]) 245 */ 246 public static double tTest(double mu, double[] sample) 247 throws IllegalArgumentException, MathException { 248 return tTest.tTest(mu, sample); 249 } 250 251 /** 252 * @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary, double) 253 */ 254 public static boolean tTest(double mu, StatisticalSummary sampleStats, 255 double alpha) 256 throws IllegalArgumentException, MathException { 257 return tTest. tTest(mu, sampleStats, alpha); 258 } 259 260 /** 261 * @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary) 262 */ 263 public static double tTest(double mu, StatisticalSummary sampleStats) 264 throws IllegalArgumentException, MathException { 265 return tTest.tTest(mu, sampleStats); 266 } 267 268 /** 269 * @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[], double) 270 */ 271 public static boolean tTest(double[] sample1, double[] sample2, double alpha) 272 throws IllegalArgumentException, MathException { 273 return tTest.tTest(sample1, sample2, alpha); 274 } 275 276 /** 277 * @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[]) 278 */ 279 public static double tTest(double[] sample1, double[] sample2) 280 throws IllegalArgumentException, MathException { 281 return tTest.tTest(sample1, sample2); 282 } 283 284 /** 285 * @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary, double) 286 */ 287 public static boolean tTest(StatisticalSummary sampleStats1, 288 StatisticalSummary sampleStats2, double alpha) 289 throws IllegalArgumentException, MathException { 290 return tTest. tTest(sampleStats1, sampleStats2, alpha); 291 } 292 293 /** 294 * @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary) 295 */ 296 public static double tTest(StatisticalSummary sampleStats1, 297 StatisticalSummary sampleStats2) 298 throws IllegalArgumentException, MathException { 299 return tTest.tTest(sampleStats1, sampleStats2); 300 } 301 302 /** 303 * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquare(double[], long[]) 304 */ 305 public static double chiSquare(double[] expected, long[] observed) 306 throws IllegalArgumentException { 307 return chiSquareTest.chiSquare(expected, observed); 308 } 309 310 /** 311 * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquare(long[][]) 312 */ 313 public static double chiSquare(long[][] counts) 314 throws IllegalArgumentException { 315 return chiSquareTest.chiSquare(counts); 316 } 317 318 /** 319 * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(double[], long[], double) 320 */ 321 public static boolean chiSquareTest(double[] expected, long[] observed, 322 double alpha) 323 throws IllegalArgumentException, MathException { 324 return chiSquareTest.chiSquareTest(expected, observed, alpha); 325 } 326 327 /** 328 * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(double[], long[]) 329 */ 330 public static double chiSquareTest(double[] expected, long[] observed) 331 throws IllegalArgumentException, MathException { 332 return chiSquareTest.chiSquareTest(expected, observed); 333 } 334 335 /** 336 * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(long[][], double) 337 */ 338 public static boolean chiSquareTest(long[][] counts, double alpha) 339 throws IllegalArgumentException, MathException { 340 return chiSquareTest. chiSquareTest(counts, alpha); 341 } 342 343 /** 344 * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(long[][]) 345 */ 346 public static double chiSquareTest(long[][] counts) 347 throws IllegalArgumentException, MathException { 348 return chiSquareTest. chiSquareTest(counts); 349 } 350 351 /** 352 * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareDataSetsComparison(long[], long[]) 353 * 354 * @since 1.2 355 */ 356 public static double chiSquareDataSetsComparison(long[] observed1, long[] observed2) 357 throws IllegalArgumentException { 358 return unknownDistributionChiSquareTest.chiSquareDataSetsComparison(observed1, observed2); 359 } 360 361 /** 362 * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareTestDataSetsComparison(long[], long[]) 363 * 364 * @since 1.2 365 */ 366 public static double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2) 367 throws IllegalArgumentException, MathException { 368 return unknownDistributionChiSquareTest.chiSquareTestDataSetsComparison(observed1, observed2); 369 } 370 371 372 /** 373 * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double) 374 * 375 * @since 1.2 376 */ 377 public static boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2, 378 double alpha) 379 throws IllegalArgumentException, MathException { 380 return unknownDistributionChiSquareTest.chiSquareTestDataSetsComparison(observed1, observed2, alpha); 381 } 382 383 /** 384 * @see org.apache.commons.math.stat.inference.OneWayAnova#anovaFValue(Collection) 385 * 386 * @since 1.2 387 */ 388 public static double oneWayAnovaFValue(Collection<double[]> categoryData) 389 throws IllegalArgumentException, MathException { 390 return oneWayAnova.anovaFValue(categoryData); 391 } 392 393 /** 394 * @see org.apache.commons.math.stat.inference.OneWayAnova#anovaPValue(Collection) 395 * 396 * @since 1.2 397 */ 398 public static double oneWayAnovaPValue(Collection<double[]> categoryData) 399 throws IllegalArgumentException, MathException { 400 return oneWayAnova.anovaPValue(categoryData); 401 } 402 403 /** 404 * @see org.apache.commons.math.stat.inference.OneWayAnova#anovaTest(Collection,double) 405 * 406 * @since 1.2 407 */ 408 public static boolean oneWayAnovaTest(Collection<double[]> categoryData, double alpha) 409 throws IllegalArgumentException, MathException { 410 return oneWayAnova.anovaTest(categoryData, alpha); 411 } 412 413 // CHECKSTYLE: resume JavadocMethodCheck 414 415 }