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: 762087 $ $Date: 2009-04-05 10:20:18 -0400 (Sun, 05 Apr 2009) $
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      @Override
41      public ContinuousDistribution makeDistribution() {
42          return new ChiSquaredDistributionImpl(5.0);
43      }   
44      
45      /** Creates the default cumulative probability distribution test input values */
46      @Override
47      public double[] makeCumulativeTestPoints() {
48          // quantiles computed using R version 1.8.1 (linux version)
49          return new double[] {0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d, 
50                  20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d};
51      }
52      
53      /** Creates the default cumulative probability density test expected values */
54      @Override
55      public double[] makeCumulativeTestValues() {
56          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
57                  0.990d, 0.975d, 0.950d, 0.900d}; 
58      }
59      
60      /** Creates the default inverse cumulative probability test input values */
61      @Override
62      public double[] makeInverseCumulativeTestPoints() {
63          return new double[] {0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
64                  0.990d, 0.975d, 0.950d, 0.900d, 1};     
65      }
66      
67      /** Creates the default inverse cumulative probability density test expected values */
68      @Override
69      public double[] makeInverseCumulativeTestValues() {
70          return new double[] {0, 0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d, 
71                  20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d, 
72                  Double.POSITIVE_INFINITY};
73      }
74      
75   // --------------------- Override tolerance  --------------
76      @Override
77      protected void setUp() throws Exception {
78          super.setUp();
79          setTolerance(5e-6);
80      }
81  
82   //---------------------------- Additional test cases -------------------------
83      
84      public void testSmallDf() throws Exception {
85          setDistribution(new ChiSquaredDistributionImpl(0.1d));
86          setTolerance(1E-4);
87          // quantiles computed using R version 1.8.1 (linux version)
88          setCumulativeTestPoints(new double[] {1.168926E-60, 1.168926E-40, 1.063132E-32, 
89                  1.144775E-26, 1.168926E-20, 5.472917, 2.175255, 1.13438, 
90                  0.5318646, 0.1526342});
91          setInverseCumulativeTestValues(getCumulativeTestPoints());
92          setInverseCumulativeTestPoints(getCumulativeTestValues());
93          verifyCumulativeProbabilities();
94          verifyInverseCumulativeProbabilities();
95      }
96      
97      public void testDfAccessors() {
98          ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution();
99          assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
100         distribution.setDegreesOfFreedom(4d);
101         assertEquals(4d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
102         try {
103             distribution.setDegreesOfFreedom(0d);
104             fail("Expecting IllegalArgumentException for df = 0");
105         } catch (IllegalArgumentException ex) {
106             // expected
107         }
108     }
109 
110     public void testDensity() {
111         double[] x = new double[]{-0.1, 1e-6, 0.5, 1, 2, 5};
112         //R 2.5: print(dchisq(x, df=1), digits=10)
113         checkDensity(1, x, new double[]{0.00000000000, 398.94208093034, 0.43939128947, 0.24197072452, 0.10377687436, 0.01464498256});
114         //R 2.5: print(dchisq(x, df=0.1), digits=10)
115         checkDensity(0.1, x, new double[]{0.000000000e+00, 2.486453997e+04, 7.464238732e-02, 3.009077718e-02, 9.447299159e-03, 8.827199396e-04});
116         //R 2.5: print(dchisq(x, df=2), digits=10)
117         checkDensity(2, x, new double[]{0.00000000000, 0.49999975000, 0.38940039154, 0.30326532986, 0.18393972059, 0.04104249931});
118         //R 2.5: print(dchisq(x, df=10), digits=10)
119         checkDensity(10, x, new double[]{0.000000000e+00, 1.302082682e-27, 6.337896998e-05, 7.897534632e-04, 7.664155024e-03, 6.680094289e-02});
120     }
121 
122     private void checkDensity(double df, double[] x, double[] expected) {
123         ChiSquaredDistribution d = new ChiSquaredDistributionImpl(df);
124         for (int i = 0; i < x.length; i++) {
125             assertEquals(expected[i], d.density(x[i]), 1e-5);
126         }
127     }
128 
129 }