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  
18  package org.apache.commons.math.distribution;
19  
20  /**
21   * Test cases for ChiSquareDistribution.
22   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
23   * ContinuousDistributionAbstractTest for details.
24   * 
25   * @version $Revision: 563850 $ $Date: 2007-08-08 06:18:46 -0700 (Wed, 08 Aug 2007) $
26   */
27  public class ChiSquareDistributionTest extends ContinuousDistributionAbstractTest {
28      
29      /**
30       * Constructor for ChiSquareDistributionTest.
31       * @param name
32       */
33      public ChiSquareDistributionTest(String name) {
34          super(name);
35      }
36      
37      //-------------- Implementations for abstract methods -----------------------
38      
39      /** Creates the default continuous distribution instance to use in tests. */
40      public ContinuousDistribution makeDistribution() {
41          return new ChiSquaredDistributionImpl(5.0);
42      }   
43      
44      /** Creates the default cumulative probability distribution test input values */
45      public double[] makeCumulativeTestPoints() {
46          // quantiles computed using R version 1.8.1 (linux version)
47          return new double[] {0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d, 
48                  20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d};
49      }
50      
51      /** Creates the default cumulative probability density test expected values */
52      public double[] makeCumulativeTestValues() {
53          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
54                  0.990d, 0.975d, 0.950d, 0.900d}; 
55      }
56      
57      /** Creates the default inverse cumulative probability test input values */
58      public double[] makeInverseCumulativeTestPoints() {
59          return new double[] {0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
60                  0.990d, 0.975d, 0.950d, 0.900d, 1};     
61      }
62      
63      /** Creates the default inverse cumulative probability density test expected values */
64      public double[] makeInverseCumulativeTestValues() {
65          return new double[] {0, 0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d, 
66                  20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d, 
67                  Double.POSITIVE_INFINITY};
68      }
69      
70   // --------------------- Override tolerance  --------------
71      protected void setUp() throws Exception {
72          super.setUp();
73          setTolerance(5e-6);
74      }
75  
76   //---------------------------- Additional test cases -------------------------
77      
78      public void testSmallDf() throws Exception {
79          setDistribution(new ChiSquaredDistributionImpl(0.1d));
80          setTolerance(1E-4);
81          // quantiles computed using R version 1.8.1 (linux version)
82          setCumulativeTestPoints(new double[] {1.168926E-60, 1.168926E-40, 1.063132E-32, 
83                  1.144775E-26, 1.168926E-20, 5.472917, 2.175255, 1.13438, 
84                  0.5318646, 0.1526342});
85          setInverseCumulativeTestValues(getCumulativeTestPoints());
86          setInverseCumulativeTestPoints(getCumulativeTestValues());
87          verifyCumulativeProbabilities();
88          verifyInverseCumulativeProbabilities();
89      }
90      
91      public void testDfAccessors() {
92          ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution();
93          assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
94          distribution.setDegreesOfFreedom(4d);
95          assertEquals(4d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
96          try {
97              distribution.setDegreesOfFreedom(0d);
98              fail("Expecting IllegalArgumentException for df = 0");
99          } catch (IllegalArgumentException ex) {
100             // expected
101         }
102     } 
103     
104 }