View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.stat.inference;
18  
19  import java.util.Collection;
20  import org.apache.commons.math.MathException;
21  import org.apache.commons.math.stat.descriptive.StatisticalSummary;
22  
23  /**
24   * A collection of static methods to create inference test instances or to
25   * perform inference tests.
26   *
27   * @since 1.1
28   * @version $Revision: 618114 $ $Date: 2008-02-03 15:48:38 -0700 (Sun, 03 Feb 2008) $ 
29   */
30  public class TestUtils  {
31      /**
32       * Prevent instantiation.
33       */
34      protected TestUtils() {
35          super();
36      }
37      
38      /** Singleton TTest instance using default implementation. */
39      private static TTest tTest = new TTestImpl();
40     
41      /** Singleton ChiSquareTest instance using default implementation. */
42      private static ChiSquareTest chiSquareTest = 
43          new ChiSquareTestImpl();
44      
45      /** Singleton ChiSquareTest instance using default implementation. */
46      private static UnknownDistributionChiSquareTest unknownDistributionChiSquareTest = 
47          new ChiSquareTestImpl();
48      
49      /** Singleton OneWayAnova instance using default implementation. */
50      private static OneWayAnova oneWayAnova =
51          new OneWayAnovaImpl();
52      
53      /**
54       * Set the (singleton) TTest instance.
55       * 
56       * @param tTest the new instance to use
57       * @since 1.2
58       */
59      public static void setChiSquareTest(TTest tTest) {
60          TestUtils.tTest = tTest;
61      }
62      
63      /**
64       * Return a (singleton) TTest instance.  Does not create a new instance.
65       * 
66       * @return a TTest instance
67       */
68      public static TTest getTTest() {
69          return tTest;
70      }
71      
72      /**
73       * Set the (singleton) ChiSquareTest instance.
74       * 
75       * @param chiSquareTest the new instance to use
76       * @since 1.2
77       */
78      public static void setChiSquareTest(ChiSquareTest chiSquareTest) {
79          TestUtils.chiSquareTest = chiSquareTest;
80      }
81      
82      /**
83       * Return a (singleton) ChiSquareTest instance.  Does not create a new instance.
84       * 
85       * @return a ChiSquareTest instance
86       */
87      public static ChiSquareTest getChiSquareTest() {
88          return chiSquareTest;
89      }
90      
91      /**
92       * Set the (singleton) UnknownDistributionChiSquareTest instance.
93       * 
94       * @param unknownDistributionChiSquareTest the new instance to use
95       * @since 1.2
96       */
97      public static void setUnknownDistributionChiSquareTest(UnknownDistributionChiSquareTest unknownDistributionChiSquareTest) {
98          TestUtils.unknownDistributionChiSquareTest = unknownDistributionChiSquareTest;
99      }
100     
101     /**
102      * Return a (singleton) UnknownDistributionChiSquareTest instance.  Does not create a new instance.
103      * 
104      * @return a UnknownDistributionChiSquareTest instance
105      */
106     public static UnknownDistributionChiSquareTest getUnknownDistributionChiSquareTest() {
107         return unknownDistributionChiSquareTest;
108     }
109     
110     /**
111      * Set the (singleton) OneWayAnova instance
112      * 
113      * @param oneWayAnova the new instance to use
114      * @since 1.2
115      */
116     public static void setOneWayAnova(OneWayAnova oneWayAnova) {
117         TestUtils.oneWayAnova = oneWayAnova;
118     }
119     
120     /**
121      * Return a (singleton) OneWayAnova instance.  Does not create a new instance.
122      * 
123      * @return a OneWayAnova instance
124      * @since 1.2
125      */
126     public static OneWayAnova getOneWayAnova() {
127         return oneWayAnova;
128     }
129     
130     
131     /**
132      * @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(double[], double[])
133      */
134     public static double homoscedasticT(double[] sample1, double[] sample2)
135         throws IllegalArgumentException {
136         return tTest.homoscedasticT(sample1, sample2);
137     }
138 
139     /**
140      * @see org.apache.commons.math.stat.inference.TTest#homoscedasticT(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
141      */
142     public static double homoscedasticT(StatisticalSummary sampleStats1,
143         StatisticalSummary sampleStats2)
144         throws IllegalArgumentException {
145         return tTest.homoscedasticT(sampleStats1, sampleStats2);
146     }
147 
148     /**
149      * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[], double)
150      */
151     public static boolean homoscedasticTTest(double[] sample1, double[] sample2,
152             double alpha)
153         throws IllegalArgumentException, MathException {
154         return tTest. homoscedasticTTest(sample1, sample2, alpha);
155     }
156 
157     /**
158      * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(double[], double[])
159      */
160     public static double homoscedasticTTest(double[] sample1, double[] sample2)
161         throws IllegalArgumentException, MathException {
162         return tTest.homoscedasticTTest(sample1, sample2);
163     }
164 
165     /**
166      * @see org.apache.commons.math.stat.inference.TTest#homoscedasticTTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
167      */
168     public static double homoscedasticTTest(StatisticalSummary sampleStats1,
169         StatisticalSummary sampleStats2)
170         throws IllegalArgumentException, MathException {
171         return tTest.homoscedasticTTest(sampleStats1, sampleStats2);
172     }
173 
174     /**
175      * @see org.apache.commons.math.stat.inference.TTest#pairedT(double[], double[])
176      */
177     public static double pairedT(double[] sample1, double[] sample2)
178         throws IllegalArgumentException, MathException {
179         return tTest.pairedT(sample1, sample2);
180     }
181 
182     /**
183      * @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[], double)
184      */
185     public static boolean pairedTTest(double[] sample1, double[] sample2,
186         double alpha)
187         throws IllegalArgumentException, MathException {
188         return tTest.pairedTTest(sample1, sample2, alpha);
189     }
190 
191     /**
192      * @see org.apache.commons.math.stat.inference.TTest#pairedTTest(double[], double[])
193      */
194     public static double pairedTTest(double[] sample1, double[] sample2)
195         throws IllegalArgumentException, MathException {
196         return tTest.pairedTTest(sample1, sample2);
197     }
198 
199     /**
200      * @see org.apache.commons.math.stat.inference.TTest#t(double, double[])
201      */
202     public static double t(double mu, double[] observed)
203         throws IllegalArgumentException {
204         return tTest.t(mu, observed);
205     }
206 
207     /**
208      * @see org.apache.commons.math.stat.inference.TTest#t(double, org.apache.commons.math.stat.descriptive.StatisticalSummary)
209      */
210     public static double t(double mu, StatisticalSummary sampleStats)
211         throws IllegalArgumentException {
212         return tTest.t(mu, sampleStats);
213     }
214 
215     /**
216      * @see org.apache.commons.math.stat.inference.TTest#t(double[], double[])
217      */
218     public static double t(double[] sample1, double[] sample2)
219         throws IllegalArgumentException {
220         return tTest.t(sample1, sample2);
221     }
222 
223     /**
224      * @see org.apache.commons.math.stat.inference.TTest#t(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
225      */
226     public static double t(StatisticalSummary sampleStats1,
227             StatisticalSummary sampleStats2)
228         throws IllegalArgumentException {
229         return tTest.t(sampleStats1, sampleStats2);
230     }
231 
232     /**
233      * @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[], double)
234      */
235     public static boolean tTest(double mu, double[] sample, double alpha)
236         throws IllegalArgumentException, MathException {
237         return tTest.tTest(mu, sample, alpha);
238     }
239 
240     /**
241      * @see org.apache.commons.math.stat.inference.TTest#tTest(double, double[])
242      */
243     public static double tTest(double mu, double[] sample)
244         throws IllegalArgumentException, MathException {
245         return tTest.tTest(mu, sample);
246     }
247 
248     /**
249      * @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary, double)
250      */
251     public static boolean tTest(double mu, StatisticalSummary sampleStats,
252         double alpha)
253         throws IllegalArgumentException, MathException {
254         return tTest. tTest(mu, sampleStats, alpha);
255     }
256 
257     /**
258      * @see org.apache.commons.math.stat.inference.TTest#tTest(double, org.apache.commons.math.stat.descriptive.StatisticalSummary)
259      */
260     public static double tTest(double mu, StatisticalSummary sampleStats)
261         throws IllegalArgumentException, MathException {
262         return tTest.tTest(mu, sampleStats);
263     }
264 
265     /**
266      * @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[], double)
267      */
268     public static boolean tTest(double[] sample1, double[] sample2, double alpha)
269         throws IllegalArgumentException, MathException {
270         return tTest.tTest(sample1, sample2, alpha);
271     }
272 
273     /**
274      * @see org.apache.commons.math.stat.inference.TTest#tTest(double[], double[])
275      */
276     public static double tTest(double[] sample1, double[] sample2)
277         throws IllegalArgumentException, MathException {
278         return tTest.tTest(sample1, sample2);
279     }
280 
281     /**
282      * @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary, double)
283      */
284     public static boolean tTest(StatisticalSummary sampleStats1,
285         StatisticalSummary sampleStats2, double alpha)
286         throws IllegalArgumentException, MathException {
287         return tTest. tTest(sampleStats1, sampleStats2, alpha);
288     }
289 
290     /**
291      * @see org.apache.commons.math.stat.inference.TTest#tTest(org.apache.commons.math.stat.descriptive.StatisticalSummary, org.apache.commons.math.stat.descriptive.StatisticalSummary)
292      */
293     public static double tTest(StatisticalSummary sampleStats1,
294         StatisticalSummary sampleStats2)
295         throws IllegalArgumentException, MathException {
296         return tTest.tTest(sampleStats1, sampleStats2);
297     }
298 
299     /**
300      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquare(double[], long[])
301      */
302     public static double chiSquare(double[] expected, long[] observed)
303         throws IllegalArgumentException {
304         return chiSquareTest.chiSquare(expected, observed);
305     }
306 
307     /**
308      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquare(long[][])
309      */
310     public static double chiSquare(long[][] counts) 
311         throws IllegalArgumentException {
312         return chiSquareTest.chiSquare(counts);
313     }
314 
315     /**
316      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(double[], long[], double)
317      */
318     public static boolean chiSquareTest(double[] expected, long[] observed,
319         double alpha)
320         throws IllegalArgumentException, MathException {
321         return chiSquareTest.chiSquareTest(expected, observed, alpha);
322     }
323 
324     /**
325      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(double[], long[])
326      */
327     public static double chiSquareTest(double[] expected, long[] observed)
328         throws IllegalArgumentException, MathException {
329         return chiSquareTest.chiSquareTest(expected, observed);
330     }
331 
332     /**
333      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(long[][], double)
334      */
335     public static boolean chiSquareTest(long[][] counts, double alpha)
336         throws IllegalArgumentException, MathException {
337         return chiSquareTest. chiSquareTest(counts, alpha);
338     }
339 
340     /**
341      * @see org.apache.commons.math.stat.inference.ChiSquareTest#chiSquareTest(long[][])
342      */
343     public static double chiSquareTest(long[][] counts)
344         throws IllegalArgumentException, MathException {
345         return chiSquareTest. chiSquareTest(counts);
346     }
347 
348     /**
349      * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareDataSetsComparison(long[], long[])
350      *
351      * @since 1.2
352      */
353     public static double chiSquareDataSetsComparison(long[] observed1, long[] observed2)
354         throws IllegalArgumentException {
355         return unknownDistributionChiSquareTest.chiSquareDataSetsComparison(observed1, observed2);
356     }
357 
358     /**
359      * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareTestDataSetsComparison(long[], long[])
360      *
361      * @since 1.2
362      */
363     public static double chiSquareTestDataSetsComparison(long[] observed1, long[] observed2)
364         throws IllegalArgumentException, MathException {
365         return unknownDistributionChiSquareTest.chiSquareTestDataSetsComparison(observed1, observed2);
366     }
367 
368 
369     /**
370      * @see org.apache.commons.math.stat.inference.UnknownDistributionChiSquareTest#chiSquareTestDataSetsComparison(long[], long[], double)
371      *
372      * @since 1.2
373      */
374     public static boolean chiSquareTestDataSetsComparison(long[] observed1, long[] observed2,
375         double alpha)
376         throws IllegalArgumentException, MathException {
377         return unknownDistributionChiSquareTest.chiSquareTestDataSetsComparison(observed1, observed2, alpha);
378     }
379     
380     /**
381      * @see org.apache.commons.math.stat.inference.OneWayAnova#anovaFValue(Collection)
382      *
383      * @since 1.2
384      */
385     public static double oneWayAnovaFValue(Collection categoryData)
386     throws IllegalArgumentException, MathException {
387         return oneWayAnova.anovaFValue(categoryData);
388     }
389     
390     /**
391      * @see org.apache.commons.math.stat.inference.OneWayAnova#anovaPValue(Collection)
392      * 
393      * @since 1.2
394      */
395     public static double oneWayAnovaPValue(Collection categoryData)
396     throws IllegalArgumentException, MathException {
397         return oneWayAnova.anovaPValue(categoryData);
398     }
399     
400     /**
401      * @see org.apache.commons.math.stat.inference.OneWayAnova#anovaTest(Collection,double)
402      *
403      * @since 1.2
404      */
405     public static boolean oneWayAnovaTest(Collection categoryData, double alpha)
406     throws IllegalArgumentException, MathException {
407         return oneWayAnova.anovaTest(categoryData, alpha);
408     }
409 
410 }