    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16
    17  package org.apache.commons.math.distribution;
    18
    19  /***
    20   * Test cases for ChiSquareDistribution.
    21   * Extends ContinuousDistributionAbstractTest.  See class javadoc for
    22   * ContinuousDistributionAbstractTest for details.
    23   *
    24   * @version $Revision: 1.16 $ $Date: 2004/07/24 20:43:29 $
    25   */
    26  public class ChiSquareDistributionTest extends ContinuousDistributionAbstractTest {
    27
    28      /***
    29       * Constructor for ChiSquareDistributionTest.
    30       * @param name
    31       */
    32      public ChiSquareDistributionTest(String name) {
    33          super(name);
    34      }
    35
    36      //-------------- Implementations for abstract methods -----------------------
    37
    38      /*** Creates the default continuous distribution instance to use in tests. */
    39      public ContinuousDistribution makeDistribution() {
    40          return DistributionFactory.newInstance().createChiSquareDistribution(5.0);
    41      }
    42
    43      /*** Creates the default cumulative probability distribution test input values */
    44      public double[] makeCumulativeTestPoints() {
    45          // quantiles computed using R version 1.8.1 (linux version)
    46          return new double[] {0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d,
    47                  20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d};
    48      }
    49
    50      /*** Creates the default cumulative probability density test expected values */
    51      public double[] makeCumulativeTestValues() {
    52          return new double[] {0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
    53                  0.990d, 0.975d, 0.950d, 0.900d};
    54      }
    55
    56      /*** Creates the default inverse cumulative probability test input values */
    57      public double[] makeInverseCumulativeTestPoints() {
    58          return new double[] {0, 0.001d, 0.01d, 0.025d, 0.05d, 0.1d, 0.999d,
    59                  0.990d, 0.975d, 0.950d, 0.900d, 1};
    60      }
    61
    62      /*** Creates the default inverse cumulative probability density test expected values */
    63      public double[] makeInverseCumulativeTestValues() {
    64          return new double[] {0, 0.210216d, 0.5542981d, 0.8312116d, 1.145476d, 1.610308d,
    65                  20.51501d, 15.08627d, 12.83250d, 11.07050d, 9.236357d,
    66                  Double.POSITIVE_INFINITY};
    67      }
    68
    69   // --------------------- Override tolerance  --------------
    70      protected void setup() throws Exception {
    71          super.setUp();
    72          setTolerance(1E-6);
    73      }
    74
    75   //---------------------------- Additional test cases -------------------------
    76
    77      public void testSmallDf() throws Exception {
    78          setDistribution(DistributionFactory.newInstance().createChiSquareDistribution(0.1d));
    79          setTolerance(1E-4);
    80          // quantiles computed using R version 1.8.1 (linux version)
    81          setCumulativeTestPoints(new double[] {1.168926E-60, 1.168926E-40, 1.063132E-32,
    82                  1.144775E-26, 1.168926E-20, 5.472917, 2.175255, 1.13438,
    83                  0.5318646, 0.1526342});
    84          setInverseCumulativeTestValues(getCumulativeTestPoints());
    85          setInverseCumulativeTestPoints(getCumulativeTestValues());
    86          verifyCumulativeProbabilities();
    87          verifyInverseCumulativeProbabilities();
    88      }
    89
    90      public void testDfAccessors() {
    91          ChiSquaredDistribution distribution = (ChiSquaredDistribution) getDistribution();
    92          assertEquals(5d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
    93          distribution.setDegreesOfFreedom(4d);
    94          assertEquals(4d, distribution.getDegreesOfFreedom(), Double.MIN_VALUE);
    95          try {
    96              distribution.setDegreesOfFreedom(0d);
    97              fail("Expecting IllegalArgumentException for df = 0");
    98          } catch (IllegalArgumentException ex) {
    99              // expected
    100         }
    101     }
    102
    103 }